0

I have an Arduino connected to my USB port (on my Linux based OS) at /dev/ttyACM0 which is listening for serial data to be read and processes them when received. All I need to do is send data to this port. I have used the program minicom with the following command:

minicom --device=/dev/ttyACM0 --baudrate=9600

and it works perfectly. The problem is that minicom runs in an interactive environment and I wish to write a shell script which sends information non interactively, something like

minicom --device=/dev/ttyACM0 --baudrate=9600  "message"

From what I understand, everything in Unix is a file - even USB ports - so I was wondering if it would be possible to simply echo the bytes directly to the device file:

echo "message" >/dev/ttyACM0

But this did not work. I realized this might have something to do with mismatch in Baud rates. I ran the following command

$ stty -F /dev/ttyACM0
speed 9600 baud; line = 0;
min = 0; time = 0;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

But the baud rate is at 9600. Is there any way I can get this to work with the above command?

2
  • 1
    Use stty to change to the correct baud rate, and then echo will work as long as nothing resets the baud rate.
    – user10489
    Commented Jul 17, 2022 at 21:23
  • Try with the arduino software. Works with my devices...
    – kanehekili
    Commented Jul 17, 2022 at 22:27

1 Answer 1

1

I don't know if you got the answer to your question, as this thread is more than a year old.

But I guess you could get away with your script by using EXCEPT wich is a scripting language that has its own shell, and can automate interaction between tty and the user.

    #!/usr/bin/expect -f
    set timeout -1
    spawn ./que.sh
    expect "Enter your name\r"
    send -- "I am Nikhil\r"
    expect "Enter your age\r"
    send -- "24\r"
    expect "Enter your salary\r"
    send -- "100k\r"
    expect eof

The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout.

Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.

The spawn command is used to start a script or a program like the shell, FTP, Telnet, SSH, SCP, and so on.

The remaining lines are the Expect script that interacts with our shell script.

The last line is the end of file which means the end of the interaction.

Now you only need to save your script and make the file executable, just like any other shell script.

The usage is described by typing man expect from the shell; alternatively, the following page explains it in detail:

https://www.geeksforgeeks.org/expect-command-in-linux-with-examples/

Hope I could help.

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – LincolnP
    Commented Oct 15, 2023 at 17:30
  • @LincolnP I think in this case, though, the information in the link is just over and above what is needed. The man expect will have all the necessary information even if the link gets outdated.
    – Alex
    Commented Oct 25, 2023 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .