LinuxQuestions.org
Visit Jeremy's Blog.
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 12-11-2014, 05:02 PM   #1
ffonseca
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Rep: Reputation: Disabled
Talking bash script to recieve and send data for serial port


Hi.

I have a problem design this script when i dont know if use expect for recieve and response the data or read the output with a cat.

Please can anybody help me?

thank you.

Sorry for my english i dont speak very well this.
 
Old 12-11-2014, 06:00 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,057

Rep: Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831
post what you have done first
 
Old 12-11-2014, 06:27 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,133

Rep: Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341
Welcome to LinuxQuestions.

Can you provide some information on the device or its communication protocol? I really do not like using bash for serial communications.
 
Old 12-11-2014, 07:15 PM   #4
ffonseca
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Well this is my script maybe understand.

stty /dev/ttyS0 raw speed 8600
Spawn cat /dev/ttyS0
Expect "0036303030303030332323" #this is a data that send the dataphone
Send "echo 06 > /dev/ttyS0" #I response only if receive the before data
Send "echo 3030303232321232344211343213313442" this data contains information I make this.
Expect "3030303030"

Basically is this.

Thank you
 
Old 12-11-2014, 09:19 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,133

Rep: Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341Reputation: 5341
I assume that the baud rate should be 9600 vs 8600.

Is the data text or hex? It looks like hex. Not an expert of expect / tcl but

expect "x00x36...x23"
send "x30...x42"

Not many examples but here is one:
https://embeddedfreak.wordpress.com/...ial-port-data/

I prefer to use c for serial I/O so I can not help that much.

Last edited by michaelk; 12-11-2014 at 09:20 PM.
 
Old 12-12-2014, 05:47 AM   #6
ffonseca
LQ Newbie
 
Registered: Dec 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for your answer yes when I send data this is hex.
Now for the language if this C is best no problem I learn if this necessary.

Thank you
 
Old 12-15-2014, 01:09 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,771
Blog Entries: 13

Rep: Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805Reputation: 4805
Quote:
Originally Posted by ffonseca View Post
Thank you for your answer yes when I send data this is hex.
Now for the language if this C is best no problem I learn if this necessary.

Thank you
I would write a C program versus use a script to communicate over a serial port. Ultimately you may eventually wish to do other processing besides receive and transmit. You may also need to decode, parse fragmented data, or conduct other processing. As a result you'll need to either use threads or write efficient code which will service the serial port rapidly enough to not have data be lost while you are performing other processing. In my signature there should be a hyperlink number under "Blog Entries:" click there and within the list of entries is an example of a USB serial port receiver talking to a GPS device. It shows you how I configure and open a USB serial port for communications and also how I transmit/receive with the same. In general there are other Linux serial port examples out there if you web search.

The big part for me is when I mention fragmented data. I do a lot of things like serial interface to GPS or other sensors where the data is continually streaming. Say most messages are 75 characters, I may receive 22 chars with one receive call, then 10 with the next one, and then 112 with a next call. Therefore I end up needing to have a parser which keeps track of where I last was in a message so when I exit and re-enter my receive loop, I can continue parsing new data without having lost where I was last at. Even though what you're showing appears to be small amounts of data, it is only going at 9600 and I'd recommend that you never assume that you're always going to get an entire data frame each time you perform a receive, nor that you'll only ever receive just one single data frame each time you perform a receive.
 
  


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
Can we send and recieve the data using uarts in lpc3250 other than uart5 biyabani Linux - Newbie 1 04-08-2013 03:00 PM
How send to data through serial port using linux C waqasdaar Programming 2 10-20-2009 02:55 PM
Can't send data on serial port (recieving works) OdieQ Linux - Hardware 7 07-26-2008 04:38 AM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 12:21 AM
shell script: sending data through the serial port funkymunky Programming 2 06-09-2004 11:47 PM

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

All times are GMT -5. The time now is 12:46 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