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 
7
  • Could you add to your question the output of the following two commands: ls -l /dev/ttyACM0and id.
    – ojs
    CommentedDec 13, 2020 at 16:05
  • I added that to the question
    – Ramin
    CommentedDec 14, 2020 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
    CommentedDec 14, 2020 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
    CommentedDec 15, 2020 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
    CommentedDec 15, 2020 at 13:48

2 Answers 2

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.

.

0
    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.

    1
    • I want to have script to do something if command fails or pass. What can I do then?
      – Ramin
      CommentedDec 13, 2020 at 22:46

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.