4

I am a bash novice and I am struggling with putting it all together.

What I am trying to do is:

1) Set Port (stty)
2) Read from dev/ttyUSB0 - data should look like 000118110000101 (cat or Gawk?)
3) Set read data into a variable eg DATA and create a URL eg http://domain.com/get_data.php?data=$DATA
4) load the URL with wget?
5) Wait for more data from ttyUSB0 (polling or loop?)

I have tried the php DIO extention that does work but is not reliable because it stops/starts for some reason.

ANY suggestions would be much appreciated, I will be very great-full if anyone could advise the best way to do this

Thanks

Brent

afro360
  • 620
  • 3
  • 9
  • 22

2 Answers2

8

This is what I used.

#Set permisions
sudo chmod o+rwx /dev/ttyUSB0


#!/bin/bash

# Port setting
stty -F /dev/ttyUSB0 cs7 cstopb -ixon raw speed 1200

# Loop
while [ 1 ]; 
do
    echo 'LOADING...'
    READ=`dd if=/dev/ttyUSB0 count=22 | sed 's/ //g'`
    echo $READ
    wget http://localhost/BASHtest/test.php?signal=$READ
    echo '[PRESS Ctrl + C TO EXIT]'
done
afro360
  • 620
  • 3
  • 9
  • 22
  • 1
    +1 for posting a solution to your own problem. To explain re: non/blocking reads; blocking reads stop the program until it's finished reading (potentially forever if it's a never-ending stream of data). A non-blocking read starts reading then gets on with other work (like updating the UI). A second thread handles the data as it comes in. So... If the read you're doing is a blocking read, you need to be aware that more than a small amount of data at a time will make your app seem to hang. – Basic Feb 12 '12 at 00:35
0

For the first step i would advise to read to a file and then use od to get an octal (there's no binary as far as i can see) representation, because standard awk doesn't cope with NULs (i think gawk too). So after you get the bytes, you pipe it through sed script to change octals to binaries, grab the output with $() (or apostrophs) and make an URL, which you feed to wget.

The only problem i can see is blocked/nonblocked read from usb. Please report if there will be one.

  • Thanks for your prompt reply pooh, sorry but my experience with reading from serial/usb devices are limited. Could you please explain what blocked/nonblocked read is? – afro360 Feb 09 '11 at 08:15
  • @afro360: i have no chance to check it now, but i'm afraid that if you just do `cat < /dev/ttyUSB0 > tmpfile`, it'll be stuck forever. The other option could be to use `dd` and request many bytes, so it'll return with lesser amount, and there you go. Try to experiment first with how to get chunks of data in a reliable way from /dev/ttyUSB0. –  Feb 09 '11 at 08:52