LinuxQuestions.org
Help answer threads with 0 replies.
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 07-31-2009, 05:20 PM   #1
t00c00l
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
[bash] synchronizing write/read to serial


Hello all!

I have created an rs323 loop and connected it to my door. When its closed, loop is closed, when door is open loop is open.

Trying to create two bash scripts, one of which will write to serial, and the other read from it. Consequently if the read pattern is different to what was sent the door would be marked as open.
Write script is relatively easy:
Code:
#/bin/bash
while true
do
 sleep 0.1
 echo "t" > /dev/ttyS0
done
However reading is a bit more tricky. I ve been trying to improve what I wrote many times, but I keep on getting a few false alarms (currently around 20-30%).

Here is my read code:

Code:
#/bin/bash
tester=0
seq=0
opened=10
while true
do
  let "tester += 1"
  let "LINE = empty"
  read  -t 1 LINE < /dev/ttyS0
  if [ "$LINE" = "t" ]
   then
   if [ $opened= "1" ]
    then
    let "opened= 0"               # CLOSED
   fi
   let "seq = 0"
  else
   let "seq += 1"
  fi
  if [ $seq = "5" ]
   then
   let "opened= 1"                # OPEN
  fi
done
Any hint or idea of how to create it in a more professional way?
 
Old 07-31-2009, 06:10 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Obviously you are using the serial port in a unusual manner. I could suggest other methods that would be better suited for this type of application but do not know if this is a just for fun, home or a school project.

Have you tried limiting read to a single character?
 
Old 08-01-2009, 01:48 AM   #3
t00c00l
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
if this is a just for fun, home or a school project.
Well... I have two dogs at home, and while I work my neighbor helps me and walks them. I would like to have a flexible mechanism to check when that happened to plan the next walk.

Quote:
Originally Posted by michaelk View Post
Have you tried limiting read to a single character?
Nope, how do I do that?
 
Old 08-01-2009, 08:44 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Trying using the -n 1 option to read one character. Just some basic reasoning is that the serial port does have a small buffer so your program might still sense the door is closed. Being that the computer loops much faster vs the ports baud rate. Other false alarms are probably being caused by switch bounce or just timing issues. You could use hardware control lines i.e. DTR,RTS but this involves some serial port programming skills. A pullup resister circuit would prevent switch bounce.

I would suggest using the parallel port instead if your computer has one built in and not being used. This will not work with a USB-parallel port adapter. The circuit is a little more complicated but will eliminate false alarms and you would need to write a simple c program. You would use the switch and pullup resister example. The pullup resister helps prevent false alarms and always keeps the circuit in a known state. Using one data line set high for +5V connected to the resister other side going to a status pin through the switch and then ground.

There are other programming examples but here is one.
http://www.epanorama.net/circuits/parallel_output.html
 
  


Reply



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
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 08:35 PM
Should be dead simple - read/write serial port from bash ericcarlson Linux - Software 3 10-21-2006 08:35 PM
read write in bash script yhus Programming 6 09-18-2006 12:23 PM
Serial port Read Write SeanatIL Programming 2 07-14-2004 03:42 PM
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

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

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