3
\$\begingroup\$

Does anybody know the magic command to set baud rate, parity, flow control, etc. for an RS-232 port on the MacOS X CLI?

\$\endgroup\$
1

2 Answers 2

5
\$\begingroup\$

To read from my arduino or attiny, I use this simple program:

#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main(int argc, char **argv) {

    int fd;
    ssize_t len;
    unsigned char buf[8192];
    struct termios tio;

    fd = open( argv[1], O_RDONLY | O_NOCTTY | O_NONBLOCK );
    cfmakeraw(&tio);
    cfsetispeed(&tio,B9600);
    cfsetospeed(&tio,B9600);
    tcsetattr(fd,TCSANOW,&tio);

    for(;;) {
        len = read( fd, &buf[0], 8192 );
        if( len > 0 ) write(1,buf,len);
        sleep(1);
    }

}

On the mac, using the FTDI chip (Arduino Duemilanove) for serial means that the stty command has no lasting effect. Once the port is closed, it reverts to default parameters. While this program is running, you can use stty to alter parameters.

Here is the default:

$ stty -f /dev/tty.usbserial-* -a
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb

Then if the C program is running:

$ stty -f /dev/tty.usbserial-A6003SHf -a
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel ignbrk -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb -hupcl
discard dsusp   eof     eol     eol2    erase   intr    kill    lnext   
^@      ^@      ^@      ^@      ^@      ^@      ^@      ^@      ^@      
quit    reprint start   status  stop    susp    werase  
^@      ^@      ^@      ^@      ^@      ^@      ^@      

You can change the baud rate if for instance the Arduino is using Serial.begin(19200) for faster transmission.

$ stty -f /dev/tty.usbserial-* 19200
$ stty -f /dev/tty.usbserial-*
speed 19200 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel ignbrk -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb -hupcl
discard dsusp   eof     eol     eol2    erase   intr    kill    lnext   
^@      ^@      ^@      ^@      ^@      ^@      ^@      ^@      ^@      
quit    reprint start   status  stop    susp    werase  
^@      ^@      ^@      ^@      ^@      ^@      ^@      

Once the C program is closed, the serial port reverts back to its default.

\$\endgroup\$
4
  • \$\begingroup\$ OK, slowly starting to make sense. So if I wanted to use your code to just write a utility in C to let me interact with my Arduino board, I'd create a command-line projected in XCode, based in C, and then paste that in. Compile it, then what would I do to run it? Just ./ProgramName /dev/tty.usbserial-XXX? \$\endgroup\$
    – Myk
    Oct 14, 2010 at 17:23
  • \$\begingroup\$ Hmm, after much googling around I may have figured this out. I found this: todbot.com/blog/2006/12/06/… Which I was able to modify to output the data I need, and compile. It seems to be working! :) \$\endgroup\$
    – Myk
    Oct 14, 2010 at 18:19
  • \$\begingroup\$ @Myk - To your first comment, yes. To your second comment, good to hear! You might want to read up/learn about the communication chain between the two ends (Mac <-> Arduino) and all the interfaces inbetween to ensure you have an idea of what's going on here. There's a lot of interfaces between the USB port and the Arduino to understand. :) I take it from your blog that you dabble in a lot of different technological things. If you plan on doing more with a serial port in the future, I'd either find an expert to help, or learn up on this stuff more. \$\endgroup\$
    – J. Polfer
    Oct 14, 2010 at 20:21
  • \$\begingroup\$ Yeah, I'm mostly a flash/flex guy - I'm just trying to find a way to build a reusable Arduino -> Flash/Flex bootstrapper for use on a project I'm doing. Generally speaking the established practice is to run something like tinkerproxy or serproxy and just use local socket connections to interface with Arduino - but with AIR2 we have the ability to run native processes through ActionScript, so that's what birthed this experiment. Thanks! \$\endgroup\$
    – Myk
    Oct 14, 2010 at 20:33
2
\$\begingroup\$

I don't know in bash (or whatever shell OS X uses), but it's probably achievable in Python with the pyserial library.

Update: How about this answer on StackOverflow: https://stackoverflow.com/questions/3918032/bash-serial-i-o-and-arduino/3926278#3926278

Oh, wait, you wrote that. Does it work?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Yeah. I'm asking around. I'm like 80% sure stty will work - it's the traditional UNIX way of doing it. But I was hoping someone who uses OS X and would know for sure. :) \$\endgroup\$
    – J. Polfer
    Oct 13, 2010 at 17:34
  • \$\begingroup\$ Ah, I see. Very thorough. \$\endgroup\$
    – pingswept
    Oct 13, 2010 at 17:35
  • \$\begingroup\$ Thanks for all your help, @sheep! I'm still having trouble with this and am beginning to think I'm just insane. Even using the various stty arguments, I put those in and it just hangs. I feel like there's some fundamental paradigm I'm not getting. >< \$\endgroup\$
    – Myk
    Oct 14, 2010 at 17:26

Your Answer

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

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