Logging serial port data to file

Michael Hagedorn michael at hagedorn.co.nz
Sat Mar 27 07:01:11 UTC 2010


Ok, here we go:

I didn't compile the dos2unix command into my busybox, therefore I
didn't try it yet.

tr great command good to know about, but I didn't get it to work
correctly, I guess once a line has been read a line will be written.

The

exec </dev/ttyUSB0
stty igncr

while read -r; do
    echo "[$(date)]: $REPLY"
done </dev/ttyUSB0 >>/usb/BigDrive/CurrentCost/CurrentCost.log

works perfect.
Thanks for your help.

As for the extra while loop to keep my logger alive if the usb2serial
has been unplugged and wait to be plugged in again, that didn't work.
I unplugged the usb2serial and plugged it back in. For some reason it
then showed up on /dev/ttyUSB1 (normally the script would die and the
usb would show up as /dev/ttyUSB0 again). I guess the script was
blocking /dev/ttyUSB0 in some way. Do usb2serial devices have
identifiers? If yes I could iterate through them to find the correct
one (I want to log multiple usb2serial devices later on).

I will leave that extra loop out for the moment and try the inittab
option. How fast is the respawning happing?


Ok here is one more question. I would like to have each day go into
its own file, I tried:

done </dev/ttyUSB0 >>/usb/BigDrive/CurrentCost/CurrentCost$(date
+"%Y_%m_%d_%M").log

That didn't work (I tried it with a %M in there so that I wouldn't
have to wait for a day). I guessed that that wouldn't work since the
output is probably once opened and then just continuously streamed to.
What would be the best way to do this?

Thanks again for your help

Regards.


On 26 March 2010 10:15, Cathey, Jim <jcathey at ciena.com> wrote:
> Things in the /etc/inittab are automatically
> respawned when they fail.  That's its main job.
>
> Your script closes the file between the stty and
> the read loop.  You are counting on the settings
> being persistent.  That's why I used the exec in
> my last example.
>
> Alternately, the last "done" could be "done </dev/ttyUSB0",
> and take out the other input redirections.  What you
> want is a single program entity that's operating on
> the intended device.
>
> I think your 10-second retry would work, but be sure
> that the USB device doesn't end up as your controlling
> terminal, giving your script a SIGHUP when it's disconnected.
>
> You need to read up on the TTY driver, a ^D will likely
> also end the read loop.
>
> -- Jim
>
>
>
> -----Original Message-----
> From: Michael Hagedorn [mailto:michael at hagedorn.co.nz]
> Sent: Thursday, March 25, 2010 2:00 PM
> To: doug.clapp1970 at alumni.unc.edu
> Cc: Cathey, Jim; Michael Abbott; busybox at busybox.net
> Subject: Re: Logging serial port data to file
>
> Thanks a lot for your input, I will try them tonight and report back
> which one works the best.
>
> Another thing is that if somebody pulls out the usb 2 serial adapter
> the script fails and exits.
> I want it to continue or restart if the device has been put back.
>
> I have come up with this (but haven't tried it yet):
>
> while true; do
>
>    # Make sure the boudrate is set correctly
>    stty -F /dev/ttyUSB0 57600
>
>    while read -r; do
>        echo "[$(date)]: $REPLY"
>    done </dev/ttyUSB0 >>/usb/BigDrive/CurrentCost/CurrentCost.log
>
>    # Try again in 10 seconds
>    sleep 10
> done
>
>
> Rather than trying to read from /dev/ttyUSB0 is there a way to check whether
> it is connected?
>
>
> Regards
>
> On 26 March 2010 06:34, Doug Clapp <doug.clapp at triad.rr.com> wrote:
>>
>>
>> Cathey, Jim wrote:
>>>>
>>>> Is there an easy way to disregard lines that contain only control
>>>> characters or filter the text?
>>>>
>>>
>>> Not trivially.  As you've been shown, "tr -d" is your
>>> buddy.
>>
>> Another possibility is to pipe through dos2unix (rather than tr) to remove the CR characters. Ultimately you have to decide which method works best for you.
>>
>> Doug Clapp
>
>


More information about the busybox mailing list