PHP Roomba Serial Port Communication

2.9k views Asked by At

I am trying to communicate with an iRobot Roomba through the serial port using the PHP class developed by Remy Sanchez. I am sure it is sending the data as the iRobot USB cable is receiving the data and lighting up, however, the Roomba doesn't seem to be acknowledging the commands as defined in the Roomba Serial Command Interface (SCI) Specification manual. Is there a possible reason for this? Does the class distort the data in some way or does the Roomba require a certain data type to be sent to it that PHP doesn't support?

Additional Information (I'm not sure if this is relevant)

Using RealTerm, I can communicate with the Roomba directly using the Send Numbers function (if I try to communicate any other way, it sends every keypress). Using PuTTY, the Roomba doesn't accept my commands, even though I can force local echo + line editing on. It receives the commands, but doesn't do anything with them even though the baud rate is configured correctly.

Code

require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("COM1");

$serial->confBaudRate(115200); //Baud rate: 115200
$serial->confParity("none");  //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1);  //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none"); //Device does not support flow control

$serial->deviceOpen();

$start = sprintf("%c",128);
$power = sprintf("%c",133);

$serial->sendMessage("$start");

$time_start = microtime(true);
// Sleep for a while
usleep(1000000);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds <br>";

$serial->sendMessage("$power");
$serial->deviceClose();
3

There are 3 answers

1
rik On

The result of -(pow(2, 8) - N) is an integer. PHP internally stores integer values as signed long.

Use pack()!

0
Bob On

Make sure that CR and/or LF is correctly being used after any commands. Some programs automatically send 1, both, or none....

0
roombahacker On

While using Putty your OS is Linux, right? So COM1 might be wrong. Try something like $serial->deviceSet("/dev/ttyAMA0"); And make sure your web-user (www-data?) is within the 'dialout' group.

But first of all get it working from the commandline (Bash):

Correct configuration of the serial port: sudo stty 115200 -F /dev/ttyAMA0 cs8 cread clocal

Send some testdata from bash (start passive mode): sudo echo -n -e "\x80" > /dev/ttyAMA0

Let the roomba clean: sudo echo -n -e "\x87" > /dev/ttyAMA0