Using Terminal for Serial Commands

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: Using Terminal for Serial Commands

Post by westfw »

You should be able to do something like:

Code: Select all

echo "a" >/dev/tty.usbmodemb21
Note that this gives you no control over things like the bitrate, which will probably remain whatever it was when the arduino IDE last accessed it (115200bps for the sketch upload?)

Note, however, that the process of opening /dev/tty.xxx will cause an auto-reset sequence on most modern arduinos, essentially unavoidable. If you want to send occasional data strings to a running sketch, you'll need to disable auto-reset, or find a mac-side program that has additional capabilities (like keeping the port half open, or incorporating intelligence that allows delays and or checking for appropriate responses.)

User avatar
cstratton
 
Posts: 294
Joined: Wed Sep 29, 2010 3:52 pm

Re: Using Terminal for Serial Commands

Post by cstratton »

ckeyes wrote:Simply send the Arduino an "a".
Why do you need to do this from a terminal and not from the gui?

If it's because you are doing it in a shell script, then you don't want these programs anyway. Just

/usr/bin/echo -n 'a' > /dev/ttywhatevermacscallit

Specifying the full path (it may be something slightly different on a mac) of echo is to avoid getting a shell built-in which may have different behavior. The -n is to tell it not to append a newline.

If you want that to work when you haven't used the serial port with the gui since boot, you'll probably also need stty to set the baud rate. You may also have to clear some other bits to avoid automatically resetting the arduino to its bootloader every time you open the port.

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Using Terminal for Serial Commands

Post by ckeyes »

I'm triggering the Arduino from a remote machine using an AppleScript.
Makes no difference to me what app is used to send the command to the Arduino
as long as it can be scripted.

I got this command to get some response from the Arduino:
arduino-serial -b 9600 -p /dev/tty.usbserial -s a
but all it does is flash the LED a few times, which isn't what
I have programmed in the sketch to do when an "a" is received.

Entering an "a" in the Arduino's serial monitor works just fine.

Thanks,

Carl

User avatar
cstratton
 
Posts: 294
Joined: Wed Sep 29, 2010 3:52 pm

Re: Using Terminal for Serial Commands

Post by cstratton »

Glad you seem to have gotten beyond the command not found.

I'd suspect auto reset to be the current culprit, but I'm going to disagree with westfw in that I think it can be avoided with host side software by getting the right mode bit settings/clearings - I had to do this recently with a similar issue for a different board under linux, and will try to get back to you with the (hopefully portable) specifics.

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Using Terminal for Serial Commands

Post by ckeyes »

Thanks, I have tried adding a delay: arduino-serial -b 9600 -p /dev/tty.usbserial -d 2000 -s a
but still just the quick flashing LED.

Carl

User avatar
cstratton
 
Posts: 294
Joined: Wed Sep 29, 2010 3:52 pm

Re: Using Terminal for Serial Commands

Post by cstratton »

It occurs to me that it also could be a failure to set the right baud rate.

For the auto reset, I was recommending clearing the HUPCL bit to stop dtr from twiddling when the port is opened/closed and potentially (depending on what logical signal the 8u2 uses to trigger reset) resetting the arduino. I believe this can be done with

stty devicefile -hupcl

but have only personally done it from within a C program that was downloading code into a different type of board which I had wired to use DTR to reset to a bootloader.

It occurs to me that a test which you could do would be to write a sketch which ignores the serial port but just does something consistent with one of the I/O pins - a blink sketch, for example. If you run the terminal commands to try to send serial, there should be no change in the operation of that sketch. But if the arduino is getting reset, then the sketch would at least briefly stop running, and you would know that unintended reset was a problem that needed to be solved.

Or if what you sketch needs to do is extremely simple, perhaps you could just make being reset by the mac the trigger?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”