ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 &
What to do with it's standard output? You haven't specified, but since you did say 'run as daemon', it would seem that it's output should go either to a system log, or to /dev/null. Perhaps something like:
(again, untested)
Code:
bash < /dev/ttyS0 > /var/log/LQbzzz.log &
This is given primarily in the hope of clarifying the conceptual elements of your question.
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.
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 !
and I need to enter these values into a mySQL. So it doesn't give me proper linux commands, that's why I need a script. After every line like the one from the code tag, I need to make an insert into the SQL DB. So I need a script that gets everything that comes from the serial port and after every line (or "!"), execute a command or function (mysql insert), but I just don't know how to make it after every line.
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";
while( <COM> ){
#
# parse and insert records here...
#
my( $date, $time, $field0, $field1, $field2, $field3) = split /\s+/, $_;
}
The perl DBI module provides the infrastructure to manipulate SQL databases. Given the relative simplicity of your incoming data records, it should be simple enough to parse them and perform the necessary SQL insertions.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.