views:

1654

answers:

4

Greetings all.

I am looking for a way to communicate with RS232 serial COM port on windows. I have found 2 solutions on the net, one which is not totally free (introduces deliberate delays on the function) and another with limited capability on Windows. The latter can only write to a COM port on Windows, not read.

I can't look at the code of the first solution since it is compiled into a .dll (makes sense, otherwise people can just edit the delay and not purchase it...) and the second one seems only to use fopen() to open the port and later fwrite() to it for writing, just like one would do to a stream. But apparently freading it returns nothing.

I know it's possible as the first solution did it, although it does require Apache to use php-cgi module instead of php5module.

Any ideas?

Thanks in advance.

+3  A: 

The easiest way to tackle this would be to write a program in another language (such as C++) and then execute it from your php script with system(). Doing Comm I/O in C++ is trivial.

This assumes you have enough access to the server to configure it to allow the executable to be run by php, etc.

SoapBox
I'd recommend you develop your comm program as a PHP library so that it integrates directly with your PHP code. Using system() to call and external executable is a sub-optimal solution.
rmeador
Agreed, but to me that no longer falls under "the easiest way" and becomes "the best way". :-)
SoapBox
+2  A: 

Another possible way would be to use the Win32 API through something like w32api_register_function() or ffi and then use serial communications calls to get it to work under Windows.

lpfavreau
A: 

Another option is to use an object via ActiveX on windows. There are several, mostly commercial serial objects for COM on windows. You can also expose a .Net based object and register it for COM use as well. Of course, this does presume you have control on the server to register a COM control, as you would need a serial interface.

Another issue is resource contention if this is for use via the Web. If this is for a serial printer, for instance, then a print queue manager would be your best option over direct communication.

Tracker1
+1  A: 

You need to set the com with a dos command like like:

$output = `mode COM1: BAUD=115200 PARITY=N data=8 stop=1 XON=off TO=on `;

The next command executes the dos command through php:

echo"$output";

Create the resource id:

$fp = fopen('COM1', 'r+');

if(!$fp){

echo"Port not accessible";

}else{

echo"Port COM1 opened successfully";

}

Write to port:

$writtenBytes = fputs($fp, "Hello");

echo"Bytes written to port: $writtenBytes";

Read from port:

$buffer = fgets($fp);

echo"Read from buffer: $buffer";

Maybe somebody help me with the fgets problem. It stacks there for exactly one minute if TO=on or stacks there forever if TO=off. It seems to be a "MODE COM" option so maybe a DOS expert can help.