Forum Moderators: coopster

Message Too Old, No Replies

Serial Port Comms with PHP

         

benshole

2:06 am on Jan 5, 2007 (gmt 0)

10+ Year Member



I am trying to communicate with a GSM/GPRS modem through PHP.
I can connect to it fine, i can issue commands,
Eg.
AT+CMGL="ALL"

That would list all the SMS messages on the device.
However, when calling the result i cant find a way to parse through the stream until the end and then STOP. I am currently looping using fgets but once it gets to the end of the response from the module it just sits there. (i presume waiting for more data!)


$ser = fopen("/dev/ttyS0","r+");
stream_set_timeout($ser, 4);
if (!$ser){
echo "port error";
}

fputs($ser,'AT+CMGL="ALL"' . "\n\r");

while (!feof($ser)) {
$buffer = fgets($ser);
echo $buffer;
}
echo "end";

Any Ideas?

Thanks,

Ben

Tastatura

2:30 am on Jan 5, 2007 (gmt 0)

10+ Year Member



What you are asking can be tricky but not necessearly hard depending on the device’s interface functionality.
I don’t know specs for the modem you are connecting to but see if there is a command that tells you something about how much data is available to retrieve. Some devices have that some don’t.
Alternately you could request predetermined data size and see how much you get back. Say you requested 32 bytes and get back 32 bytes, That would be indication to go get more. At some point you will request 32 and get back less then that. That can be indication that you reached the end of data or that there is an error in communication (so you would have to have solid error handling function). Actually at this point it would be very helpful if the device throws specific error a la “ you are asking me to give you X amount of data but I can’t comply because I only have Y amount” (where X>Y). That would be confirmation that you got all of the data. Assuming that you got all of data you can cleanly break communication or ask device to do something else, instead of just sitting there doing nothing as you said.
Incoming data can be buffered and then parsed or you can try to process it in real time, depending on what you are doing….

Out of curiosity, why are you doing this in PHP? Can you use different language for this and write wrapper for it? I never did this stuff in PHP; I would usually prototype in LabView and then convert to whatever language is needed, or keep using LabView code and write wrapper around it.