Daemon to read from com ports
Hello.
I have to make a script that can run as daemon and execute commands after every line that it gets from the serial port. I tryied with PHP, but I don't know how to make it execut a command after every "\n" or "\r" that comes from the serial. I can read /dev/ttyS0, I see there every thing I want to get, but I got no ideea how to execute something after every line from the comport. It doesn't matter what programming language I use (I prefer PHP or Perl), I only need to insert some of the lines from a Siemens HiPath 3500 to SQL and than to a web interface to view all calls that has been made using the HiPath. I need a few words, I need a serial sniffer. Thank you in advance and please excuse my poor english. |
The concept of 'execute commands' is a bit vague, but I will assume you mean something like 'do what bash would do with a line of text'. With that in mind, it would make sense to simply run bash, or other shell, with it's standard input redirected to a serial port:
(untested) Code:
bash < /dev/ttyS0 & (again, untested) Code:
bash < /dev/ttyS0 > /var/log/LQbzzz.log & Note that there may some security implications associated with your endeavor, but that is a separate issue. Note also, that is quite possible to use a serial port as a proper console with login & password security, very much in the same sense as a telnet or ssh login via network. The Remote-Serial-Console-HOWTO/ may be worth a read. --- rod. |
Ok. Thanks for your reply. I'll give you an example of what I'm trying to do.
The HiPath gives me this (over the serial cable): Code:
01:02:2008 09:32 150 0269233914 0:49 ! Thanks a lot. |
Okay, to rephrase your question: you want to get incoming data records delimited by newlines from a serial port, and insert the records into an SQL database, and you prefer to use either Perl or PHP to do this. Since Perl is my weapon of choice, I will make a suggestion based on it.
Since reading a serial port can be done much like reading an ordinary file, and since Perl by default uses newlines to delimit records read from files, most of that work is done for you. Something like Code:
open( COM, "/dev/ttyS0" ) || die "Cannot read serial port : $!\n"; You may need to change the access permissions on the serial port, although it sounds like you have already accomplished some suitable access using another method. You can start the script from rc.local, or other acceptable method. --- rod. |
Or if you want to do this in bash, you could do something like the following:
Code:
while read date time field0 field1 field2 field3; do |
All times are GMT -5. The time now is 07:15 AM. |