Read from serial port and store in hexadecimal
Asked Answered
D

1

5

I have a vhf radio which sent a status message continuosly through the serial port, and I need the messages that I got to be stored as hex data in a text file

I tried hexdump command as shown below, and the data that I've got from vhf radio is correct, but the problem with this script that when I execute it, it does not end until I press ctrl-c

d -A n -t x1 -w128 /dev/ttyS0 > file.txt

so I've tried another command which is read command as follow:

COUNTER=0
while [ $COUNTER -lt 10 ]; do
read -r -t1 -N128 DATA < /dev/ttyS0 
echo $DATA >> file1.txt
od -A n -t x1 -w128 file1.txt >> file2.txt
let COUNTER=COUNTER+1
done

but the data stored in file2.txt is not correct.

the message that I got from the radio is not in a format that I could interprete it as per the radio prtocol document. so when I said that the data is not correct I mean that the message is could not be interpreted (it received randomly)

note that I've setted the serial port before executing both scripts as follow:

stty -g /dev/ttyS0 raw
stty -F /dev/ttyS0 9600

so, please help me to figure this out. or gave me aother way to read from serial port.

Regards,

Derian answered 28/9, 2015 at 12:13 Comment(4)
So you want the script to terminate when? Once a certain number of bytes have arrived, or?Gilbertine
yes I want the script to terminate once a certain number of byte is received. the status message of the radio is around 45 bytes but because the radio is transmitting continuously I want to make sure that I received a whole status message. so lets say that I need to receive 100 bytes then terminate the script.Derian
For the first command where you get the data you want, can you please post a sample of the data in your question (say 100 bytes of it)?Gilbertine
Is the data ASCII? The need for CTRL+C is most likely that the 'data' does not contain an EOF (end of file) marker. If you know what the 'start of message' and 'end of message' markers are then you can extract by looking for those patterns. You might also try 'tail -c (--bytes=K) and pipe that to your hexdump tool. :)Fasten
D
8

The Problem is solved :)

I tried to use hexdump command by setting -N to KB and it works successfuly

it reads from serial port until 1000 bytes and it stops

od -A n -N KB -t x1 -w128 /dev/ttyS0 > /tmp/filename.txt

so, thank you guys for your cooperation. I really appriciated.

Derian answered 30/9, 2015 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.