11

I want to make a shell or C script that interprets the Arduino serial input to run commands. For that, I will need to make that script be the Serial monitor for my arduino. Another reason I want to use scripts or the command line as a Serial I/O is because the Arduino monitor function freezes the IDE.

My arduino's serial port is /dev/tty.usbmodem1d112

This is what I have tried so far:

  1. cat /dev/tty.usbmodem1d112 < This returns absolutely nothing

  2. screen /dev/tty.usbmodem1d112 < This Prints output, but has problems with input.

  3. cat < /dev/tty.usbmodem1d112 < Same result as cat without the < symbol.

Sometimes, I also get annoying Resource Busy messages. This makes me re-plug the Arduino board.


OS X (10.9.5) Arduino IDE latest version. Arduino M0 pro board.

6
  • 1
    On linux, I usually have to configure the device with stty to raw mode or whatever I want first, but I'm not sure how portable that is.
    – BrettAM
    Dec 26, 2015 at 1:17
  • 1
    screen /dev/SomeUSBSerial SerialSpeed works fine here on Debian with CP2102, FTDI232, Atmega32u4 (Leonardo), Atmega8U2 (serial chip of UNO-R2)... input and output are ok.
    – gone
    Dec 26, 2015 at 4:19
  • For what it is worth, the 1st and 3rd versions of your command are identical in function. In the 1st, cat uses the argument as the file to display. In the 2nd, the shell sets the standard input for cat to the file. The net result of both is that cat reads from the file and writes to standard output. The < and > operators are used to redirect the input and output of a program.
    – dlu
    Dec 26, 2015 at 14:21
  • 2
    Have you tried miniterm.py? It works really well in Ubuntu as serial monitor with Arduino boards. Dec 27, 2015 at 22:06
  • Yes, miniterm really works very well on Ubuntu and faster than screen, minicom, cu, etc. helpmanual.io/help/miniterm.py
    – Nakamoto
    Dec 25, 2020 at 2:27

1 Answer 1

8

You'll need to condition the serial line with the stty command to get this to work. One of the easier ways to do it is to start by using the Serial Monitor and running the command:

stty -g < /dev/tty.usbmodem1d112

That will show you all of the settings that the Serial Monitor used to conditions the line. The -g flag to stty tells it to give you the results in a form that can be used as the arguments to another stty command. You could do this like this:

echo -n 'stty ' > stty.arduino ; sudo stty -g -f /dev/tty.usbmodem1d112 >> stty.arduino

chmod +x stty.arduino

The first line creates a shell script to condition the serial line. The second line makes it executable so that you can run it as a shell command – ./stty.arduino. The sudo runs the command as root (with elevated privilege) which allows you to open the port even though the Serial Monitor will already have it open.

If you want you could move the command into a directory on your $PATH (for example ~/bin) so that it can be found regardless of where your current directory is.

After getting set up you can see the output from Serial.println() on the Arduino by doing:

./stty.arduino

cat < /dev/tty.usbmodem1d112

Depending on what you are trying to do, you might find the expect or script commands helpful. Expect (man page) lets you automate interactions with a text based application or process. Script (man page) creates a transcript of your interactions on the command line with another program or process.

7
  • Strangely, running the stty -g < /dev/... acts like the command is running, but nothing happened for long. When I use the second command to make a shell script, upon execution, it freezes my Terminal command line.
    – John K
    Dec 26, 2015 at 9:04
  • Can you show the output you get from stty -g…? Are you running the command with -f and/or sudo? I'm very puzzled that the second command, chmod, would hang your shell – it should succeed (or fail) immediately.
    – dlu
    Dec 26, 2015 at 14:18
  • chmod was fine. The long command creating shell script works, but the execution of the sh hangs shell. I did try sudo, and -f, but it shows new line as if processing, but nothing seems to happen
    – John K
    Dec 26, 2015 at 14:29
  • When you say "nothing seems to happen" do you mean the command produces no output and you get your prompt back? Or, the prompt doesn't come back until you interrupt the command? Or, it appears to execute as you'd expect by the settings on the serial line are unchanged? Or,… Too many possible nothings :-)
    – dlu
    Dec 27, 2015 at 20:39
  • 1
    Would you try ./stty.arduino < /dev/tty.usbmodem1d112 and see if that works? Looking this over, I see that the command to condition the line doesn't specify the line – so it's going to act on your terminal, and that's almost certainly not what you want.
    – dlu
    Dec 27, 2015 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.