LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-11-2011, 09:22 PM   #1
warwick
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Rep: Reputation: Disabled
Talking Taking command arguments from serial port into shell script


I have a linux system connected to an external sensor device that spits out strings of serial data every few seconds. I need to send the data to a remote URL for logging and graphing purposes.

The data coming down the serial is essentially in the format:

ID=1234,DATA=4567

And I need to call a URL from my linus box in the format

wget -s "http://www.remotesite.com/monitor.php?ID=1234&DATA=4567"

I can read the data to the shell using cat </dev/ttyS0 but I'm now stuck as to how I might then format that data and pass it to a shell script which would presumably run in an endless loop and contain the wget.

Any pointers, code snippets or suggestions please?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-12-2011, 11:31 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,509

Rep: Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735Reputation: 6735
Quote:
Originally Posted by warwick View Post
I have a linux system connected to an external sensor device that spits out strings of serial data every few seconds. I need to send the data to a remote URL for logging and graphing purposes.

The data coming down the serial is essentially in the format:
ID=1234,DATA=4567

And I need to call a URL from my linus box in the format
wget -s "http://www.remotesite.com/monitor.php?ID=1234&DATA=4567"

I can read the data to the shell using cat </dev/ttyS0 but I'm now stuck as to how I might then format that data and pass it to a shell script which would presumably run in an endless loop and contain the wget.
Any pointers, code snippets or suggestions please?
You don't say what you want this written in, but if you're using a shell program, I'd suggest starting here:
http://en.wikibooks.org/wiki/Serial_...g/Serial_Linux

Personally, I'd use Perl, and the Hardware::UPS module for serial port connectivity. Read from the serial port, and use the split function to chop it up into two variables, which you can then do what you want with. That gives you some error handling and other goodies too. You mention wget...are you sending that data to a remote box, for insertion into a database? Can you describe what you're trying to do, and the environment a bit more?

If it's a database that's used for graphing, I'd again definitely go with Perl. Just read the data, make a connection straight to the database over the network, and shove the records in.
 
Old 06-12-2011, 03:30 PM   #3
warwick
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks for the reply, and sorry for the holes in the info provided. The Linux box is very minimal - just an OpenWRT / DD-WRT equipped router, so my choices are somewhat limited. I'd rather stick to a simple shell script if at all possible. Error checking of the serial input isn't so important, I can afford to just lose the reading if there is a comms issue, but clearly dont want to 'lock up' the script.

I have some control over the data being sent by the serial device (its actually a little 8 bit processor that I have written code for), so the data string could be kept to a single alphanumeric string.

The data is indeed going to a remote box for graphing and/or alarm generation (freezing grannies, watery basements etc), and I would rather is possible keep all the database inserts etc remote server side - hence the wget.

I was initially just generating the whole shell command (eg wget -s "http://www.remotesite.com/monitor.php?ID=1234&DATA=4567") on the serial port and enabling a console on the serial port, but the odd comms glitch (I'm sending data every few seconds) means I get the odd unbalanced quote, and the shell then sits in an endless loop.

My thinking was that a simple shell script that grabs a line of serial data (not caring what it is) and just wgets it back out is a way round that issue.

Hope that's the detail needed, thanks once again for the reply.
 
1 members found this post helpful.
Old 06-12-2011, 04:04 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
I don't understand "I get the odd unbalanced quote".

Something like (assuming a recent bash and obviously untested):
Code:
#!/bin/bash
while true
do
   while -r read line
   do
       line=${line//,/&}  # Change , to & for PHP
       line=${line//\"/\\\"}  # Escape any " characters for use in double quoted string
       wget -s "http://www.remotesite.com/monitor.php?ID=$line"
   end < /dev/ttyS0
   sleep 1
end
 
2 members found this post helpful.
Old 06-12-2011, 04:24 PM   #5
warwick
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks Catkin.

By unbalanced quote (and when sending wget commands direct)I mean that on the odd occasion (but bearing in mind the data transfers every 5 seconds) the serial data stream is corrupted and one quote mark is lost. The shell is thus still looking for a quote mark after the carriage return (giving a prompt of '>', so when the serial device blindly sends the next update 5 seconds later, the first opening quote is seen as the closing quote of the last line, and the real closing quote opens a second! Thus unless or until the same error occurs again, no valid strings are sent.

By using your script below all I am relying on is getting a carriage return through to terminate the line - indeed even if I miss it another will be along shortly. At most a corrupted reading (I can add a simple checksum) but no chance of getting stuck in my loop as above. I'll admit I dont fully understand the syntax of the script you have given, but nothing a few hours reading wont solve (I hope!).

Many thanks.
 
Old 06-13-2011, 02:26 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Here are some links to aid your reading:
  • The ${line<whatever>} expressions are Shell Parameter Expansions which may need a recent bash version.
  • read
  • sleep man page (need not be as long as the 1 second illustrated -- can use a fractional decimal as the number of seconds)

Last edited by catkin; 06-13-2011 at 02:27 AM. Reason: brevity
 
Old 06-13-2011, 02:29 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by warwick View Post
By using your script below all I am relying on is getting a carriage return through to terminate the line - indeed even if I miss it another will be along shortly. At most a corrupted reading (I can add a simple checksum) but no chance of getting stuck in my loop as above.
You would need the checksum because read as shown blocks until it gets a newline -- and the alternatives of setting a timeout or reading up to another character do not seem helpful in this case.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] ? running shell script taking command line arguments in a jsp page? etika Linux - Newbie 1 02-11-2011 02:26 AM
Nautilus-connect-server taking arguments from command line? snowman81 Linux - Software 1 07-06-2010 11:25 AM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 09:35 PM
Serial Port read in shell script tjt Linux - Newbie 1 08-12-2004 10:18 AM
shell script: sending data through the serial port funkymunky Programming 2 06-10-2004 12:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:16 AM.

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