转:https://blog.csdn.net/zujipi8736/article/details/86606093
(等串口可以读取了,再读取串口的数据)
/**
功能:向串口接收一个字符
参数ch:待接收的字符
参数wait_usec:等待n微秒
返回值:成功读入返回 0x02
失败读入返回 0x03
*/
unsigned char uart_getchar(unsigned char *ch, unsigned int wait_usec)
{
struct timeval tv;
fd_set set;
FD_ZERO(&set);
FD_SET(uart_fd, &set);
memset(&tv, 0, sizeof(tv));
tv.tv_sec = 0;
tv.tv_usec = wait_usec;
if(select(uart_fd+1, &set, NULL, NULL, &tv)>0)
{
if(FD_ISSET(uart_fd, &set))
{
read(uart_fd, ch, 1);
return UART_GET_SUCCESS;
}
}
return UART_GET_FAIL;
}