Pregunta

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.

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top