这是这个原始帖子的后续问题:
我根据接受的答案对我的代码进行了更改,终端现在设置为阻止和规范输入。现在read()
按预期工作,除了第一次读取(通常是一些额外的随机符号与一些好的数据混合)之外,我从激光测距仪得到单行数据,代表我期望看到的内容。
但是,我现在有一个问题,如果读取太多,激光测距仪会进入非工作状态,停止通信,只能通过重启电源来重置。设备上只有 4 次读取就发生了这种情况,而我无法进行超过 8 次读取。读取是一次全部还是跨越多个程序启动都没有关系。
int main(){
int uart0_filestream = -1;
int loop, i, sentBytes;
int isError, rx_length;
unsigned char rx_buffer[256];
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(uart0_filestream, F_SETFL, 0);
if(uart0_filestream == -1){
printf("ERROR: Unable to open UART\n");
checkError(errno);
exit(1);
}
struct termios options;
isError = tcgetattr(uart0_filestream, &options);
if(isError < 0){
printf("ERROR: tcgetattr failed.\n");
checkError(errno);
exit(1);
}
//set c ontrol options and baud
options.c_cflag |= (CLOCAL | CREAD);
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
//set 8 data bits, 1 stop bit, no parity
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//set input options
options.c_iflag |= (INPCK | ISTRIP); //strip parity
options.c_iflag &= ~(IXON | IXOFF | IXANY); //turn off software flow control
//set canonical input processing
options.c_lflag |= (ICANON | ECHO | ECHOE);
tcflush(uart0_filestream, TCIFLUSH);
isError = tcsetattr(uart0_filestream, TCSANOW, &options);
if(isError < 0){
printf("ERROR: tcsetattr failed.\n");
checkError(errno);
exit(1);
}
//read() successfully returns expected data, with the exception of the first
//read. Reading more than 6-8 times, regardless of same session or restarting
//program puts LRF in non-working state which requires a power cycle to fix
for(i=0; i<4;i++ ){
rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
if(rx_length < 0){
printf("ERROR: read() failed.\n");
checkError(errno);
}
if(rx_length > 0){
printf("rx_lentgh = %i:\t ", rx_length);
for(loop=0; loop<rx_length; loop++){
printf("%c", rx_buffer[loop]);
}
printf("\n");
}
}
//writing once has no affect, sending command has no affect
//looping the write, eventually 'something' gets to the LRF
//but it puts it in the same state as when reading too many
//times and have to power cycle
for(i = 0; i<8; i++){
sentBytes = write(uart0_filestream, "o\n", 2);
if(sentBytes < 2){
printf("ERROR: write() failed.\n");
checkError(errno);
}
}
close(uart0_filestream);
}
我不相信它是测距仪,因为我可以运行 minicom,它会成功地连续显示输出。
正如我的代码注释中所述,我在向激光测距仪写入任何命令时也遇到了麻烦。每个命令由一个字符组成,后跟一个新行。一次写入没有影响,但循环多次会使设备处于与多次读取相同的状态。
我已阅读Posix Serial Programming Guide,并尝试了不同的标志组合,我认为这些组合可能会影响读取或写入,但没有成功。