Serial COM port access in PHP
a workaround to access COM10 and above
... is not as easy as it should be. Each solution has its limitations. Here are some ideas...
fopen
With fopen/fread/fgets/fputs you can treat the serial port as a file$device = "COM8";
exec("mode $device BAUD=115200 PARITY=n DATA=8 STOP=1 xon=off octs=off rts=on");
$comport = fopen($device, "wb+");
fputs($comport,'test'.chr(22).chr(252));
fclose($comport);
Unfortunately COM10 and above do not work with Windows.
The php serial extension by thebyteworks
Download here is a commercial solution working that also copes with COM10+. Installation is a bit difficult though: You must modify the httpd.conf of the Apache server and switch to PHP-CGI mode. The required extension dll is dependent on the PHP version (only tested up to 5.3.0). The trial version has several seconds of delay or additional output "TRIAL". Full functionality costs about $30.shell_exec and set (DOS command)
$x=shell_exec('set /p x="hello" $lt;nul $gt>\\\\.\\COM11');
Will transfer "hello" to COM11. Unfortunately this is limited to ASCII text characters.
shell_exec and copy (DOS command)
First you create the binary file, then transfer the file to COM11. Be aware that you need to set data & parity first. 8 bits of data are needed for binary safe files!$x=shell_exec('mode COM11 PARITY=N data=8 stop=1 xon=off');
$x=shell_exec('copy test.bin \\\\.\\COM11');
But this was unreliant too.
That's why I've written and compiled a univeral tool for serial port access that can be called by PHP via the shell extension.
my own C++ workaround
Structure: comxs.exe (string port, no of bytes to read as hex, command as hex values) Example from shell:comxs.exe COM11 10 FE FE 00 E0 03 FD
will write the sequence (fe fe 00 e0 03 fd) to COM11 and then listen until 10 bytes are received.
In PHP you can call the tool like:
$x=shell_exec("comxs.exe $comport $readbytes $command");
$x will contain (as a written string):
"fe fe 00 e0 03 fd fe fe e0 00 03 66 10 20 14 00 fd"
(This is a CI-V dialogue for the Perseus SDR communications receiver)
Download comxs.zip
This is the C++ source:
It's very basic and still beta. There is no timeout - the process will be active until the requested number of bytes have been received. Use 0 for write-only.