EDIT: I have done some testing with the following script (found here: PHP serial port data return from Arduino). It appears that even using fwrite to /dev/ttyUSB0 outputs my new command plus the last command sent from the device. This makes me think that maybe the serial port was not set up correctly (e.g. might need to use stty to change settings to clear the buffer) or there is something else I need to do (with php) to clear the buffer at /dev/ttyUSB0 before sending a new command. Any help would be appreciated, thanks.
$fp =fopen("/dev/ttyUSB0", "w+");
if( !$fp) {
echo "Error";die();
}
fwrite($fp, $_SERVER['argv'][1] . 0x00);
echo fread($fp, 10) . "\n";
fclose($fp);
Original question:
I'm using php-serial to communicate with a device via serial port. The device has a function to display all input and output to the serial port on a screen. When I run the following script I can see on the device that the script is taking every command output by the device and repeating it back to the device. I believe this is causing an issue in that I just want to answer 'OK' back to the device, not repeat the output and then say 'OK'. Repeating the same command back first seems to confuse it and then it will not adhere the 'OK' command. I can get around this by outputting 'OK' every 0.1 seconds on a loop, but I would rather read the output from the device and then respond to it accordingly. I don't believe there is anything in my script telling it to do repeat the commands back. Is this an issue with the php-serial class that anyone has experienced? I tried setting $autoflush = false; with no difference.
My script:
require_once('php_serial.class.php');
$serial = new phpSerial;
$serial->deviceSet('/dev/ttyUSB0');
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
while (true) {
$read = $serial->readPort();
if ($read) {
echo date("r") . ' ' . $read;
// $serial->sendMessage("\r\nOK\r\n");
}
}
$serial->deviceClose();
Example output on the device screen before my script is run:
Tx: AT
Tx: AT&F
Tx: ATE0
Tx: AT+IPR=9600
Tx: AT
Tx: AT
Tx: AT
Tx: ATH0
Tx: AT+CMGF=1
Example output on the device screen while my script is running:
Tx: AT
Rx: AT
Tx: AT&F
Rx: AT&F
Tx: ATE0
Rx: ATE0
Tx: AT+IPR=9600
Rx: AT+IPR=9600
Tx: AT
Rx: AT
Tx: AT
Rx: AT
Tx: AT
Rx: AT
Tx: ATH0
Rx: ATH0
Tx: AT+CMGF=1
Rx: ^[AT+CMGF=1
install minicom and try before opening the port with:
$sms="ttyUSB0";
exec("minicom -b 9600 -o -D /dev/" . $sms);