我寫在需要Ç - 選擇()似乎阻塞長於超時
等待串行使用select()
讀取串行數據(RS232數據採訪節目115200波特),
時間戳它(clock_gettime()),
讀取的ADC上SPI,
解釋它,
在另一個tty設備
循環發送新數據,並重復
的ADC是無關緊要的了。
在循環結束時,我再次使用select()和0超時輪詢並查看數據是否已經可用,如果這意味着我有溢出,即,即。我期望循環在更多數據之前結束,並且在循環開始時select()會在循環開始時阻塞並在它到達時立即獲取它。
數據應該每隔5ms到達一次,我的第一個select()超時計算爲(5.5ms - 循環時間) - 應該是4ms。
我沒有超時但很多超時。
檢查時間戳顯示select()阻塞超過超時(但仍返回> 0)。 它看起來像select()在超時之前獲取數據後返回。
1000次重複可能發生20次。 可能是什麼原因?我如何解決它?
編輯: 這裏被砍倒的代碼版本(!我做更多的錯誤檢查比這個)
#include <bcm2835.h> /* for bcm2835_init(), bcm2835_close() */
int main(int argc, char **argv){
int err = 0;
/* Set real time priority SCHED_FIFO */
struct sched_param sp;
sp.sched_priority = 30;
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp)){
perror("pthread_setschedparam():");
err = 1;
}
/* 5ms between samples on /dev/ttyUSB0 */
int interval = 5;
/* Setup tty devices with termios, both totally uncooked, 8 bit, odd parity, 1 stop bit, 115200baud */
int fd_wc=setup_serial("/dev/ttyAMA0");
int fd_sc=setup_serial("/dev/ttyUSB0");
/* Setup GPIO for SPI, SPI mode, clock is ~1MHz which equates to more than 50ksps */
bcm2835_init();
setup_mcp3201spi();
int collecting = 1;
struct timespec starttime;
struct timespec time;
struct timespec ftime;
ftime.tv_nsec = 0;
fd_set readfds;
int countfd;
struct timeval interval_timeout;
struct timeval notime;
uint16_t p1;
float w1;
uint8_t *datap = malloc(8);
int data_size;
char output[25];
clock_gettime(CLOCK_MONOTONIC, &starttime);
while (!err && collecting){
/* Set timeout to (5*1.2)ms - (looptime)ms, or 0 if looptime was longer than (5*1.2)ms */
interval_timeout.tv_sec = 0;
interval_timeout.tv_usec = interval * 1200 - ftime.tv_nsec/1000;
interval_timeout.tv_usec = (interval_timeout.tv_usec < 0)? 0 : interval_timeout.tv_usec;
FD_ZERO(&readfds);
FD_SET(fd_wc, &readfds);
FD_SET(0, &readfds); /* so that we can quit, code not included */
if ((countfd=select(fd_wc+1, &readfds, NULL, NULL, &interval_timeout))<0){
perror("select()");
err = 1;
} else if (countfd == 0){
printf("Timeout on select()\n");
fflush(stdout);
err = 1;
} else if (FD_ISSET(fd_wc, &readfds)){
/* timestamp for when data is just available */
clock_gettime(CLOCK_MONOTONIC, &time)
if (starttime.tv_nsec > time.tv_nsec){
time.tv_nsec = 1000000000 + time.tv_nsec - starttime.tv_nsec;
time.tv_sec = time.tv_sec - starttime.tv_sec - 1;
} else {
time.tv_nsec = time.tv_nsec - starttime.tv_nsec;
time.tv_sec = time.tv_sec - starttime.tv_sec;
}
/* get ADC value, which is sampled fast so corresponds to timestamp */
p1 = getADCvalue();
/* receive_frame, receiving is slower so do it after getting ADC value. It is timestamped anyway */
/* This function consists of a loop that gets data from serial 1 byte at a time until a 'frame' is collected. */
/* it uses select() with a very short timeout (enough for 1 byte at baudrate) just to check comms are still going */
/* It never times out and behaves well */
/* The interval_timeout is passed because it is used as a timeout for responding an ACK to the device */
/* That select also never times out */
ireceive_frame(&datap, fd_wc, &data_size, interval_timeout.tv_sec, interval_timeout.tv_usec);
/* do stuff with it */
/* This takes most of the time in the loop, about 1.3ms at 115200 baud */
snprintf(output, 24, "%d.%04d,%d,%.2f\n", time.tv_sec, time.tv_nsec/100000, pressure, w1);
write(fd_sc, output, strnlen(output, 23));
/* Check how long the loop took (minus the polling select() that follows */
clock_gettime(CLOCK_MONOTONIC, &ftime);
if ((time.tv_nsec+starttime.tv_nsec) > ftime.tv_nsec){
ftime.tv_nsec = 1000000000 + ftime.tv_nsec - time.tv_nsec - starttime.tv_nsec;
ftime.tv_sec = ftime.tv_sec - time.tv_sec - starttime.tv_sec - 1;
} else {
ftime.tv_nsec = ftime.tv_nsec - time.tv_nsec - starttime.tv_nsec;
ftime.tv_sec = ftime.tv_sec - time.tv_sec - starttime.tv_sec;
}
/* Poll with 0 timeout to check that data hasn't arrived before we're ready yet */
FD_ZERO(&readfds);
FD_SET(fd_wc, &readfds);
notime.tv_sec = 0;
notime.tv_usec = 0;
if (!err && ((countfd=select(fd_wc+1, &readfds, NULL, NULL, ¬ime)) < 0)){
perror("select()");
err = 1;
} else if (countfd > 0){
printf("OVERRUN!\n");
snprintf(output, 25, ",,,%d.%04d\n\n", ftime.tv_sec, ftime.tv_nsec/100000);
write(fd_sc, output, strnlen(output, 24));
}
}
}
return 0;
}
時間戳我的串行流看到我的輸出是相當規則(偏差通常會被下一個循環追上)。一段輸出:
6.1810,0,225.25
6.1867,0,225.25
6.1922,0,225.25
6,2063,0,225.25
,,,0.0010
在這裏,一切都很好。下一個樣例是6.2063 - 最後一個14.1ms,但它沒有超時,6.1922-6.2063的前一個循環也沒有超出輪詢select()。我的結論是,最後一個循環與採樣時間有關,並且選擇花費了-10毫秒太長時間返回而沒有超時。
,,, 0.0010表示循環之後的循環時間(ftime) - 我真的應該檢查循環時間是什麼時候出錯的。我明天會試試。
你是如何設置你的掩碼?,並且你是否爲time參數傳遞一個空指針,或者將struct的值設置爲零。每個都會導致它自己的行爲。 – ryyker
您可能需要包含一個_ [SSCCE](http://sscce.org/)_來進一步說明問題。顯然,超時問題可能會導致您的問題。這也將吸引更多的觀衆。 – ryyker
將Raspberry-pi添加到標籤。 – ryyker