
/dev/ttyUSB0에 연결된 GPS 장치가 있습니다. 데이터를 읽기 위한 간단한 코드를 작성했지만 읽기가 항상 실패합니다. 문제가 어디에 있는지 모르겠습니다. 이것이 제 코드입니다. 도와주실 수 있나요 :).
#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();
}
답변1
? /
앞에 a가 없기 때문인 것 같습니다 .dev
fd = open("/dev/ttyUSB0" , O_RDWR | O_NOCTTY | O_NDELAY);
내가 당신이라면 이것에 대한 변수를 만들 것입니다
/dev/ttyUSB0
이렇게 하면 open
및 에서 error
동일한 파일 이름을 표시할 수 있습니다. 혼란을 줄여줍니다.