Php Serial Port Communication Linux Distributions

  

I'm using php-serial to communicate with a device via serial port. The device has a function to display all input and output to the serial port on a screen. When I run the following script I can see on the device that the script is taking every command output by the device and repeating it back to the device. In this post I will cover five conman utilities used for serial communication under Linux / Unix / *BSD and Mac OS X. Find out information about your serial ports Type the following dmesg command. By executing the following command, you grant PHP to use the serial port (USB) If you are not running Linux you don't have to run this command, in fact, you can't. For other linux distributions you are going to have to check its documentation (same for osx). On Linux, all serial ports are driven the same way. Only their device nodes are changing to reflect the port type: i.MX processor internal ports. Re: [SOLVED] Serial port dropping bytes The device on the other end is an Atmel xmega microcontroller. I did not use any flow control when I wrote the firmware as the communication routines in the firmware are very fast as the chip runs at 32 MHz and the USART is interrupt driven.

Active9 months ago

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.

Linux/php serial port read - How to stop fread() waiting forever if no new data? I think we have a problem with communication here. What I meant to convey is that. (PHP 4 >= 4.3.0, PHP 5 serial port. To get my Linux box to talk to an external serial.

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?

dsolimano
7,6473 gold badges41 silver badges57 bronze badges
syazsyaz
1,3046 gold badges29 silver badges43 bronze badges

7 Answers

Every solution above is either inefficient or too much work.

You can just use the PHP-DIO library (dio_fcntl, dio_open, dio_read, dio_write, dio_seek, ...). It's also in the PHP manual's entry for DIO:

This PECL package isn't available by default. To get it for Windows if you have PHP 5.2.x greater than 5.2.6, you can download it as part of a ZIP:

Both of these links were found in http://www.deveblog.com/index.php/download-pecl-extensions-for-windows/

Here is the build from Linux, just get it and do the phpize/configure/make/make install thing.

I don't know whether it should be used in an Apache session, but go for it.

user633183
77.9k22 gold badges152 silver badges192 bronze badges
Dustin OpreaDustin Oprea
5,3336 gold badges46 silver badges77 bronze badges
Open serial port linux

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.

SoapBoxSoapBox
18.2k3 gold badges41 silver badges82 bronze badges

You need to set up the com port using a DOS-like command.

For example, the following line executes the command through php:

To display the results you can use:

Create the resource id:

Write to port:

Read from port:

Maybe someone can 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.

Perhaps instead of fgets, one should use fgetc, since fgets capture through to the newline, while fgetc captures a single character. If a new line isn't encountered, it may block until there is one or until the buffer is flushed. The one minute delay may be windows flushing its buffer on an interval.

David Refoua
1,9851 gold badge23 silver badges39 bronze badges
Giorgos Pap

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.

lpfavreaulpfavreau
8,5783 gold badges26 silver badges35 bronze badges

I had the same problem and already considered writing my own php extension when I came across this solution which is popular with Arduino developers - 'serproxy' (found it at many places, ie. http://www.lspace.nildram.co.uk/freeware.html ) sets up a tcp stack to/from the serial ports and allowed me to use the php socket functions to communicate with it.

Linux Serial Port Program

MichaelMichael

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.

Tracker1Tracker1
14.7k9 gold badges68 silver badges99 bronze badges

If you want to deal with sms using com port then here is the most famous php serial communication class by Rémy Sanchez with google sample code. Here is a thread which includes that topic.

Community
adiadi
10.1k4 gold badges20 silver badges35 bronze badges

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

PHP Serial was written at a time where I did not know any other language thanPHP and I started to get seriously bored with its abilities.

I somehow got hold of a « Citizen C2202-PD » point-of-sale display, and I wantedto play around with it. I also managed to get the documentation of it, andcreated a convenience class to access the serial port though the Linux file.

Afterwards, I posted it to PHP Classes,and this probably is what brought it any visibility.

Example

State of the project

Interestingly enough, this piece of code that is widely untested has created alot if interest ever since it was created, and especially nowadays witheverybody toying around with Arduinos and Raspberry Pis. I receive about 1 emailevery month asking for help with the code or sending patches/suggestions.

I think that it is time for me to remove the dust off this project and to giveit a full visibility on modern tools, aka GitHub.

Bugs

There is lots of bugs. I know there is. I just don't know which are they.

Php Serial Port Communication Linux Distributions

Platform support

  • Linux: the initially supported platform, the one I used. Probably the lessbuggy one.
  • MacOS: although I never tried it on MacOS, it is similar to Linux and somepatches were submitted to me, so I guess it is OK
  • Windows: it seems to be working for some people, not working for someothers. Theoretically there should be a way to get it done.

Concerns

I have a few concerns regarding the behaviour of this code.

  • Inter-platform consistency. I seriously doubt that all operations go the sameway across all platforms.
  • Read operations. Reading was never needed in my project, so all the tests Idid on that matter were theoretic. I was also quite naive, so the API isprobably not optimal. What we need is to re-think reading from scratch.
  • Configuration done by calling functions. This is so Java. It would be muchbetter to be able to pass a configuration array once and for all. Furthermore,I suspect that the order of call matters, which is bad.
  • Auto-closing the device. There is an auto-close function that is registeredat PHP shutdown. This sounds quite ridiculous, something has to be done aboutthat.
  • Use exceptions. Currently there is an heavy use of the errors system to reporterrors (2007 baby), but this is seriously lame. They have to be replaced byactual exceptions.

Call for contribution

Linux Serial Port Programming

I have about 0 time to code or test this project. However, there is clearly aneed for it.

As in all open-source projects, I need people to fit this to their needs and tocontribute back their code.

What is needed, IMHO:

  • Address the concerns listed above, and find new ones.
  • Create a reproducible test environment for each OS, and prove that eachfeature works (basically, unit-testing).
  • Report of use cases, bugs, missing features, etc.

If you feel like doing any of those, do not hesitate to create an issue or apull-request, I'll gladly consider consider it :)

Licence

PHP SerialCopyright (C) 2007-2014 PHP Serial's contributors (see CONTRIBUTORS file)

Serial Port Communication Program

This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.

Serial Port Communication Software

This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.

Hyperterminal

You should have received a copy of the GNU General Public License alongwith this program; if not, write to the Free Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.