skaap2k
asked on
PHP serial port
While reading data from a serial port, it seems to me that it never shows a EOF (which makes sense I suppose), however, while I am reading from the serial port, unless there is actual data to read, the script gets stuck waiting for data, is there anyway I can check the buffer? or some other method I can use so that I can continue to do other tasks and then go looking at the serial port when there is data there ...
The script is in PHP.. here is a snippet:
$fp=fopen("/dev/ttyS0", "r+");
while (!feof($fp) && !$ok) {
$input=fgets($fp); // We get stuck here!!
print "I got to here.. \n"; // quick output to see if the script arrives here
}
fclose($fp);
Any help appreciated! :)
RN
The script is in PHP.. here is a snippet:
$fp=fopen("/dev/ttyS0", "r+");
while (!feof($fp) && !$ok) {
$input=fgets($fp); // We get stuck here!!
print "I got to here.. \n"; // quick output to see if the script arrives here
}
fclose($fp);
Any help appreciated! :)
RN
Isn't there a marker in your collected data where you can recognize the end of the read stream? Adding a pseudo threading for such io-operation is not easy. But you also need to recognize the end, so if there are no other things to do read the stream till the end, then execute the function which processes all the content.
ASKER
It's attached to a modem, so theoretically, after every line there is a OK ... handling things when I actually get data is fine, and I can do other tasks after I receive data - however - it's when there is no data for a prolonged period of time, I would like to get the script to do other things..
Perhaps it's not possible?
Perhaps it's not possible?
Is it a CLI or web based script? If it's CLI I suggest you fork the process that listens to the serial port and share the info with the parent process. It releases the parent process so it enables it to do other stuff in the meantime.
Have a look at these functions:
http://php.net/pcntl
http://php.net/manual/en/ref.sem.php
Have a look at these functions:
http://php.net/pcntl
http://php.net/manual/en/ref.sem.php
ASKER
It's CLI - i'm having a look there right this minute! I think you're onto something! hah :)
ASKER
Ok, so I've read through some of the manual stuff about forking etc:
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
}
So, my idea is to put the fgets into the child pid and deal with data from there - now, for the other tasks i want to do, where do I put that code? and can I kill a childpid (I would need to stop listening on the serial port to perform these other tasks). how would i share data between the parent & child?
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
}
So, my idea is to put the fgets into the child pid and deal with data from there - now, for the other tasks i want to do, where do I put that code? and can I kill a childpid (I would need to stop listening on the serial port to perform these other tasks). how would i share data between the parent & child?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Using php for this task is not the best language for this. Better use java for such things as java has build in classes/mechanism for such tasks.
o share information between the processes use http://php.net/manual/en/ref.sem.php - function as TerRRef had written.
o share information between the processes use http://php.net/manual/en/ref.sem.php - function as TerRRef had written.
ASKER
Thnx very much for your comments - however, I've managed to do this without using fork,
I installed a sighandler, and used SIGALRM to send other commands to the modem which will give it the requred output to do my other tasks!
I installed a sighandler, and used SIGALRM to send other commands to the modem which will give it the requred output to do my other tasks!
Nice one!
ASKER
you're a backwards FeRReT arent ya?! hah. thanks again :)
I'm working through exactly the same issue as described by skaap2k.
I am using a Linux (Ubuntu) server and PHP as a web server. The solution offered appears to be for a command line version of PHP, and doesn't work in my web server.
In short, I need to read and write to a GSM modem via the serial port using php. Writing is no problem. Reading using fread() stalls the server until the required number of bytes and a CR is received. I need to return from checking the serial port with a null if there is no new data. I'm happy to pick a byte at a time from the serial buffer and assemble it into a string in php.
Any suggestions welcome!
I am using a Linux (Ubuntu) server and PHP as a web server. The solution offered appears to be for a command line version of PHP, and doesn't work in my web server.
In short, I need to read and write to a GSM modem via the serial port using php. Writing is no problem. Reading using fread() stalls the server until the required number of bytes and a CR is received. I need to return from checking the serial port with a null if there is no new data. I'm happy to pick a byte at a time from the serial buffer and assemble it into a string in php.
Any suggestions welcome!