3

I am making a simple shell script to write and read data to serial device. I am using these commands at terminal and they are responding correctly:

To write I am using:

echo -en '\xAA\x04\xC2' > /dev/ttyUSB0

To read I am using read:

cat -v < /dev/ttyUSB0

but when I am including this in a shell script, it is not responding. I need help regarding this. Also I want to know that when I send the write command, It should give me output in hex format but it is giving me output in this format M--*^NM-^H Also need help in this.

planet260
  • 1,384
  • 1
  • 14
  • 30
Aircraft
  • 275
  • 2
  • 5
  • 12

2 Answers2

4

I would expect that your cat statement is blocking waiting for end-of-file. It may treat your input tty as a standard terminal device, in which case it will require an end-of-file character to terminate the input (e.g, Control-D) and it could apply other processing to the input and the output. You had best familiarize yourself with the stty command and its options (see stty man page). Try doing stty -F /dev/ttyUSB0 to see what options are set.

The easiest way of reading the input might be to read a single character at a time. You could try with the following script. This will read a single character at a time from the input until a 'q' is entered.

stty -F /dev/ttyUSB0 raw
stty -F /dev/ttyUSB0 -echo
while read -rs -n 1 c && [[ $c != 'q' ]]
do
        echo "read <$c>" # Replace this with code to handle the characters read
done < /dev/ttyUSB0

If -F does not work, redirect stdin to the device.

For your output problem, I think it is working but you are seeing the characters displayed as characters and not the hex-codes. To see the hex codes (for verification testing only - I don't think you want to send the hex codes to the terminal), try:

echo -en '\xAA\x04\xC2' | od -tx1

You may also want to have raw mode set when outputting to avoid the driver changing the output characters.

rghome
  • 8,529
  • 8
  • 43
  • 62
  • Thanks for your reply. I have tried your suggestion of od -tx1 but it is not giving me any response. I am sending: echo -en '\xAA\x04\xC2' | od -tx1 > /dev/ttyUSB0. Can you tell me the complete command to send. Thanks – Aircraft Feb 28 '15 at 06:23
  • The code which you have given me is giving error: stty: invalid argument `-F' – Aircraft Feb 28 '15 at 09:52
  • Try redirecting from stdin instead – rghome Feb 28 '15 at 10:24
  • Can you provide me the code pls. I want to send 0xAA,0x04,0xC2,0x90 and output I will get is : 0xAD. Thanks in advance – Aircraft Mar 01 '15 at 08:21
  • 1
    The code which you have given me is giving output like this: read <�> when I have tested the same command in terminal, I get "M--" which you said is ascii(I can deal with M--), but now what is this "read <�>". Please help in decoding this. Is there a way in which I can check that my hex values has been sent to serial device properly. – Aircraft Mar 01 '15 at 08:52
  • My code is just an example: as you can see on line 5 there is an echo statement that outputs "read <$c>" where $c is the character it read. You need to replace that with the code that does what you want to do with the input received. For checking what is sent - the only thing you can do is pipe the output into `od` and see what it says. – rghome Mar 01 '15 at 23:09
  • Thanks I have done everything and I am getting the data in hex format but the problem with your example code is that it is responding line by line, for example: read read <06> read <30> I want to store it in array, for example: arry[]= AA,06,30. How to do this? – Aircraft Mar 02 '15 at 05:19
  • I also want to know that how can I check for EOF. I mean when I have received all the input it should move out of the loop. How to do this.? – Aircraft Mar 02 '15 at 08:15
  • Assuming you are using `bash` you can append to an array `a` using `a+=$c` - see this link: [link](http://stackoverflow.com/questions/1951506/bash-add-value-to-array-without-specifying-a-key) – rghome Mar 02 '15 at 08:28
0

I've found another method I'd like to share:

echo "blabla" > /dev/ttyx & answer=$(head -n1 /dev/ttyx)

The & puts the transfer from echo to the port immediately in the background, while head waits until it gets one line as an answer, which is then put into a variable.

Braiam
  • 1
  • 11
  • 47
  • 78
m00ny
  • 1