Joined: Wed. Oct 4, 2006
Read serial port with PHP? Yea some time we may face such a. I'm working on linux machine so I can do the serial port reading with PHP itself, In. I will put the code in a folder called serialport inside /var/htm/ so I can. Aug 09, 2016 Most embedded Linux / BSD systems such as routers, servers and nas devices comes with console interface (serial port with RS-232). BIOS can uses this, and after boot BIOS screen I/O is redirected so that you can use the device. RS-232 is also used for communicating to headless server, where no monitor or keyboard is installed, during boot when operating system is not running yet.
Posted by mooseydoom: Thu. Mar 13, 2008 - 09:15 PM
Total votes: 0
A thread documanting my trials and tribulations using a serial port in Linux. May eventually turn into a tutorial. I have been looking for a hterm or bray's terminal substitute for use under Linux, but there seems to be little info on what and how to make stuff work.
My current setup:
Fedora core 8
Keyspan USA-19HS usb to serial adapter
STK500 board
Fedora core 8
Keyspan USA-19HS usb to serial adapter
STK500 board
Setup hardware
Luckily my Keyspan usb to serial adapter was automatically recognized and set up as /dev/ttyUSB2 by Fedora. Run
after you plug in the adapter to see if the device was set up. You should see something like:
usb 7-1: new full speed USB device using uhci_hcd and address 6
usb 7-1: configuration #1 chosen from 2 choices
keyspan 7-1:1.0: Keyspan 1 port adapter converter detected
usb 7-1: Keyspan 1 port adapter converter now attached to ttyUSB2
usb 7-1: configuration #1 chosen from 2 choices
keyspan 7-1:1.0: Keyspan 1 port adapter converter detected
usb 7-1: Keyspan 1 port adapter converter now attached to ttyUSB2
The last line tells you that the adapter was set up as ttyUSB2. Check the dev folder by running
crw-rw---- 1 root uucp 188, 2 2008-03-13 12:30 /dev/ttyUSB2
**Important note: 'Permission denied' errors
Notice that /dev/ttyUSB2 is owned by the root user and the uucp group. You might get 'Permission denied' errors when trying to use the /dev/ttyUSB2 port if you are not root or a member of the uucp group. There are a couple of options around this.
A) 'su' to root
B) prefix 'sudo' to your commands. ex: 'sudo stty -F /dev/ttyUSB2 9600'
C) add yourself to the uucp group. 'gpasswd -a me uucp'
D) make the /dev/ttyUSB2 device readable and writable to everyone. sudo chmod 0666 /dev/ttyUSB2
E) create a udev rule to make the /dev/ttyUSB2 device readable and writable by everyone whenever the adapter is plugged in. (too complicated to explain here)
Notice that /dev/ttyUSB2 is owned by the root user and the uucp group. You might get 'Permission denied' errors when trying to use the /dev/ttyUSB2 port if you are not root or a member of the uucp group. There are a couple of options around this.
A) 'su' to root
B) prefix 'sudo' to your commands. ex: 'sudo stty -F /dev/ttyUSB2 9600'
C) add yourself to the uucp group. 'gpasswd -a me uucp'
D) make the /dev/ttyUSB2 device readable and writable to everyone. sudo chmod 0666 /dev/ttyUSB2
E) create a udev rule to make the /dev/ttyUSB2 device readable and writable by everyone whenever the adapter is plugged in. (too complicated to explain here)
Set up the baud rate with the stty command. I'm using 115200, so I run
. To verify the settings, run
speed 115200 baud; line = 0;
min = 1; time = 5;
ignbrk -brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
min = 1; time = 5;
ignbrk -brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
You can do some very primitive debugging. Receive data from the STK500 by opening a terminal window and running
You can send characters by running
But it would be really much better if all this could be done inside one program, and maybe get some hex or binary viewing too.
Candidate programs:
* HTerm under Wine
* Bray's Terminal under Wine
* Minicom
* Picocom
* Gtkterm
* Cutecom
HTerm and Wine
I installed wine using yum.
Setup the serial port to be recognized by wine. Create a symbolic link to /dev/ttyUSB2 in ~/.wine/dosdevices. (run 'wine hterm.exe' first so that wine can create the ~/.wine directory).
I chose com5 because I already had /dev/ttyS0 thru /dev/ttyS3 which get mapped to com1 thru com4 in wine.
I can run HTerm in Wine,
I can run HTerm in Wine,
but HTerm doesn't seem to recognize my serial ports under Wine. I can connect to com5, but nothing really happens. There's an error in the console: 'fixme:comm:set_queue_size insize 4096 outsize 4096 unimplemented stub' so maybe HTerm is calling something that isn't implemented in Wine. Also, there's something glitchy about the cursor placement when you type in text. The cursor is about one character in front of where it should be.
![Linux serial port Linux serial port](http://fedora.12.x6.nabble.com/file/n4938590/image.png)
Bray's Terminal and Wine
Bray's website http://bray.velenje.cx/avr/terminal seems down right now, but I downloaded terminal.exe from here http://www.myplace.nu/mp3/download.htm (scroll down to the 'Tools and Others' section)
Set up the serial ports as described in the above HTerm section.
Bray's terminal actually works when you connect! :)
Minicom v2.2
I finally got minicom working, although I don't really like minicom. It's like running hyperterminal in windows... yucky.
Run the minicom setup.
Go to Serial port setup and set the Serial Device to /dev/ttyUSB2. (Press A to change the Serial Device, and press Enter when you are finished to go back to the menu)
Press E to set the baud rate (Bps/Par/Bits). I'm using 115200 bps. If you know what stop bits and stuff you need to use, set it here. If you don't know, I think '8N1' is pretty standard and should work. My setting reads 115200 8N1
Press F to set the Hardware flow control to 'No'.
Press G to set the Software flow control to 'No'.
Leave Callin Program and Callout Program blank. (I have no clue what these do)
Press Enter to go back to the main menu.
Press E to set the baud rate (Bps/Par/Bits). I'm using 115200 bps. If you know what stop bits and stuff you need to use, set it here. If you don't know, I think '8N1' is pretty standard and should work. My setting reads 115200 8N1
Press F to set the Hardware flow control to 'No'.
Press G to set the Software flow control to 'No'.
Leave Callin Program and Callout Program blank. (I have no clue what these do)
Press Enter to go back to the main menu.
Go to the Modem and dialing menu. I'm not sure if this is necessary, but I deleted all of the strings in here. Press Enter to go back to the main menu.
Go to the Screen and keyboard menu. Press Q and set the Local echo setting to Yes. This allows you to see what you are sending over the serial connection in the terminal. Press Enter to go back to the main menu
Choose Save setup as dfl then Exit the minicom setup. It should start up minicom.
In the minicom window you should be able to start typing to send characters. Press 'ctrl-a z' to see a list of menu options. I have to go to the menu and set the local Echo to off then on each time to see my input, even though it's supposed to be saved in the settings
Picocom v1.4
I try running picocom
but I'm not sure if it's working. Press 'ctrl-a x' to exit the program.
Gtkterm v0.99.5
Run gtkterm
Go to Configuration => Port and setup the device and baud rate. Turn on the Local Echo by making sure Configuration => Local Echo is checked. Now start typing!
Cutecom
There was no cutecom package in yum, so I have to compile it myself. I downloaded 0.20.0 from their site http://cutecom.sourceforge.net
Install cutecom:
The README says to make sure to use qt4's qmake. Check which qmake is in your path with
This returns '/usr/lib64/qt-3.3/bin/qmake' for me, which is qt3's qmake. Find where yum put qmake for qt4
Looks like qt4's qmake is at /usr/lib64/qt4/bin/qmake. Add this to the PATH variable like it says to do in the README
The install went ok. Running Cutecom opens up a nice Bray-esque interface. :)
Last Edited: Fri. Mar 14, 2008 - 12:15 AM
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.
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
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)
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.
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.
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.