
Tengo un dispositivo GPS conectado a /dev/ttyUSB0. Escribí un código simple para leer datos pero la lectura siempre falla. No tengo idea de dónde está el problema. Este es mi código. ¿Podrían ayudarme? :).
#include<iostream>
#include<fstream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
using namespace std;
int read_port(void)
{
int fd;
fd = open("dev/ttyUSB0" , O_RDWR | O_NOCTTY | O_NDELAY);
if (fd ==-1)
{
perror("open_port: Unable to open /dev/ttyUSB0 - ");
}
char buffer[32];
int n = read(fd, buffer, sizeof(buffer));
if (n < 0)
fputs("read failed!\n", stderr);
return (fd);
}
int main()
{
read_port();
}
Respuesta1
Supongo que te falta un /
delante dev
.
fd = open("/dev/ttyUSB0" , O_RDWR | O_NOCTTY | O_NDELAY);
Si yo fuera tú, haría una variable para esto.
/dev/ttyUSB0
De esa manera, open
y error
pueden mostrar el mismo nombre de archivo. Ahorra confusión.