2

I'm writing some C code to get the message from arduino, and the port i'm using is tty.usbmodem1411 which works well to burn the code to arduino board. However, while writing the C code

#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int main(){
    int fd = open("/dev/tty.usbmodem1411", O_RDWR);
    printf("open\n");

This snap of code can be compiled but while running, even the "open" cannot be displayed. The program is stuck on the open line.

I use command chmod 777 and chown and chgrp to change the permission of the file, but it still doesn't work. And also I cannot use command

cat /dev/tty.usbmodem1411

it just blocked and nothing happened. I have checked online and changed the port to cu.usbmodem1411 which seems the same.

Anyone has some ideas? Thank you.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
stcheng
  • 133
  • 5
  • 17
  • have you tried other functions for open? open should be error checked. if(open("file",O_RDWR) == NULL) – hit Jun 18 '13 at 00:32
  • Are sure the port is not used by other application when you are trying to open it. Also add a check on fd to what it the error. – praks411 Jun 18 '13 at 11:10

1 Answers1

2

well, a serial port is not like any file. There are a bunch of controls you have to setup on the file so you can set it up correctly (remember the parity/speed etc..?). You can do that using termios settings, Here's the first result on stackoverflow:

or not. Here's a simple code I hacked a while back to flash a serial device:

I had to patch a few stuff from the original project to make it work on OSX, it may be useful for you.

And to open the tty chardev, you shall not use cat, that would only output stuff as fast as it can gets it (which in best case outputs nothing, in worst case scrambles your terminal). Instead you shall use:

  • minicom (but a bit weird to use/configure)
  • screen /dev/tty.usbmodem1411 115200
  • python -m serial.tools.miniterm /dev/tty.usbmodem1411 115200
Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90
  • thanks so much. your help lets me know many more things. now the tty port still doesn't work but cu port actually runs. i've looked up some information on the difference between them. so currently for arduino, it is no difference of using either port. yes? – stcheng Jun 18 '13 at 23:16
  • indeed, there's no difference between an Arduino serial connection and any other serial connection. – zmo Jun 19 '13 at 08:18
  • what's the difference between cu and tty. i consider there's no difference here. yes? – stcheng Jun 19 '13 at 19:59