Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-11-2010, 08:17 AM   #1
LQ Newbie
Registered: Aug 2010
Posts: 1

Rep: Reputation: 0
bash: serial port write THEN read

Hi folks,

I need to communicate with a target device using serial port and want to access it with bash commands solely. I poll the device by sending a string and then wait for a response string.

Setting up the connection using stty works fine and I can send messages to the device simply by "abcdef > /dev/ttyS2", which are well received. I'm facing a problem when trying a WRITE/READ operation:

abcdef > /dev/ttyS2 ; read -n20 RESPONSE < /dev/ttyS2 ; echo $RESPONSE
This way I'll end up stuck waiting for a response, although I know that my target device sends more than 20 characters.

It works, if I send the message and put a breakpoint in my device before it responds. That way "read -n20 ..." will receive the 20 characters perfectly.

I can only guess that the read command is executed too late.
Unfortunately I can't get into read mode before sending, since then I'm told "Permission denied".
Is there any way to open /dev/ttyS2 for both writing and reading? Or can I redirect input buffering in a way, that no messages will get lost?

Appreciating your suggestions.
Old 08-11-2010, 10:05 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your problem most likely has to do with the way serial ports tend to be configured by default. It is waiting for a line terminated by an end-of-line character(s). You probably want to get it into raw mode.
stty -F /dev/ttyS2 raw
You may also need to adjust the permissions to allow non-root access. This can probably be made automatic at boot time using appropriate udev rules.

I would consider bash to be a poor choice for developing serial communications applications.

--- rod.
1 members found this post helpful.


port, read, serial, stty, tty, write

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
[bash] synchronizing write/read to serial t00c00l Programming 3 08-01-2009 09:44 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
Should be dead simple - read/write serial port from bash ericcarlson Linux - Software 3 10-21-2006 09:35 PM
Serial port Read Write SeanatIL Programming 2 07-14-2004 04:42 PM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 01:21 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:39 PM.

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