Serial port access in C
simo-morra opened this issue · 1 comments
Hi,
I'm implementing a HAL module and need to communicate with an arduino connected to the raspberry over usb. To test the communication, I wrote a simple C program (using termios.h and open/read/write for serial access) which sends a string to arduino. Arduino reads the message and sends it back. It all works well: I tested it with the arduino serial monitor and from a Linux pc.
So I compiled it for the raspberry (getting no errors or warnings) and installed it through adb push. This is the code to open the serial port:
int openPort() {
int fd;
struct termios options;
fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd != -1) {
fcntl(fd, F_SETFL, 0);
//serial port settings
tcgetattr(fd, &options);
cfsetispeed(&options, B9600); //input baud rate
cfsetospeed(&options, B9600); //output baud rate
options.c_cflag |= (CLOCAL | CREAD);
//set data format (8N1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input
options.c_oflag &= ~OPOST; //raw output
//confirm settings
tcsetattr(fd, TCSAFLUSH, &options);
}
return fd;
}
I tried using all of the port names available under /dev/tty* but none of them corresponds to arduino. So it's like android doesn't recognize that arduino is connected, even if it does turn on when I connect the cable.
Any ideas about what could be the problem (maybe some driver/kernel stuff?) and how to solve it?
I discovered that the raspberry actually recognizes the connected arduino, but as if it was an usb device (under /dev/bus/usb). So I tried to handle the communication via libusb, but it doesn't seem to work, because the arduino is indeed a serial device.
I think the problem is with some missing drivers that should recognize that arduino is a serial device, but I don't know what.