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
LVL 5
skaap2kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
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.
0
skaap2kAuthor Commented:
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?
0
TeRReFCommented:
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
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

skaap2kAuthor Commented:
It's CLI - i'm having a look there right this minute! I think you're onto something! hah :)
0
skaap2kAuthor Commented:
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?
0
TeRReFCommented:
I usually use a while loop to keep things going. Realize that the child process starts of, where the
$pid = pcntl_fork(); appears, so both parent and child will continue there...

$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // we are the parent
     pcntl_waitpid(0, $status, WNOHANG); // enables parent to continue doing other stuff
} else {
     // we are the child
     // put your serial code here...
     exit(); // make sure that the child terminates after completing it's task.
}
while (true) {
// here is where the parent does it's other stuff
}


To share data you can use a file or you can use the shared memory function I provided. If you want to use a file, look into a memory disk partition (if you are using a *NIX OS) It will be blazingly fast. If you want to look into the shared memory option, check the examples here:
http://nl3.php.net/manual/en/ref.shmop.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hernst42Commented:
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.
0
skaap2kAuthor Commented:
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!

0
TeRReFCommented:
Nice one!
0
skaap2kAuthor Commented:
you're a backwards FeRReT arent ya?! hah. thanks again :)
0
TecsolAusCommented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.