LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2008, 03:37 PM   #1
bzzz
LQ Newbie
 
Registered: Mar 2007
Location: Sibiu, Romania (Europe)
Distribution: Debian Lenny
Posts: 16

Rep: Reputation: 0
Angry 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.

Last edited by bzzz; 01-30-2008 at 03:39 PM.
 
Old 01-31-2008, 11:16 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 02-01-2008, 01:24 AM   #3
bzzz
LQ Newbie
 
Registered: Mar 2007
Location: Sibiu, Romania (Europe)
Distribution: Debian Lenny
Posts: 16

Original Poster
Rep: Reputation: 0
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.

Last edited by bzzz; 02-01-2008 at 01:25 AM.
 
Old 02-01-2008, 10:01 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 02-02-2008, 12:52 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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
 
  


Reply

Tags
com, php, port, serial, siemens, sql



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Enable daemon to run with service daemon start,etc baddah Programming 6 12-02-2007 05:51 PM
reject all ip & ports and allow only some ports with iptables ysar68 Linux - Security 1 05-12-2007 08:50 PM
USB 2.0 ports: devices not detected, but works fine with USB 1.1 ports (Kubuntu 6.06) KWTm Linux - Hardware 1 05-09-2007 04:42 AM
are outbound ports converged while local ports are ok ? inanc Linux - Networking 0 02-06-2007 10:37 AM
named daemon variable ${ROOTDIR} in init.d does not allow zones to be read r00tnuke Red Hat 0 01-24-2004 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration