
/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
同じファイル名を表示できます。混乱を避けることができます。