Polo-BOO! Halloween Sale going on now – click for details!

15.7. Example serial code in Python

The example Python code below uses the pySerial library to communicate with the Jrk G2 via serial. It demonstrates how to set the target of the Jrk by sending a “Set target” command and how to read variables using a “Get variables” command.

The Jrk’s input mode should be set to “Serial / I2C / USB”, or else the “Set target” command will not work. Also, you might need to change the line that sets port_name in order to specify the correct serial port.

If the Jrk is connected to your PC via USB, you will need to set the Jrk’s serial mode to “USB dual port” in the “Input” tab of the Jrk G2 Configuration Utility. The baud rate specified in this code and in the Jrk’s settings do not have to match because the serial bytes are transferred via USB. Also, instead of using this code, you might consider running the Jrk G2 Command-line Utility (jrk2cmd), which uses the native USB interface, since it can take care of all of the low-level details of communication for you (see Section 15.3).

If the Jrk is connected via its RX and TX lines, you will need to set the Jrk’s serial mode to “UART” and select the baud rate you want to use in the “Input” tab of the Jrk G2 Configuration Utility. The baud rate in the code should match the baud rate specified in the configuration utility.

If you run the code and get the error “ImportError: No module named serial” or “ModuleNotFoundError: No module named ‘serial’”, it means that the pySerial library is not installed, and you should follow the instructions in the pySerial documentation to install it.

# Uses the pySerial library to send and receive data from a Jrk G2.
#
# NOTE: The Jrk's input mode must be "Serial / I2C / USB".
# NOTE: You might need to change the "port_name =" line below to specify the
#   right serial port.

import serial

class JrkG2Serial(object):
  def __init__(self, port, device_number=None):
    self.port = port
    self.device_number = device_number

  def send_command(self, cmd, *data_bytes):
    if self.device_number == None:
      header = [cmd]  # Compact protocol
    else:
      header = [0xAA, device_number, cmd & 0x7F]  # Pololu protocol
    self.port.write(bytes(header + list(data_bytes)))

  # Sets the target.  For more information about what this command does,
  # see the "Set Target" command in the "Command reference" section of
  # the Jrk G2 user's guide.
  def set_target(self, target):
    self.send_command(0xC0 + (target & 0x1F), (target >> 5) & 0x7F)

  # Gets one or more variables from the Jrk (without clearing them).
  def get_variables(self, offset, length):
    self.send_command(0xE5, offset, length)
    result = self.port.read(length)
    if len(result) != length:
      raise RuntimeError("Expected to read {} bytes, got {}."
        .format(length, len(result)))
    return bytearray(result)

  # Gets the Target variable from the Jrk.
  def get_target(self):
    b = self.get_variables(0x02, 2)
    return b[0] + 256 * b[1]

  # Gets the Feedback variable from the Jrk.
  def get_feedback(self):
    b = self.get_variables(0x04, 2)
    return b[0] + 256 * b[1]

# Choose the serial port name.  If the Jrk is connected directly via USB,
# you can run "jrk2cmd --cmd-port" to get the right name to use here.
# Linux USB example:  "/dev/ttyACM0"
# macOS USB example:  "/dev/cu.usbmodem001234562"
# Windows example:    "COM6"
port_name = "/dev/ttyACM0"

# Choose the baud rate (bits per second).  This does not matter if you are
# connecting to the Jrk over USB.  If you are connecting via the TX and RX
# lines, this should match the baud rate in the Jrk's serial settings.
baud_rate = 9600

# Change this to a number between 0 and 127 that matches the device number of
# your Jrk if there are multiple serial devices on the line and you want to
# use the Pololu Protocol.
device_number = None

port = serial.Serial(port_name, baud_rate, timeout=0.1, write_timeout=0.1)

jrk = JrkG2Serial(port, device_number)

feedback = jrk.get_feedback()
print("Feedback is {}.".format(feedback))

target = jrk.get_target()
print("Target is {}.".format(target))

new_target = 2248 if target < 2048 else 1848
print("Setting target to {}.".format(new_target))
jrk.set_target(new_target)

Related Products

Jrk G2 18v19 USB Motor Controller with Feedback
Jrk G2 24v13 USB Motor Controller with Feedback
Jrk G2 18v27 USB Motor Controller with Feedback
Jrk G2 24v21 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback (Connectors Soldered)
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors