2025 May 7 version, gzip'd tarball
2024 Apr 22 version, gzip'd tarball
2024 Jan 4 version, gzip'd tarball
2023 Aug 4 version, gzip'd tarball
PyDNC is a Python script to send and receive files from CNC controls over a serial line. It uses the PySerial library for the serial port interface, so it should work fine on Windows, Unix, OS X, etc. It has been tested with USB to RS232 converters with Haas, Yasnac, and Fadal controls with no problems at up to 38400 bps. Provision is made for various data bit options, stop bit options, parity, and baud rates.
Of particular use is that when sending data to the control, the script checks for an XOFF code every character to prevent RS232 overrun problems in the ancient controls. Note that most current Linux serial drivers for USB converters do NOT implement software (XON/XOFF) flow control in the driver!
From the README file in the archive:
These programs are useful for making, sending, and receiving CAM files to and from a CNC control. Particularly ancient controls without macros, subroutines, or general math processing. There are also programs for modifying g-code files, extracting the travel limits from a g-code file, and removing dis-allowed code from a g-code file.
Process a g-code file, renumbering the lines as it goes. Drops existing line numbers, and renumbers ALL g-code lines, except O-word and "%" lines. Set the line number offset, and the roll-over point for those controls with maximum line numbers.
Compute various statistics of a g-code file. Mostly, find the min and max X, Y, Z values for each tool, in order. Reports the max and min coordinates for each tool, so an operator can work out if tools stick out far enough _before_ running the code.
Strip all comments, line numbers, etc. from a g-code file for those controls that can't handle such stuff in a DNC file. Specifically, note that Fadal controls won't accept O-words, line numbers, or comments in a program being drip-fed to the control.
Offset or scale all the X, Y, and/or Z values in a g-code file. Can also swap spindle speed and feed rate commands. The spindle and feed replacements are matching the spindle and feed rate, not the tool number! Can be very helpful for modifying a g-code file from one machine to another (different max spindle, max feed, etc.). Can also be used to mirror or offset code.
Convert g-code file to positions for plotting with gnuplot. Input is a g-code file, output is a long series of x,y,z,mode lines for each move in the g-code file. The mode is taken from the currently active motion mode (0, 1, 2, 3) for each move. The mode number has 1 added to the number for plotting in gnuplot, as line color 0 is special.
Python pre-processor to make a DNC-ready g-code-only file from a source file with g-code, macros, and setup comments. The macros allow a higher level of cam programming than straight hand-coding the coordinates. With a CAD program that can provide a consistent set of x, y, z coordinates for corners, the macros can be used as a CAM programming language for 2.5-D cutting.
The Fadal post-processor, fadal_post.py, is for the FreeCAD Path workbench, and produces g code that conforms to the Format 2 Fadal specification, which is closer to Fanuc dialect. The post does not currently handle tapping jobs correctly, although that may be partly due to issues with the Fadal mill being used for testing.
Improvements welcome, as they will only be made here as problems have to be overcome.
The MX3 post-processor, fadal_post.py, is for the FreeCAD Path workbench, and produces g code that conforms to the MX3 minimal dialext. The development controller does not have most of the options, but does include arcs! The post does not currently handle tapping jobs correctly, although that may be partly due to issues with the Fadal mill being used for testing.
Improvements welcome, as they will only be made here as problems have to be overcome.
A user submitted a link to a Python GUI for the pydnc script: github link
I have not tested the GUI yet, but it looks like an excellent addition.
Python pre-processor to make a DNC-ready g-code-only file from a source file with g-code, macros, and setup comments. The macros allow a higher level of cam programming than straight hand-coding the coordinates. With a CAD program that can provide a consistent set of x, y, z coordinates for corners, the macros can be used as a CAM programming language for 2.5-D cutting.
Create the source file with embedded setup comments starting with "#" - these comments must be the whole line! Embedded g-code does not have any particular marker - just enter g-code as it should be used. Macros start a line, and are the only item on a line - do not try to mix g-code or macros on the same line. Macro tokens take a list of parameters or other specific format; see below for the format for each token.
Recursion is supported in the subroutines and included files; lines are processed for macros at output. Macro expansion happens after coordinate replacement (in the subroutine).
This script takes input from stdin, and outputs to stdout and stderr. G-code output is on stdout, and notifications and setup comments (those lines starting with "#") are on stderr. Typical use:
[gungnir.local] cam_macros.py < source_file > dnc_gcode 2> setup_notes
NIBBLE_X Xxs,xe Yys,ye Sdelta
NIBBLE_X X-8.0,-3.942 Y-0.2765,+0.2765 d0.100
NIBBLE_Y Xxs,xe Yys,ye Sdelta
NIBBLE_Y X-12.410,-11.660 Y+0.2765,+0.5290 d0.100
PERIMETER Ddiameter L xxxx,yyyy[,zzzz] nan for coordinate means skip.... END
Run around a perimeter, in order. Code with individual points on each line (1 per line), ending with "END" on a line. Profile is only closed if start point added at end of list. Any coordinate entered as "nan" will be treated as the same as the last used coordinate. Z values optional on all lines, but only added if entered. X and Y coordinates needed for each line, even if Y is nan.
If present, the D token specifies a tool diameter, and the perimeter coordinates are offset by the tool radius (diameter/2). The "L" token flips the side of compensation to the left, as viewed moving along the coordinates. Without the "L" token, compensation moves the tool to the right. Compensation should be correct for any angles and radius, although small areas which do not fit the tool may cause unexpected results. A perimeter which cannot fit the tool could cause incorrect or unusable tool paths!
SUB Xxs,xe Yys,ye Zzs,ze Sdelta g-code g-code . . . END
Repeat a chunk of g-code once for each x,y,z value between zs and ze, tepping down by no more than delta. The entire chunk of code until END is repeated at each Z level, with a G1 Z move to each new depth at the start of each repetition. This is very similar to calling a subroutine in most controls, but can be used in those crippled controls without subroutines or macros. No provision for expanding macros inside the subroutine!
Note that all listed axes will be stepped each repetition. This is helpful for turning the OD on a typical lathe. Also, if SET RAMP is active the axis changes will be done on the first line of the subroutine, rather than on a generated G1 move.
INCLUDE filename
HELIX Xx.xxxx Yy.yyyy Zz_start,z_end Sdelta Ddiameter Rradius N
HELIX X-7.9000 Y0. Z0.00,-0.500 d0.0625 r0.1
SLOT Xxs,xe Yys,ye Zzs,ze Rradius Sz_step C
RAMP Xxs,xe Yys,ye Zzs,ze Rradius Sz_step C
TOOL number
SET G43 - add g43 with z move after tool change SET G53 - add g53 Z0. before tool change SET ZTOOL - set Z-position for G43 move after tool change SET SPLIT_G53 - Split the G0 from the G53 Z0. before a tool change. Outputs a block with just "G0", then a block with "G53 Z0." SET NO_SPLIT_G53 - Use G0 G53 Z0. before a tool change, with the G0 and G53 Z0. in one block. SET NOC - turn off _all_ comments; strips comments from g-code and suppress macro comments SET COMMENT - turn on _all comments; leave g-code comments, and add macro expansion comments SET ARC2LINE - convert G2, G3 moves into G1 with line segments SET ARCP - set precision for converting arcs to lines SET ARCS - stop converting arcs to line segments SET NBLOCKS - output computed Nxxxx at the start of non-comment lines SET NONBLOCKS - turn off all Nxxx at the start of all lines SET NMAX - maximum line number; resets when reached SET NSTEP - increment Nxxxx line numbers by this much each time SET RAMP - In subroutines (SUB ...... END), change iterating axes on first line of subroutine, rather than direct G1 move added to subroutine. SET NORAMP - In subroutines (SUB .... END), change iterating axes at the start of each repetition on an added G1 move; then repeat the subroutine lines.
Each set flag remains until explicitly cancelled.