Solved

PHP serial port

Posted on 2007-04-11
11
906 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:skaap2k
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 18887876
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
 
LVL 5

Author Comment

by:skaap2k
ID: 18887902
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
 
LVL 29

Expert Comment

by:TeRReF
ID: 18888015
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
 
LVL 5

Author Comment

by:skaap2k
ID: 18888022
It's CLI - i'm having a look there right this minute! I think you're onto something! hah :)
0
 
LVL 5

Author Comment

by:skaap2k
ID: 18888068
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 18888119
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
 
LVL 48

Expert Comment

by:hernst42
ID: 18888122
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
 
LVL 5

Author Comment

by:skaap2k
ID: 18888308
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
 
LVL 29

Expert Comment

by:TeRReF
ID: 18888315
Nice one!
0
 
LVL 5

Author Comment

by:skaap2k
ID: 18888325
you're a backwards FeRReT arent ya?! hah. thanks again :)
0
 

Expert Comment

by:TecsolAus
ID: 32968140
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now