0

Hi I am using a raspberry pi and trying to write to a usb serial port in a bash script. I found echo would be the right way to do it. Also I want to test if the port is busy or not when writing. So I am using this script:

#! /bin/bash
if  echo -e "USB Command" >> /dev/ttyACM0 ; then
    echo "Success"
else
    echo "Fail"
fi

But the problem is it is always success even when there is no permission to access the port. Am I using the best way to write to port? How can I check if the write was succeeded? Thanks

Edit: Here is the output of:

ls -l /dev/ttyACM0

crw-rw---- 1 root dialout 166, 0 Dec 13 16:38 /dev/ttyACM0

Ramin
  • 3
  • Could you add to your question the output of the following two commands: ls -l /dev/ttyACM0and id. – ojs Dec 13 '20 at 16:05
  • I added that to the question – Ramin Dec 14 '20 at 20:37
  • As you can see then that device is part of the dialout group and the user you are using is most probably a member of that group which the command id should show. Can you confirm to us that your user is part of the dialout group? – ojs Dec 14 '20 at 20:54
  • Here is the id output: id uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),105(input),109(netdev),997(gpio),998(i2c),999(spi) – Ramin Dec 15 '20 at 10:43
  • Ok, so your user is part of the dialout group and that group has read and write access to that serial port. So why do you think you don't have write access to that serial port? – ojs Dec 15 '20 at 13:48
  • Well. I am asking if the firmware on connected device has some issue and I can not open the port to write or if it is already opened. How should I know if I had a successful write to device? – Ramin Dec 15 '20 at 19:37
  • Hmmm, serial communication does not work like ethernet connection, there is no SYN/ACK. So to know if you are getting some commands through you need some kind of device on the other end that can show you incoming traffic through the serial port. Have you had a look through this question: https://unix.stackexchange.com/questions/117037/how-to-send-data-to-a-serial-port-and-see-any-answer – ojs Dec 15 '20 at 20:45

2 Answers2

0

Something is broken for you:

echo Hello >> /dev/mem; echo $?
bash: /dev/mem: Permission denied
1

If you're talking about classic Unix permissions and you don't have them to access the device, the command should absolutely fail.

Use udev rules or echo 123 | sudo tee /dev/device to work around it.

0

Check your script. I think what you will want to do is test for the return code from the echo command.

For example, if you simply run the echo from the command prompt:

$ echo -e "USB Command" >> /dev/ttyACM0

End then immediately check the return code:

$ echo $?
0

You should be able to determine success. At least the success of putting data on the tty.

A return code of 0 means success. Anything else is an error and you can probably google for the non-zero error code to see what it means.

Get that working manually and then see how to incorporate it into your script.

.

mikem
  • 845