6

I'm trying to port the following small QBASIC program (which works 100%) to PHP:

OPEN "com1:2400,n,8,1,DS," FOR RANDOM AS #3
OPEN "data.dat" FOR OUTPUT AS #2
REM read 17 chars from the port
scale$ = INPUT$(17, #3)
PRINT scale$
WRITE #2, scale$
CLOSE #2
CLOSE #3
SYSTEM

Currently I'm calling it in its compiled (exe) form from PHP (on WAMP5) but I'd like to get rid of the QBASIC and call it directly from PHP.

I wrote this PHP function but it just hangs at the fgets() line:

function read_port($port='COM1:', $length=17, $setmode=TRUE, $simulate='') {
    if ($simulate){
        $buffer = '"'.strval(rand(1000, 2000));
        return $buffer;
    }
    if ($setmode){
        shell_exec('mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off odsr=on octs=on dtr=on rts=on idsr=on');
    }
    $fp = fopen($port, "rb+");
    if (!$fp) {
        file_put_contents('debug1.log','COM1: could not open'."\n",FILE_APPEND);
    } else {
        $buffer = fgets($fp, $length); // <-- IT JUST HANGS HERE DOING NOTHING !
        fclose ($fp);
    }
    return $buffer;
}

I'm using this PHP line to call the above function:

$res = read_port('COM1:', 17, TRUE, SIMULATE_SCALE);

Any help will be creatly appreciated! I've basically given up trying. If QBASIC can do it perfectly then we must be able to make this work with PHP!

user3840170
  • 26,597
  • 4
  • 30
  • 62
CJ_
  • 61
  • 1
  • 1
  • 2

3 Answers3

9

You might want to look into PHP Serial by Rémy Sanchez. There's an article about it here:

Controlling the Serial Port with PHP

Also have a look at this example provided by jared at dctkc dot com on the PHP site:

http://php.net/manual/en/function.fopen.php#20935

Mike
  • 21,301
  • 2
  • 42
  • 65
  • 2
    Thank you for your answer, but PHP Serial by Rémy Sanchez can only write a serial port but not read when running on Windows. Read and write is supported on Linux though. – CJ_ Jul 12 '10 at 09:58
  • 1
    @CJ_: There's this note on the PHP Serial page: *"If you're a windows user, try to access the serial port through network with serproxy instead"*. serproxy is used in the example given on the PHP site. The example shows writing, but Rémy Sanchez comment suggests that it can be used for reading too. If not, you can try danp's suggestion with the help of [Google Translate](http://translate.google.co.uk/translate?u=http%3A%2F%2Fwww.phpcs.com%2Fcode.aspx%3FID%3D36224&sl=fr&tl=en&hl=&ie=UTF-8). – Mike Jul 12 '10 at 10:14
5

Pretty sure PHP has no access to hardware ports by default. It has access to network resources, file resources, but without some kind of transport between the hardware and what you're trying to read, can't see this working.

There may however be a platform specific extension you can load which will enable this - just investigating.

e: Yep, there is - check this extension, might be what you're after. Without something like this, it's just not going to work.

"This extension allows the direct access the parallel and serial(rs232) port in reading and writing by the DLL inpout32.dll under WIN9x/NT/2000/XP for any assembly. An example of concret application: Complete house automation with web interface and php, connection hardware of any nature with the ports like assemblies simple or to complicate. One idea simple but quite practical... Extension and source was compiled with Delphi 6 for PHP5.0 to 5.1.2, sources and example included."

danp
  • 14,876
  • 6
  • 42
  • 48
  • Thank you for your answer danp, but my French is non-existing. What is download in French? téléchargement? – CJ_ Jul 12 '10 at 10:06
  • From a very quick overview, the extension suggested by danp looks simple to use. Here's a [Google Translate](http://translate.google.co.uk/translate?u=http%3A%2F%2Fwww.phpcs.com%2Fcode.aspx%3FID%3D36224&sl=fr&tl=en&hl=&ie=UTF-8) link. – Mike Jul 12 '10 at 10:16
  • @Mike and others: Just to let you know, I haven't found a workable solution yet, but have been using Google Translate ever since; very handy. My problem actually got worse. I switched to Wampserver 2.1 and my app now only works for a few hours and then hangs the system. Even a re-boot does not fix the situation, and my client has to switch to "weighbridge overide" mode. – CJ_ Feb 04 '11 at 06:59
3

If you're on Linux or other UNX-like system (e.g. Mac OS X), try fopen('/dev/ttyS0') - in UNX, everything is a file, even serial ports. See this for a few tips for finding out which port maps to which "file".

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222