0
votes

I'm having an issue with my PHP website (which is using an API, that's why it has to be PHP).

This website is launched on a raspberry pi b+ which is connected to a thermal printer (through serial port), I used a python script to test the printer.

Now my question is: Is it possible to send data through the web to make the raspberry print some data ? So send an instruction like write to the port '/dev/ttyxxx' client sided?

Thanks for your help

2

2 Answers

0
votes

If you mean: "I have a PHP application that needs to access the server's serial port": It is possible for PHP to access the serial port on the server (in this case, your raspberry pi). PHP treats it like it is a normal file.

From the PHP Fopen page:

<?php
// Set timeout to 500 ms
$timeout=microtime(true)+0.5;

// Set device controle options (See man page for stty)
exec("/bin/stty -F /dev/ttyS0 19200 sane raw cs8 hupcl cread clocal -echo -onlcr ");

// Open serial port
$fp=fopen("/dev/ttyS0","c+");
if(!$fp) die("Can't open device");

// Set blocking mode for writing
stream_set_blocking($fp,1);
fwrite($fp,"foo\n");

// Set non blocking mode for reading
stream_set_blocking($fp,0);
do{
  // Try to read one character from the device
  $c=fgetc($fp);

  // Wait for data to arive 
  if($c === false){
      usleep(50000);
      continue;
  }  

  $line.=$c;

}while($c!="\n" && microtime(true)<$timeout); 

echo "Responce: $line";  
?>

If you mean: "I have a website that somehow needs to send something to the client's serial port" Then the only solution is a browser App.

There's the Chrome Serial API which chrome apps can use. Video Example

0
votes

I come to think of several solutions; basically you would want your php-page to parse the data and create a trusted output that can be printed (i.e. a PDF-file, if your printer supports this).

Your next task is how to have this trusted output sent to the printer. Again, several solutions exists.

  1. Have your php-script execute a system executable, e.g. cat output.pdf > /dev/ttyxxx (it is clear here, that I do not know how to print from unix). Notice that the executable is not dependent on input at all, i.e. you want to reduce the risk of injection attacks and the like. This point requires that the output.pdf, that you created, is trustworthy.

  2. Have a cron-job look for output files and send them to the printer. Same considerations as above apply. This might be better as it can avoid bottlenecks if multiple php-sessions are trying to print a document.

  3. Build a smaller framework around the above that can report back if errors occur etc. But still, basically option 1 + magic.

All in all, split the process into two steps. One that accepts the input, parses and checks for erroneousness/malevolent input, and creates the needed output for the printer. This can be done in a protected environment, which if hacked, does not expose the system (at least not more than the usual php would). Step 2 then takes care of sending the output to the hardware, either bash-script, executable, or python.