Connection

pygmc.connect(port=None, baudrate=None, timeout=5)[source]

Connect to device.

If all parameters are None, _auto_connect() flow is used which attempts to connect to all available ports. If ANY parameter is given; it’s used to refine the search, any matches are considered. Parameters are used as an OR search.

Parameters:
  • port (str | None, optional) – Exact port (device dev path / com port) e.g. ‘/dev/ttyUSB0’ If port is specified, the following kwargs are ignored: vid, pid, description, hardware_id.

  • baudrate (int | None) – Device baudrate. Leave None to auto-detect baudrate. Only applicable when port is specified.

  • timeout (int) – Time limit for pyserial to raise timeout.

Raises:

ConnectionError – Unable to connect to device.

class pygmc.connection.Connection(port, baudrate, timeout=5, serial_connection=None)[source]

Bases: object

Represent a connection to a GMC device.

A wrapper around pyserial with common operations for a GMC device.

close_connection() None[source]

Close connection.

get(cmd, wait_sleep=0.3) bytes[source]

Write command to device and get response.

Only use in development/learning environment. May give incomplete/empty response if device is busy.

Parameters:
  • cmd (bytes) – Write command e.g. <GETVER>>

  • wait_sleep (float, optional) – Time to sleep to give device time to write, by default 0.3

Returns:

Device response

Return type:

bytes

get_at_least(cmd: bytes, size: int, wait_sleep=0.05) bytes[source]

Write cmd, read at least <size> bytes then wait <wait_sleep> and read the buffer.

Parameters:
  • cmd (bytes) – Write command e.g. <GETVER>>

  • size (int) – Minimum size expected to read or timeout.

  • wait_sleep (float, optional) – Time to sleep (seconds) to give device time to write, by default 0.05

Notes

The reason for this method is due to the unspecified expected length in GETVER. We write the command then we must wait for the device to write the response. For GETVER on GMC300S: .get() wait=0.1s, two-hundred loops took 20.4 seconds and failed 13% .get_at_least() wait=0.05s, two-hundred loops took 15.3 seconds and 0% failed.

Returns:

Device response

Return type:

bytes

get_connection_details() dict[source]

Get connection details.

Values of None means not available or not applicable.

Return type:

dict

get_exact(cmd, size=None, expected=b'') bytes[source]

Write and read exact.

Write command to device, provide expected LF or size (bytes), wait until either LF, size, or timeout is reached, then return device response.

Parameters:
  • cmd (bytes) – Write command e.g. <GETVER>>

  • size (int | None, optional) – Expected response size, by default None

  • expected (bytes, optional) – Expected end char, by default b’’

Returns:

Device response

Return type:

bytes

read(wait_sleep=0.3) bytes[source]

Read all available data.

Which may be incomplete. (noob/newbie method)

Parameters:

wait_sleep (float, optional) – Time to sleep to give device time to write, by default 0.3

Returns:

Device response

Return type:

bytes

read_at_least(size, wait_sleep=0.05) bytes[source]

Read at least <size> bytes then wait <wait_sleep> and read the buffer.

i.e. Wait as long as needed to get at-least <size> bytes then wait <wait_sleep> seconds and read whatever else is ready in the buffer.

Parameters:
  • size (int) – Minimum size expected to read or timeout.

  • wait_sleep (float | int) – Time to wait in seconds to check if there’s anything remaining in the buffer.

Notes

This method resets the input & output buffers after; incase there was extra info that would’ve been added to the buffers. This is useful for ill-defined specs where there is no exact size prescribed and not waiting enough may result in empty/partial response and waiting too long is wasteful if the response was ready quickly.

Return type:

bytes

read_until(size=None, expected=b'') bytes[source]

Read device data until expected LF is reached or expected result size is reached.

Waits until conditions met or timeout. Some data has ‘n’ which causes reading to stop. default changed from b’n’ to b’’

Parameters:
  • size (None | int, optional) – Length of expected bytes, by default None

  • expected (bytes, optional) – Expected end character, by default b’’

Returns:

Device response

Return type:

bytes

reset_buffers() None[source]

Reset input & output buffers on pyserial connection.

reset_input_buffer(): Clear input buffer, discarding all that is in the buffer. reset_output_buffer(): Clear output buffer, aborting the current output and discarding all that is in the buffer.

write(cmd: bytes, log: bool = True) None[source]

Write command to device.

Parameters:
  • cmd (bytes) – Write command e.g. <GETVER>>

  • log (bool) – Default=True to log cmd at debug level. Set false when writing sensitive information such as WiFi password.

Discover GMC devices.

pygmc.connection.discovery.DeviceDetails

alias of Device

class pygmc.connection.discovery.Discovery(port=None, baudrate=None, timeout=3)[source]

Bases: object

Discover GMC Devices

get_all_devices() list[source]

Get all discovered devices.

Returns:

List of all discovered devices.

Return type:

list

get_device_by_serial_number(serial_number) list[source]

Get devices by matching serial number.

May return more than one device if sym-links are used.

Parameters:

serial_number (str) – Device serial number.

Returns:

List of matching devices.

Return type:

list

get_device_by_version(version) list[source]

Get devices by matching version.

Parameters:

version (str) – Device version regex. e.g. ‘GMC-5’ will match any device starting with GMC-5.

Returns:

List of matching devices.

Return type:

list

pygmc.connection.utils.get_all_usb_devices(regexp=None, include_links=True) list[source]

Get all available USB devices.

Parameters:
  • regexp (None | str, optional) – Search for ports using a regular expression. Port name, description and hardware ID are searched. hardwareID example (‘USB VID:PID=1A86:7523 LOCATION=2-1’) Default=None, find all.

  • include_links (bool, optional) – include symlinks under /dev when they point to a serial port, by default True

Returns:

available ports, type [serial.tools.list_ports_linux.SysFS]

Return type:

list

pygmc.connection.utils.get_gmc_usb_devices(vid='1A86', pid='7523', description='.*', include_links=True) list[source]

Get a list of GMC USB devices.

Default parameters are best known parameters that matches GMC USB devices.

Parameters:
  • vid (str | int) – Hexadecimal string or int to exact match the Vendor-ID vid. e.g. ‘0x1A86’ or ‘1A86’ or 6790 will all match.

  • pid (str | int) – Hexadecimal string or int to exact match the Product-ID pid. e.g. ‘0x7523’ or ‘7523’ or 29987 will all match. (Note ‘7523’ is considered a hex as a string)

  • description (str) – Regex to match usb device description. (case-insensitive flag used) Known descriptions: ‘USB2.0-Serial’ & ‘USB-Serial’

  • include_links (bool) – Include symlinks under /dev when they point to a serial port, by default True

Returns:

List of GMC devices.

Return type:

list