为什么重启后串口读取不工作
Why serial port reading is not working after reboot?
从串行端口读取时出现问题。
问题是当我启动计算机时,应用程序没有接收到数据。如果我使用像jpnevulator
这样的工具,我可以看到数据来了。在我杀死jpnevulator
之后,我也可以从我的应用程序中读取数据。只有当我尝试在重启后运行应用程序时,它才会工作,直到我首先用其他工具(如jpnevulator
)读取一些数据。
应用程序很大,所以我将只放置我认为相关的代码部分。
bool CSerial::SetPort(char *port){
this->port = (char*) malloc(strlen(port));
strcpy(this->port, port);
return true;
}
bool CSerial::Setup(){
if(!Open()){
return false;
}
tcgetattr(fdPort, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
if(tcsetattr(fdPort, TCSANOW, &options)){
return false;
}
return true;
}
bool CSerial::Open(){
this->fdPort = open(this->port, O_RDWR | O_NOCTTY | O_NDELAY /*| O_NONBLOCK*/ );
if(fdPort == -1){
return false;
}
else{
//fcntl(fdPort, F_SETFL, 0);
fcntl(fdPort, F_SETFL, FNDELAY);
}
return true;
}
bool CSerial::ReadData(uint8_t *data, uint32_t length, int32_t *receivedDataBytes){
int32_t temp_receivedDataBytes = -1;
fd_set readFd;
FD_ZERO(&readFd);
FD_SET(fdPort, &readFd);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 50000;
int test;
if(fcntl(fdPort, F_GETFD) != -1){
while(test = select(fdPort + 1, &readFd, NULL, NULL, &timeout) == 1 && temp_receivedDataBytes == -1){
FD_ZERO(&readFd);
FD_SET(fdPort, &readFd);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 50000;
if(FD_ISSET(fdPort, &readFd))
temp_receivedDataBytes = read(fdPort, data, length);
else continue;
}
if(temp_receivedDataBytes < 0){
return false;
}
if(temp_receivedDataBytes > 0){
*receivedDataBytes = temp_receivedDataBytes;
return true;
}
}
return false;
}
我设法解决了这个问题。问题是jpnevulator
在使用它进行阅读时为我设置端口,然后为我的应用程序保留设置。
为了解决这个问题,我设置了几乎所有的串口。
bool Serial::Setup(){
if(!openPort()){
return false;
}
tcgetattr(fdPort, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
options.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);
options.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
if(tcsetattr(fdPort, TCSANOW, &options)){
return false;
}
return true;
}
相关文章:
- 我的文件无法正常工作,无法从C++文件中读取上下文
- 尝试读取/写入Graphviz DAG值的工作证明会导致segfault
- 读取二进制文件大小的短自上周以来停止工作
- Linux C++读取 UART 设备无法始终如一地工作
- char* 如何读取十六进制的这个整数?它在 ostream::write() 中是如何工作的
- 从工作目录 QtWebEngine 读取文件
- 为什么将uint8_t读取为十六进制没有按预期工作?
- ifstream读取二进制文件在发行模式下工作,但在调试模式下不使用
- C++程序在 freopen 中读取大文本后停止工作
- 读取进程内存无法正常工作,使用 UTF16 字符串
- 当写和读在不同的文件中时,C++二进制读取会产生segfault,但放在一个文件中的相同代码可以工作
- 我无法获得基于读取字符数组的算法来工作
- CMD窗口停止工作,文本文件使用fstream读取错误
- USB串行虚拟COM端口:读取不工作,但写入工作
- 似乎没有任何读取文件的方法可以工作,所有方法都不返回任何东西-C++
- 从文件 C++ 程序读取停止工作
- 正在从工作目录C++中的文件夹中读取
- c++中的文件读取:for和while嵌套循环没有按预期工作:串行
- C++:使用get()读取文件;没有按预期工作
- std::getline() 访问冲突,以前可以工作,但现在无法从任何文件中读取