Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP serial port

Posted on 2007-04-11
11
Medium Priority
?
944 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 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