Reading the Ardunio serial monitor with the ZA6 in a robot program- troubleshooting issues

I am attempting to reading the serial monitor from an Arduino in a robot program code but I am hitting some roadblocks. I have installed pyserial but when running the program I am getting the error message of " No module named serial."

Below is my code. If anyone has more experience with this I’d appreciate the help!

from robot_command.rpl import *

import serial

port = ‘/dev/ttyACM0’ # Replace ‘COMX’ with the actual port name (e.g., ‘COM3’ on Windows or ‘/dev/ttyUSB0’ on Linux)
baud_rate = 9600 # Must match the Arduino’s Serial.begin() baud rate

set_units(“mm”, “deg”, “second”)
waypoint_1 = j[6.591, 32.873, 0.577, 19.443, -33.605, -63.230]
waypoint_2 = j[-17.420, 32.873, 0.577, 92.895, 92.442, -63.230]
waypoint_3 = j[-17.420, 67.227, -0.380, 91.062, 93.632, -96.656]
waypoint_4 = j[-17.420, 73.047, -3.811, 90.906, 93.675, -99.052]
waypoint_5 = j[-17.419, 75.705, -5.615, 90.851, 93.678, -100.014]
waypoint_6 = j[-17.420, 73.047, -3.811, 90.907, 93.675, -99.052]
waypoint_7 = j[-17.420, 67.227, -0.380, 91.061, 93.631, -96.656]
waypoint_8 = j[16.076, 66.903, 0.642, 122.268, 81.529, -100.972]
waypoint_9 = j[56.712, 70.228, -9.262, 124.145, 76.801, -96.121]
waypoint_10 = j[56.715, 78.826, -14.924, 124.623, 78.508, -98.641]
waypoint_11 = j[56.712, 70.228, -9.261, 124.145, 76.801, -96.121]

def main():

ser = serial.Serial(port, baud_rate)
movej(waypoint_1)

set_digital_out("Vacuum_Table", False)
while True:
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()  # Read a line from the serial monitor and decode it
        value = int(line)  # Convert the line to an integer

    print(f"Received value: {value}")

    if value > 80:
        # Perform your task here when the value is above 80
        print("Performing task...")
        # Add your code to execute the task

        sleep(2.0)
        set_digital_out("suction_gripper", False)
        movej(waypoint_2)
        movej(waypoint_3)
        movej(waypoint_4)
        movej(waypoint_5)
        set_digital_out("suction_gripper", True)
        sleep(0.5)
        movej(waypoint_6)
        movej(waypoint_7)
        movej(waypoint_8)
        movej(waypoint_9)
        movej(waypoint_10)
        set_digital_out("suction_gripper", False)
        movej(waypoint_11)
        set_digital_out("Vacuum_Table", True)
        sleep(5.0)
        movej(waypoint_1)
1 Like

The workaround that I use for this specific issue is to install the library from code if it isn’t found. E.g.

try:
    import serial
except:
    import sh
    sh.sudo.pip3.install("pyserial")
    import serial
1 Like

Great workaround! Thanks for your help!

The next hurdle in the process I’m running into: I am getting an error on line 37: invalid literal for int() with base 10:

Below is the updated code with @Shane 's fix for the import serial:

I am getting an error message for line 37: invalid literal for int() with base 10. The code is the following: from robot_command.rpl import *

try:
import serial
except:
import sh
sh.sudo.pip3.install(“pyserial”)
import serial

port = ‘/dev/ttyACM0’ # Replace ‘COMX’ with the actual port name (e.g., ‘COM3’ on Windows or ‘/dev/ttyUSB0’ on Linux)
baud_rate = 9600 # Must match the Arduino’s Serial.begin() baud rate

set_units(“mm”, “deg”, “second”)
waypoint_1 = j[6.591, 32.873, 0.577, 19.443, -33.605, -63.230]
waypoint_2 = j[-17.420, 32.873, 0.577, 92.895, 92.442, -63.230]
waypoint_3 = j[-17.420, 67.227, -0.380, 91.062, 93.632, -96.656]
waypoint_4 = j[-17.420, 73.047, -3.811, 90.906, 93.675, -99.052]
waypoint_5 = j[-17.419, 75.705, -5.615, 90.851, 93.678, -100.014]
waypoint_6 = j[-17.420, 73.047, -3.811, 90.907, 93.675, -99.052]
waypoint_7 = j[-17.420, 67.227, -0.380, 91.061, 93.631, -96.656]
waypoint_8 = j[16.076, 66.903, 0.642, 122.268, 81.529, -100.972]
waypoint_9 = j[56.712, 70.228, -9.262, 124.145, 76.801, -96.121]
waypoint_10 = j[56.715, 78.826, -14.924, 124.623, 78.508, -98.641]
waypoint_11 = j[56.712, 70.228, -9.261, 124.145, 76.801, -96.121]

def main():

ser = serial.Serial(port, baud_rate)
movej(waypoint_1)

set_digital_out("Vacuum_Table", False)
while True:
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()  # Read a line from the serial monitor and decode it
        value = int(line)  # Convert the line to an integer

    print(f"Received value: {value}")

    if value > 80:
        # Perform your task here when the value is above 80
        print("Performing task...")
        # Add your code to execute the task

        sleep(2.0)
        set_digital_out("suction_gripper", False)
        movej(waypoint_2)
        movej(waypoint_3)
        movej(waypoint_4)
        movej(waypoint_5)
        set_digital_out("suction_gripper", True)
        sleep(0.5)
        movej(waypoint_6)
        movej(waypoint_7)
        movej(waypoint_8)
        movej(waypoint_9)
        movej(waypoint_10)
        set_digital_out("suction_gripper", False)
        movej(waypoint_11)
        set_digital_out("Vacuum_Table", True)
        sleep(5.0)
        movej(waypoint_1)

That’s hard to say when we don’t know what the connected device is sending. Obviously not a something which can be converted into an integer. Could you look if there are not some excess character in the payload?