LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] synchronizing write/read to serial (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-synchronizing-write-read-to-serial-744250/)

t00c00l 07-31-2009 05:20 PM

[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?

michaelk 07-31-2009 06:10 PM

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?

t00c00l 08-01-2009 01:48 AM

Quote:

Originally Posted by michaelk (Post 3627150)
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 (Post 3627150)
Have you tried limiting read to a single character?

Nope, how do I do that?

michaelk 08-01-2009 08:44 AM

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


All times are GMT -5. The time now is 10:06 PM.