PHP - Reading COM Port from Windows

The following is a library for serial communication through PHP: http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html . The problem is that the readPort method is not fully implemented. It can read in * nix, but apparently not in Windows. Method:

/** * Reads the port until no new datas are availible, then return the content. * * @pararm int $count number of characters to be read (will stop before * if less characters are in the buffer) * @return string */ function readPort ($count = 0) { if ($this->_dState !== SERIAL_DEVICE_OPENED) { trigger_error("Device must be opened to read it", E_USER_WARNING); return false; } if ($this->_os === "linux") { $content = ""; $i = 0; if ($count !== 0) { do { if ($i > $count) $content .= fread($this->_dHandle, ($count - $i)); else $content .= fread($this->_dHandle, 128); } while (($i += 128) === strlen($content)); } else { do { $content .= fread($this->_dHandle, 128); } while (($i += 128) === strlen($content)); } return $content; } elseif ($this->_os === "windows") { /* Do nohting : not implented yet */ } trigger_error("Reading serial port is not implemented for Windows", E_USER_WARNING); return false; } 

The author declares:

==> /! \ WARNING /! \: it works with linux for r / w, but with windows I could only write work. If you are a windows user, try accessing the serial port over the network using serproxy instead.

The limitation of the PHP class library has already been mentioned in SO several times. I did not find a decent solution. Reading is a must for my application.

Does anyone know what to do?

+6
source share
2 answers

If you can guarantee that you are on Windows, I could recommend an interesting approach: use COM (as in Microsoft COM, not in the serial port) or .NET.

There, the free .NET class that I regularly use is called CommStudio Express . I found it very reliable, but you can always use the standard SerialPort class built into .NET if you don't have to worry about accidentally disconnecting the USB-to-serial adapter .

In any case, it’s easy enough to get in a PHP .NET class with the DOTNET class :

 $serial = new DOTNET('system', 'System.IO.Ports.SerialPort'); $serial->PortName = 'COM3'; $serial->Open(); 

I have not tested this code (and not on Windows at the moment), but something like this should work fine. Then you can use all the usual .NET methods in PHP.

+3
source

No, DOTNET freezes the COM port. You need to either restart the computer so that it detects again, or advanced users, as a rule, remove and install Arduino.

0
source

Source: https://habr.com/ru/post/909144/


All Articles