Solved

PHP serial port

Posted on 2007-04-11
11
926 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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