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:
cat /dev/tty.usbmodem1d112
< This returns absolutely nothingscreen /dev/tty.usbmodem1d112
< This Prints output, but has problems with input.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.
1 Answer 1
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.
-
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– John K2015年12月26日 09:04:59 +00:00Commented Dec 26, 2015 at 9:04 -
Can you show the output you get from
stty -g...
? Are you running the command with-f
and/orsudo
? I'm very puzzled that the second command,chmod
, would hang your shell – it should succeed (or fail) immediately.dlu– dlu2015年12月26日 14:18:25 +00:00Commented 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 happenJohn K– John K2015年12月26日 14:29:22 +00:00Commented 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– dlu2015年12月27日 20:39:14 +00:00Commented Dec 27, 2015 at 20:39
-
1Would 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– dlu2015年12月27日 20:46:12 +00:00Commented Dec 27, 2015 at 20:46
stty
toraw
mode or whatever I want first, but I'm not sure how portable that is.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.cat
uses the argument as the file to display. In the 2nd, the shell sets the standard input forcat
to the file. The net result of both is thatcat
reads from the file and writes to standard output. The < and > operators are used to redirect the input and output of a program.