pystages.cncrouter

class pystages.cncrouter.CNCRouter(dev: str | None = None, reset_wait_time=2.0)

Class to command CNC routers.

__init__(dev: str | None = None, reset_wait_time=2.0)

Open serial device to connect to the CNC routers. Raise a ConnectionFailure exception if the serial device could not be open.

Parameters:
  • dev – Serial device. For instance ‘/dev/ttyUSB0’. If not provided, try to discover a suitable device according to device vendor and product IDs

  • reset_wait_time – Depending on the state of the stage, it can take some time for GRBL to reset. This parameter makes the wait time to be tuned, by giving a time in seconds.

get_current_status() Tuple[CNCStatus, dict] | None

Sending ‘?’ character permits to get the status of the CNC router

Returns:

A tuple containing the status and a dictionary of all other parameters in the output of the command.

get_grbl_settings() dict

Obtains and parse the list of GRBLSettings with the $$ command.

Returns:

A dictionary containing the GRBLSetting as key and its corresponding value

home(wait=False)

Sends a $H command. The stage responds a message [MSG:Sleeping] after ok. Take caution for collisions before calling this method !

Parameters:

wait – Optionally waits for move operation to be done.

property is_moving: bool

Queries the current status of the CNC in order to determine if the CNC is moving

Returns:

True if the CNC reports that a cycle is running (Run) or if it is in a middle of a homing cycle (Home).

property position: Vector

Current stage position. Vector.

Getter:

Query and return stage position.

Setter:

Move the stage.

receive() str

Read input serial buffer to get a response. Blocks until a response is available.

Returns:

Received response string, CR-LF removed.

receive_lines(until: str = 'ok') List[str]

Receive multiple lines until getting as specific value.

Parameters:

until – The expected response indicating the end of received lines.

Returns:

The list of all received lines. Note that the expected line is not included in the list.

reset_grbl(wait_time: float | None = None) bool

Sends a CTRL+X control to reset the GRBL

Returns:

True if the GRBL sent the correct prompt at the end of the reset

Parameters:

wait_time – Depending on the state of the stage, it can take some time for GRBL to reset. This parameter makes the wait time to be tuned, by giving a time in seconds.

send(command: str, eol: str | None = None)

Send a command.

Parameters:
  • command – Command string.

  • eol – End of command character(s). If None, LF will be automatically append.

send_receive(command: str) str

Send a command, wait and return the response.

Parameters:

command – Command string.

Returns:

Received response string, CR-LF removed.

set_grbl_setting(setting: GRBLSetting, value: float | bool | InvertMask | StatusReportMask | int)

Set the GRBL setting of the Router with given value. The value type must correspond to type defined in GRBLSetting.

set_origin() str

Set current position as origin

Returns:

The response of the CNC (‘ok’ if command has been submitted correctly).

sleep() str

Sends a $SLP command. The stage responds a message [MSG:Sleeping] after ok.

Returns:

The response of the CNC (‘ok’ if command has been submitted correctly).

unlock() bool

Unlock the motor. It may happen when the stage has gone further its limits, and raised an alarm, or has been disabled when going in sleep mode ($SLP)

Returns:

True if message [MSG:Caution: Unlock] has been returned

class pystages.cncrouter.CNCStatus(value)

Possible statuses that the CNC can report.

ALARM = 'Alarm'
CHECK = 'Check'
DOOR = 'Door'
HOLD = 'Hold'
HOME = 'Home'
IDLE = 'Idle'
RUN = 'Run'
class pystages.cncrouter.GRBLSetting(value)

GRBL Setting are obtained by sending the ‘$$’ command as a list of key-value pairs ‘$K=V’ with K being a number.

ACCELERATION_X = '$120'
ACCELERATION_Y = '$121'
ACCELERATION_Z = '$122'
ARC_TOLERANCE = '$12'
DIR_PORT_INVERT = '$3'
HARD_LIMITS = '$21'
HOMING_CYCLE = '$22'
HOMING_DEBOUNCE = '$26'
HOMING_DIR_INVERT = '$23'
HOMING_FEED = '$24'
HOMING_PULL_OFF = '$27'
HOMING_SEEK = '$25'
JUNCTION_DEVIATION = '$11'
LASER_MODE = '$32'
LIMIT_PINS_INVERT = '$5'
MAX_RATE_X = '$110'
MAX_RATE_Y = '$111'
MAX_RATE_Z = '$112'
MAX_TRAVEL_X = '$130'
MAX_TRAVEL_Y = '$131'
MAX_TRAVEL_Z = '$132'
PROBE_PIN_INVERT = '$6'
REPORT_INCHES = '$13'
SOFT_LIMITS = '$20'
SPINDLE_RPM_MAX = '$30'
SPINDLE_RPM_MIN = '$31'
STATUS_REPORT_MASK = '$10'
STEPS_PER_MM_X = '$100'
STEPS_PER_MM_Y = '$101'
STEPS_PER_MM_Z = '$102'
STEP_ENABLE_INVERT = '$4'
STEP_IDLE_DELAY = '$1'
STEP_PORT_INVERT = '$2'
STEP_PULSE = '$0'
property default_value: float | bool | InvertMask | StatusReportMask | int

Gives the default value of the GRBL setting

property description: str

Gives the string description of the GRBL setting

property type: type

Gives the type of the value stored in the GRBL setting

class pystages.cncrouter.InvertMask(value)

Invert Mask flags values

INVERT_X = 1
INVERT_Y = 2
INVERT_Z = 4
class pystages.cncrouter.StatusReportMask(value)

Status report flags values

LIMIT_PINS = 16
MACHINE_POSITION = 1
NOTHING = 0
PLANNER_BUFFER = 4
RX_BUFFER = 8
WORK_POSITION = 2