我正在尝试连接覆盆子和类似arduino(crius aio)的设备。 Crius每秒写入5个字节(每个传输8个字节,115200波特率,1个停止位,无奇偶校验位,LSB先发送,非反转字节),我可以在逻辑分析仪上看到它们,所以我确信字节是正确发送的。所以我试图使用termios将这些字节读取到raspberry。
int serialport_init(const char* serialport)
{
fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (fd == -1)
{
perror("open_port: Unable to open ttyAMA0\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC );
tcgetattr(fd,&termAttr);
termAttr.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
termAttr.c_iflag = IGNPAR | ICRNL;
termAttr.c_oflag = 0;
termAttr.c_lflag = ICANON; //Input mode=canonical + no echo (wait for CR)
termAttr.c_cc[VTIME] = 0; //Inter-char timer.
termAttr.c_cc[VMIN] = 1 ; //Block until n bytes are received
//May not work (GNU/Linux 2.6.35)
tcflush(fd, TCIOFLUSH);
if( tcsetattr(fd, TCSANOW, &termAttr) == -1)
{
printf("termAttr ERROR\n");
return -1;
}
printf("UART1 configured....\n");
usleep(2000000); //delay 2 seconds and then return
return 0;
}
void signal_handler_IO (int status)
{
static uint8_t b = 0;
if(read(fd, &b, 1)>0)
printf("received data from UART...: %d\n", b);
b++;
}
当我在信号处理程序上发表if语句时,我的代码工作正常。当我取消注释该行时,我的代码在打印“UART1已配置......”后停止。 任何提示?