Ser*_*nko 0 c++ linux ubuntu serial-port
我正在尝试使用串行连接的read()函数.
我使用以下设置初始化串口:
bool HardwareSerial::begin(speed_t speed) {
int USB = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (USB == 1) {
std::cout << "\n Error! in Opening ttyUSB0\n" << std::endl;
} else {
std::cout << "\n ttyUSB0 Opened Successfully\n" << std::endl;
}
struct termios tty;
struct termios tty_old;
memset(&tty, 0, sizeof tty);
// Error Handling
if (tcgetattr(USB, &tty) != 0) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}
//Save old tty parameters
tty_old = tty;
// Set Baud Rate
cfsetospeed(&tty, (speed_t) speed);
cfsetispeed(&tty, (speed_t) speed);
// Setting other Port Stuff
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
// Make raw
cfmakeraw(&tty);
//Flush Port, then applies attributes
tcflush(USB, TCIFLUSH);
if (tcsetattr(USB, TCSANOW, &tty) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}
_USB = USB;
return true;
}
Run Code Online (Sandbox Code Playgroud)
然后我定期调用类成员read()函数调用流读取:
int HardwareSerial::read() {
int n = 0;
char buf;
n = ::read(_USB, &buf, 1);
std::cout << std::hex << static_cast<int> (buf) << " n:";
std::cout << n << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当端口接收数据时read()按预期工作并打印传入的字节.但是如果我停止发送字节,程序会挂起,直到某些字节没有收到.我希望:: read将返回0,但它不会返回任何内容并等待传入的数据.收到新数据后,程序继续工作,:: read返回1;
那么我在配置中错过了什么?我尝试了不同的VMIN和VTIME但结果是一样的.
您正在以阻塞方式从USB读取数据,例如,如果没有可用数据,则呼叫被阻止,并且在数据到达之前,进程将不会取得任何进展.
可以做到,您可以将描述符设置为读入NON-BLOCKING模式,这些内容如下:
int flags = fcntl(_USB, F_GETFL, 0);
fcntl(_USB, F_SETFL, flags | O_NONBLOCK)
Run Code Online (Sandbox Code Playgroud)
现在,你会尝试阅读,你可以这样做:
int count;
char buffer;
count = read(_USD, buf, 1);
// Check whenever you succeeded to read something
if(count >=0) {
// Data is arrived
} else if(count < 0 && errno == EAGAIN) {
// No Data, need to wait, continue, or something else.
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用select函数来检查设备描述符何时可以读取.
| 归档时间: |
|
| 查看次数: |
533 次 |
| 最近记录: |