[英]How to fix timeout on rs232 serial port?
從串口讀取時出現問題。 問題是,對於從rs232
端口讀取的最后2個字節(CRC字節),讀取將等待,直到timeval
設置的超時結束,然后返回。 在rs485
,使用與讀取相同的方法,read返回正常。 我在debbuging時看到的是,在rs485
上,在2個CRC字節之后有一個額外的字節值為FF
。 我找不到2之間的另一個區別。
以下是代碼的相關部分:
設置端口
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] = 0;
options.c_cc[VTIME] = 1;
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(this->fdPort, TCSANOW, &options)){
return false;
}
return true;
}
打開港口
bool Serial::openPort(){
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);
}
int status = 0;
status |= TIOCM_RTS;
ioctl(this->fdPort, TIOCMSET, &status);
return true;
}
等待框架,其中數據以塊的形式被解釋和讀取。
bool GlobalProtocol::WaitFrame(uint32_t timeOut) {
int32_t bytesRec = 0;
int32_t bytesToRec = 1;
int recPhase = GC_PHASE_START;
uint8_t *pData = m_RxBuff;
bool commSuccess = true;
m_LastError = boards::GCL_ERR_OK;
while (readData(pData, bytesToRec, &bytesRec, timeOut)) {
if (bytesRec) {
switch (recPhase) {
case GC_PHASE_START:
if (*pData != GC_START_FRAME_BYTE) {
continue;
}
recPhase++;
pData++;
break;
case GC_PHASE_DEST:
if (*pData != m_MasterAddr) {
commSuccess = false;
break;
}
recPhase++;
pData++;
break;
case GC_PHASE_SRC:
recPhase++;
pData++;
break;
case GC_PHASE_LEN_LO:
if (*pData < 2 || *pData > GC_MAX_COMM_DATA_LEN) {
commSuccess = false;
break;
}
recPhase++;
pData++;
break;
case GC_PHASE_LEN_HI:
if (*pData != 0) {
commSuccess = false;
break;
}
recPhase++;
pData++;
bytesToRec = m_RxBuff[GC_PHASE_LEN_LO];
break;
case GC_PHASE_DATA:
if (bytesRec != bytesToRec) {
commSuccess = false;
break;
}
recPhase++;
pData += bytesRec;
bytesToRec = 2;
break;
case GC_PHASE_CRC_LO:
if (bytesRec != bytesToRec) {
commSuccess = false;
break;
}
if (CheckCRC(m_RxBuff, m_RxBuff[GC_PHASE_LEN_LO] + GC_PHASE_DATA + sizeof(uint16_t))) {
m_RecAddr = m_RxBuff[GC_PHASE_SRC];
return true;
}
commSuccess = false;
break;
}
if (!commSuccess) break;
}
else break;
}
m_LastError = boards::GCL_ERR_ANSWERNOTREC;
return false;
}
閱讀完成的地方。
bool Serial::readData(uint8_t *data, uint32_t length, int32_t *receivedDataBytes, int32_t timeoutVal){
int32_t tempReceivedDataBytes = -1;
fd_set readFd;
FD_ZERO(&readFd);
FD_SET(fdPort, &readFd);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = timeoutVal * 1000;
int test = 0;
uint32_t bytesAvail = 0;
QTime timer;
timer.start();
if(fcntl(fdPort, F_GETFD) != -1){
while(((test = select(this->fdPort + 1, &readFd, NULL, NULL, &timeout)) == 1) && (tempReceivedDataBytes == -1)){
ioctl(fdPort, FIONREAD, &bytesAvail);
if(FD_ISSET(fdPort, &readFd) && bytesAvail >= length){
tempReceivedDataBytes = read(fdPort, data, length);
}
if(timer.elapsed() > (timeoutVal + 5)){ //fail-safe
logger[debug_log] << "TIMEOUT" << endl;
break;
}
}
if(test == -1)
logger[debug_log]<< strerror(errno) << endl;
if(tempReceivedDataBytes < 0){
return false;
}
if(tempReceivedDataBytes >= 0){
*receivedDataBytes = tempReceivedDataBytes;
return true;
}
}
return false;
}
如果我將超時設置為100毫秒,則等待100毫秒來讀取2個字節,如果我將其設置為10毫秒,則等待10毫秒來讀取2個字節。
我嘗試更改端口設置,但沒有成功。
我一直試圖解決這個問題,但沒有成功。
編輯:我應該添加ioctl(fdPort, FIONREAD, &bytesAvail);
將bytesAvail
設置為2,但是read
等待直到超時才能讀取它們。
編輯2:
這是一個超時設置為25秒的日志,因此您可以了解問題所在:
04/17/2014 15:51:50:584,Main board start:
04/17/2014 15:51:50:586,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:586,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:586,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:587,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:587,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:597,1 Received 110 Needed to receive 110
04/17/2014 15:51:50:597,1 Received 2 Needed to receive 2
04/17/2014 15:51:50:634,PID board start:
04/17/2014 15:51:50:635,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:635,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:635,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:635,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:635,1 Received 1 Needed to receive 1
04/17/2014 15:51:50:641,1 Received 70 Needed to receive 70
04/17/2014 15:52:15:647,0 Received 2 Needed to receive 2
04/17/2014 15:52:15:647,Set Leds board start:
04/17/2014 15:52:15:649,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:649,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:652,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:652,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:652,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:652,1 Received 2 Needed to receive 2
04/17/2014 15:52:15:652,1 Received 2 Needed to receive 2
04/17/2014 15:52:15:652,Get state board start:
04/17/2014 15:52:15:654,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:654,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:654,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:654,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:655,1 Received 1 Needed to receive 1
04/17/2014 15:52:15:657,1 Received 30 Needed to receive 30
04/17/2014 15:52:15:657,1 Received 2 Needed to receive 2
有問題的是PID板(在rs232上),其他的在同一個rs485上。
這里有一個超時設置為30毫秒(我不記得確切的值):
04/17/2014 15:08:08:045,Main board start:
04/17/2014 15:08:08:046,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:046,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:046,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:047,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:047,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:056,1 Received 110 Needed to receive 110
04/17/2014 15:08:08:056,1 Received 2 Needed to receive 2
04/17/2014 15:08:08:078,PID board start:
04/17/2014 15:08:08:079,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:079,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:079,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:079,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:079,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:094,1 Received 70 Needed to receive 70
04/17/2014 15:08:08:120,0 Received 2 Needed to receive 2
04/17/2014 15:08:08:120,Set Leds board start:
04/17/2014 15:08:08:122,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:122,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:122,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:122,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:122,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:122,1 Received 2 Needed to receive 2
04/17/2014 15:08:08:122,1 Received 2 Needed to receive 2
04/17/2014 15:08:08:123,Get state board start:
04/17/2014 15:08:08:124,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:124,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:124,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:125,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:125,1 Received 1 Needed to receive 1
04/17/2014 15:08:08:128,1 Received 30 Needed to receive 30
04/17/2014 15:08:08:128,1 Received 2 Needed to receive 2
編輯:我仍然無法弄清楚出了什么問題。 如果我刪除rs485上的額外字節,它是相同的。 這是另一個日志:
06/24/2014 12:57:01:923,Set Leds board start:
06/24/2014 12:57:06:701,Select value 1 Received: 1 Needed: 1 Bytes available: 9
06/24/2014 12:57:06:701,Select value 1 Received: 1 Needed: 1 Bytes available: 8
06/24/2014 12:57:06:702,Select value 1 Received: 1 Needed: 1 Bytes available: 7
06/24/2014 12:57:06:702,Select value 1 Received: 1 Needed: 1 Bytes available: 6
06/24/2014 12:57:06:702,Select value 1 Received: 1 Needed: 1 Bytes available: 5
06/24/2014 12:57:06:702,Select value 1 Received: 2 Needed: 2 Bytes available: 4
06/24/2014 12:57:06:752,Select value 0 Received: 2 Needed: 2 Bytes available: 2
06/24/2014 12:57:06:752,Get state board start:
06/24/2014 12:57:06:754,Select value 1 Received: 1 Needed: 1 Bytes available: 4
06/24/2014 12:57:06:754,Select value 1 Received: 1 Needed: 1 Bytes available: 3
06/24/2014 12:57:06:754,Select value 1 Received: 1 Needed: 1 Bytes available: 2
06/24/2014 12:57:06:754,Select value 1 Received: 1 Needed: 1 Bytes available: 1
06/24/2014 12:57:06:755,Select value 1 Received: 1 Needed: 1 Bytes available: 10
06/24/2014 12:57:06:758,Select value 1 Received: 30 Needed: 30 Bytes available: 30
06/24/2014 12:57:06:808,Select value 0 Received: 2 Needed: 2 Bytes available: 2
06/24/2014 12:57:06:886,Main board start:
06/24/2014 12:57:06:888,Select value 1 Received: 1 Needed: 1 Bytes available: 3
06/24/2014 12:57:06:888,Select value 1 Received: 1 Needed: 1 Bytes available: 2
06/24/2014 12:57:06:889,Select value 1 Received: 1 Needed: 1 Bytes available: 1
06/24/2014 12:57:06:890,Select value 1 Received: 1 Needed: 1 Bytes available: 23
06/24/2014 12:57:06:890,Select value 1 Received: 1 Needed: 1 Bytes available: 22
06/24/2014 12:57:06:898,Select value 1 Received: 110 Needed: 110 Bytes available: 113
06/24/2014 12:57:06:898,Select value 1 Received: 2 Needed: 2 Bytes available: 3
正如您所看到的,當可用字節數等於要讀取的字節數時,它會在讀取函數中等待,直到超時然后返回。
你用的是什么硬件? 真正的串口或USB適配器? 配置了一些FTDI usb到串行適配器,因此它們分批在usb上發送字節。 當端口完全加載時,它會加速傳輸,但它們的行為與您在處理幾個字節時一樣。
我設法修復它。 這是一個簡單的修復。
我必須替換while(((test = select(this->fdPort + 1, &readFd, NULL, NULL, &timeout)) == 1) && (tempReceivedDataBytes == -1))
with while( (tempReceivedDataBytes == -1) && ((test = select(this->fdPort + 1, &readFd, NULL, NULL, &timeout)) == 1))
。
如果串行緩沖區為空,則它會在select
中等待,直到超時,即使它讀取了某些內容。 如果在端口上有某些東西,它正在檢查while的第二部分並退出它。
設置VMIN的非零值,以便一旦接收到至少那么多字符就可以滿足讀取。
這意味着:
如果沒有緩沖數據,則對於某些數據,呼叫將等待超時
如果至少緩沖了VMIN字符,則調用將立即返回,而不等待超時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.