gscrib.writersο
G-code output to files and machine connections.
This module provides utilities for writing G-code to different outputs, including files, network sockets, and serial connections.
- class gscrib.writers.BaseWriterο
Bases:
ABCBase class for all the G-code writing implementations.
This class defines the interface for writing G-code commands to various outputs, such as files, serial connections, or network sockets.
- abstractmethod connect()ο
Establish connection or open resource for writing
- Return type:
- abstractmethod disconnect(wait=True)ο
Close connection or resource
- Parameters:
wait (bool)
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- class gscrib.writers.ConsoleWriter(stderr=False)ο
Bases:
FileWriterWriter that outputs commands to the console.
This class implements a G-code writer specifically designed for console output.
Example
>>> writer = ConsoleWriter() >>> writer.write(b"G1 X10 Y10\n")
- Parameters:
stderr (bool)
- connect()ο
Establish the connection to the console output.
- Return type:
- disconnect(wait=True)ο
Close the file if it was opened by this writer.
- Parameters:
wait (bool)
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- class gscrib.writers.FileWriter(output)ο
Bases:
BaseWriterWriter that outputs commands to a file or file-like object.
This class implements a G-code writer that can write G-code commands to a file. The writer handles both text and binary output modes automatically, converting between bytes and strings as needed.
Example
>>> writer = FileWriter("output.gcode") >>> writer.write(b"G1 X10 Y10\n") >>> writer.disconnect()
- connect()ο
Establish the connection to the output file.
- Return type:
- disconnect(wait=True)ο
Close the file if it was opened by this writer.
- Parameters:
wait (bool)
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- class gscrib.writers.LogWriterο
Bases:
BaseWriterWriter that outputs commands to Pythonβs logging system.
This class implements a G-code writer that logs G-code commands using Pythonβs logging framework.
Example
>>> writer = LogWriter() >>> writer.set_level("info") >>> writer.write(b"G1 X10 Y10\n")
- flush()ο
Flush the output buffer
- Return type:
None
- get_logger()ο
Get the logger used by this writer.
- Returns:
The logger used by this writer.
- Return type:
- set_level(level)ο
Set the logging level for the logger.
- class gscrib.writers.PrintrunWriter(mode, host, port, baudrate)ο
Bases:
BaseWriterWriter that sends commands through a serial or socket connection.
This class implements a G-code writer that connects to a device using printrun core.
- Parameters:
mode (DirectWrite | str)
host (str)
port (str)
baudrate (int)
- connect()ο
Establish the connection to the device.
- Returns:
Self for method chaining
- Return type:
- Raises:
DeviceConnectionError β If connection cannot be established
DeviceTimeoutError β If connection times out
- disconnect(wait=True)ο
Close the connection if it exists.
- Parameters:
wait (bool) β If True, waits for pending operations to complete
- Raises:
DeviceTimeoutError β If waiting times out
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- get_parameter(name)ο
Get the last reading for a parameter by name.
This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.
- set_timeout(timeout)ο
Set the timeout for waiting for device operations.
- Parameters:
timeout (float) β Timeout in seconds.
- Return type:
None
- write(statement)ο
Send a G-code statement through the device connection.
- Parameters:
statement (bytes) β The G-code statement to send
- Raises:
DeviceConnectionError β If connection cannot be established
DeviceTimeoutError β If connection times out
DeviceWriteError β If write failed
- Return type:
None
- class gscrib.writers.SerialWriter(port, baudrate)ο
Bases:
BaseWriterWriter that sends commands through a serial connection.
This class implements a G-code writer that connects to a device through a serial port and sends G-code commands to it.
Example
>>> writer = SerialWriter("/dev/ttyUSB0", 115200) >>> writer.write(b"G1 X10 Y10\n") >>> writer.disconnect()
- connect()ο
Establish the serial connection to the device.
Creates a printcore object with the configured port and baudrate, and waits for the connection to be established.
- Returns:
Self for method chaining
- Return type:
- Raises:
DeviceConnectionError β If connection cannot be established
DeviceTimeoutError β If connection times out
- disconnect(wait=True)ο
Close the serial connection if it exists.
- Parameters:
wait (bool) β If True, waits for pending operations to complete
- Raises:
DeviceTimeoutError β If waiting times out
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- get_parameter(name)ο
Get the last reading for a parameter by name.
This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.
- set_timeout(timeout)ο
Set the timeout for waiting for device operations.
- Parameters:
timeout (float) β Timeout in seconds.
- Return type:
None
- write(statement)ο
Send a G-code statement through the serial connection.
- Parameters:
statement (bytes) β The G-code statement to send
- Raises:
DeviceConnectionError β If connection cannot be established
- Return type:
None
- class gscrib.writers.SocketWriter(host, port)ο
Bases:
BaseWriterWriter that sends commands through a network socket connection.
This class implements a G-code writer that connects to a device through a TCP/IP socket and sends G-code commands to it.
Example
>>> writer = SocketWriter("localhost", 8000) >>> writer.write(b"G1 X10 Y10\n") >>> writer.disconnect()
- connect()ο
Establish the socket connection to the device.
Creates a printcore object with the configured host and port, and waits for the connection to be established.
- Returns:
Self for method chaining
- Return type:
- Raises:
DeviceConnectionError β If connection cannot be established
DeviceTimeoutError β If connection times out
- disconnect(wait=True)ο
Close the socket connection if it exists.
- Parameters:
wait (bool) β If True, waits for pending operations to complete
- Raises:
DeviceTimeoutError β If waiting times out
- Return type:
None
- flush()ο
Flush the output buffer
- Return type:
None
- get_parameter(name)ο
Get the last reading for a parameter by name.
This method retrieves the last reported value for a device parameter. These parameters are stored and updated each time the device reports a new value for them.
- set_timeout(timeout)ο
Set the timeout for waiting for device operations.
- Parameters:
timeout (float) β Timeout in seconds.
- Return type:
None
- write(statement)ο
Send a G-code statement through the socket connection.
- Parameters:
statement (bytes) β The G-code statement to send
- Raises:
DeviceConnectionError β If connection cannot be established
- Return type:
None