# How do you send multiple data bytes to Irobot Roomba via python serial

So I'm trying to send commands to my Roomba Discovery via python serial and it works fine as long as it only requires the command opcode. The commands that require additional data bytes (like specific movement or music) don't seem to be working. It's an old roomba, however, I do not believe that is the issue as I am able to use Java based applications like RoombaMidi2 from hackingroomba.com. The code below works fine to have the roomba clean for 5 seconds then sleep:

import serial
import time
import struct

ser = serial.Serial("/dev/ttyUSB0", 57600)

ser.write(bytes([128])) # 128: start command
ser.write(bytes([131])) # 131: Safe Mode
time.sleep (0.25)

ser.write(bytes([135])) # 135: clean command
time.sleep (5)

ser.write(bytes([133])) # 133: puts robot to sleep
ser.close()


But if I want to do a move command it doesn't seem to work. I've tried:

ser.write(bytes([137]))
ser.write(bytearray([255, 56]))
ser.write(bytearray([1, 244]))


No error, but it doesn't do anything. I've also tried sending it as 1 byte array:

ser.write(bytearray([137, 255, 56, 1, 244]))


Any idea on what I'm doing wrong?

I'm not sure if/how pyserial works with a bytearray, but the way it is typically done is with the struct library. Here is some sample code:

import serial
import struct

ser = serial.Serial("/dev/ttyUSB0", 57600)

# compute left and right wheel velocities
right_vel = int(velocity + (rotation/2))
left_vel = int(velocity - (rotation/2))

cmd = struct.pack(">Bhh", 145, right_vel, left_vel)

ser.write(cmd)
ser.flush()


I know the ">Bhh" thing can look scary, but it is simply specifying the format of the bytes to transmit over the wire. Here, you are saying this:

• > The bytes will be in big-endian format
• B The first thing transmitted (145) is an "unsigned char"
• hh The next two things transmitted are "signed short integers". Note that these are 2 bytes long each.

See this tutorial for more info on using Python to control Create2: Python Tethered Driving for Create 2

• Hey, thank you. I wasn't expecting anyone to answer. The example provided doesn't seem to work (changed 145 to 137), no error again, but the roomba doesn't seem to react. But in any case you have given me a lead. Mar 31 at 5:11

I'm not sure if this helps or not, but Discovery models manufactured before November 2005 do not contain the entirety of the SCI set. For more information on this, see the first paragraph of the SCI specification for Discovery Series. It is possible that your robot is one of the unlucky ones. Unfortunately, I'm not aware that the OSMO upgrade modules for these robots are still available (I am 99.9% confident iRobot no longer makes or stocks them). It is possible that your problem is not a Python one but a compatibility one. What's the manufacture date on your robot?

Please note that I am an iRobot employee, but the postings on this site are my own and don't necessarily represent iRobot's positions, strategies, or opinions.