LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Daemon to read from com ports (http://www.linuxquestions.org/questions/programming-9/daemon-to-read-from-com-ports-617482/)

bzzz 01-30-2008 04:37 PM

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.

theNbomr 01-31-2008 12:16 PM

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.

--- rod.

bzzz 02-01-2008 02:24 AM

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.

Thanks a lot.

theNbomr 02-01-2008 11:01 AM

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.

--- rod.

osor 02-02-2008 01:52 PM

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
        # process fields here, and access them with $date, $time, etc.
done < /dev/ttyS0



All times are GMT -5. The time now is 02:34 PM.