I am trying to connect weighting machine with PHP.
I have tested with hyperterminal it is working fine.
But when I use PHP code with fopen
or dio_open
it is executed but when fgets
or dio_read
is called, it hangs and does not display anything.
Fopen Example
exec('mode com2: BAUD=2400 PARITY=N data=8 stop=1 xon=on');
$fp = fopen("COM2:", "r");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
echo fgets($fp);
fclose($fp);
}
DIO Example
exec('mode COM2: baud=2400 data=8 stop=1 parity=n xon=off to=on');
$fd = dio_open('COM2:', O_RDONLY | O_NONBLOCK, 0644);
echo dio_read($fd, 256);
I'm unable with my tries of both ways to gather any useful output.
Do I require inpout32.dll
file to connect?
And what is role of php_iol.dll
, this file I also required?
PHP can not read from serial ports under Windows. This is not the case on a Linux system, on which there is no problem to reading from COM ports with the common PHP filesystem functions.
Those filesystem functions are also the preferred ways to do in regard of the DIO extension:
The use of the DIO functions should
be considered only when direct control of a device is needed.
In all other cases, the standard filesystem functions are
more than adequate.
As you report your DIO example as "non-working", you need to keep track of errors. See:
- How to get useful error messages in PHP?
Next to that you need to find a working POSIX example first, as DIO is not hyperterminal but POSIX style.
Alternatively some software exists to proxy a COM port through network (for example GPL'ed serproxy). This might solve you integration problem with your operating system, however I'm pretty sure you're out of luck.
Compare with:
- How to Read RS232 Serial Port in PHP like this QBasic Program (Jul 2010)
- Reading COM (Serial Modem) in PHP (Jun 2011)
- Giving PHP permission to access COM port (Feb 2012) - writing only