Click to See Complete Forum and Search --> : Trying to use serial input to write a text file


mikelbrady
11-21-2002, 11:28 AM
Hi folks, first time post. I did a search on this, but could not find what I needed. I am trying to use the serial port to log the data coming in to a file. This is connected to a device that outputs a line of data every couple seconds. I am able to grab a line of data, which is what I want, but I am have to manually hit Ctrl C to end the script. I want to have the script end after a line of data is recieved from the device. Here is what my script looks like right now:

#!/bin/bash
stty -F /dev/ttyS0 quit \012
cat /dev/ttyS0 >> logfile.txt

Using this script it will listen to the serial port and write the input to the text file, bit then it just sit's there waiting. I thought by adding the quit \012 to the stty line I would be telling it to quit when it recieves a carriage return. If I hit enter on the keyboard it does end the script, but when connected to my device, or even a terminal, hitting enter on the terminal does not end the script. How do a I get this to end the CAT command or the script based on imput from the serial port? I feel like I am almost there.....

Stuka
11-21-2002, 01:55 PM
Look into the read command for bash - cat will keep going until it gets an EOF marker, which it's obviously not getting over the serial port (or one's not being sent, which amounts to the same thing). read, OTOH, gets one line at a time - and I think that's what you need.

mikelbrady
11-21-2002, 02:59 PM
Thanks Stuka, let me try that. Let's say I am recieving a LF and CR from the serial input, could I use stty to set my end of file to CR?

Stuka
11-21-2002, 04:09 PM
Not sure if that's possible - that's why I suggested just using read.

mikelbrady
11-21-2002, 04:13 PM
I can't seem to find a way to get read to accept input from /dev/ttyS0

Sorry for my ignorance on this.

mikelbrady
11-21-2002, 04:52 PM
OK, if anyone is following this thread, here is some more info. I took a trace of the serial device using a breakout box program in Windows. Here is what each the device is outputting:

MESS:8,0,2.9,40,33,48,0,021121,154216<CR><LF>


So you can see there is a carriage return and line feed at the end of each data line. I thought I might be able to use stty to set the end of file or quit to CR, but can't seem to get it to work.

Stuka
11-21-2002, 04:58 PM
I think to get read to work you'll need to do this:myscript < /dev/ttyS0since read is pulling from the stdin of the script.

mikelbrady
11-21-2002, 05:22 PM
Sorry to waste your time. I must be dense, but I can't figure this out. I'm not sure how to setup the script to use the read command. I have tried a bunch of stuff, but none of it seems to work.

mikelbrady
11-22-2002, 11:27 AM
Thanks for the help! I looked deeper into using the read command, and after sleeping on it last night, I figured out how to use it. I was stuck on trying to get read to use /dev/ttyS) directly. Once I setup my script like this, it worked:

testscript:

#!/bin/bash
echo Reading from controller....
read CNTDATA
echo "$CNTDATA" >> logfile.txt

and then call it like this:

testscript < /dev/ttyS0

When I run the script, it waits until it recieves a line from the serial device, then echoes it to the file, and ends the script. Exactly what I needed.

Thanks again.

Stuka
11-22-2002, 01:03 PM
Cool! Glad I could help.