Php Serial Port Communication Linux Download

  

I'm working through exactly the same issue as described by skaap2k. (11/04/07 12:01 AM, ID: 22503690)

PHP USB serial port call from Windows to Linux. Up vote 1 down vote favorite. I have a piece of simple code that works on a Windows - WAMP environment, e.g. Try composer require hyperthese/php-serial. The only tiny gotcha is you need to make sure the user/program is able to access the dialout group. This class can be used to communicate with a serial port under Linux or Windows. It takes the path (like '/dev/ttyS0' for linux or 'COM1' for windows) of serial device and checks whether it is valid before opening a connection to it. Once the connection is opened, it can send data to the serial port, and read answers (reading is only implemented for Linux).


I am using a Linux (Ubuntu) server and PHP as a web server. The solution offered appears to be for a command line version of PHP, and doesn't work in my web server.
In short, I need to read and write to a GSM modem via the serial port using php. Writing is working fine.
Reading using fread() of /dev/ttyS1 stalls the server until the required number of bytes set in fread() and a CR is received.
I need to return from checking the serial port with a null if there is no new data. I'm happy to pick a byte at a time from the serial buffer and assemble it into a string in php.
Any suggestions welcome!
Active9 years, 2 months ago

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

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:

}

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

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!

dmp
10.1k6 gold badges33 silver badges46 bronze badges
CJ_CJ_

3 Answers

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

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

MikeMike
18.3k2 gold badges33 silver badges62 bronze badges

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.

Open serial port linux

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.'

dmpdmp
10.1k6 gold badges33 silver badges46 bronze badges

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'.

PiskvorPiskvor

Php Serial Port Communication

73.9k41 gold badges159 silver badges210 bronze badges

Linux Serial Port Example

Not the answer you're looking for? Browse other questions tagged phphardware-interfaceqbasic or ask your own question.