0

I am using a Raspberry Pi to read real time RS232 data via a USB port using a Prolific 2303 adaptor.

I want to parse this and display it in a web page. My first approach was to use a python script. This could read and parse the data but passing it to a PHP web page proved a problem. Either I could use GET to send it or save it to a file and read it with PHP. The latter is a non starter with an SD card, it's not going to last too long and the former might involve file storage anyway.

I then tried a PHP approach;

$device = "/dev/ttyUSB0";
$fp = fopen($device,"r") or die();
while(true){
  $xml = fread($fp,"400");
    print_r($xml);
}
fclose($fp);

print_r(); produces:

CC128-v0.110011909:27:3016.70000951000660008500024 CC128-v0.110011909:27:3616.70000951000670008600024 CC128-v0.110011909:27:4216.70000951000680008700027 CC128-v0.110011909:27:4816.70000951000680008600024 CC128-v0.110011909:27:5516.70000951000680008800024

This is 5 bursts of XML stripped of all its tags. The complete XML stream is 340 characters long, 57600-N-8-1 sent every 6 seconds.

Then it crashes with "Fatal error: Maximum execution time of 30 seconds exceeded in ~/meter.php on line 79" Sometimes there is missing or corrupted data, too.

If I use $xml = fgets($fp); I get no data.

The stream I am expecting, as read using a Python is:

['<msg><src>CC128-v0.11</src><dsb>00114</dsb><time>17:45:02</time><tmpr>16.8</tmpr><sensor>0</sensor><id>00095</id><type>1</type><ch1><watts>00065</watts></ch1><ch2><watts>00093</watts></ch2><ch3><watts>00024</watts></ch3></msg>\r\n']

I tried to use PECL-DIO but could not locate all the dependencies. Apparently, it is deprecated for the current version of Debian and will be removed from the next. I don't know if that refers to Buster, Bullseye or Bookworm. I also tried using php_serial.class which I found on GIT but could not get a complete XML file output or even the stripped down data only stream.

What am I missing to get a PHP variable updated every 6 seconds?

Sabreur
  • 21
  • 7
  • Use AJAX to send requests to a php file every X seconds. The php file reads/returns the data, use Javascript to display it on your webpage – brombeer Oct 30 '21 at 18:30
  • You can [increase PHP’s time limit](https://stackoverflow.com/a/5164954/231316). However, how much data do you care about. Is it only “now”, or do you want historical? – Chris Haas Oct 31 '21 at 00:35
  • I guess you have spotted that this is a Current Cost Monitor. The historic data will go to the database, once I can read it. If I could detect the end of the data message I can exit the 'while loop' and restart it for the next message so the timeout would not be a problem. It seems that the 'fread()' is running much slower than the data but that still does not explain why it doesn't stop after the 400 bytes or is the limitation 'fopen()'? – Sabreur Oct 31 '21 at 09:25
  • `fread` returns false once it reaches the end of the stream, have you tried checking for that? – Chris Haas Oct 31 '21 at 12:03

0 Answers0