当前位置: 技术问答>linux和unix
如何使用select实时监听串口通信?
来源: 互联网 发布时间:2016-05-09
本文导语: 如题,想在实时监听2440板子的串口数据,不知道用select怎么写? | INT32 ReadComPort (INT32 ComPort, void *data, INT32 datalength) { INT32 retval = 0; FD_ZERO (&fs_read); FD_S...
如题,想在实时监听2440板子的串口数据,不知道用select怎么写?
|
INT32 ReadComPort (INT32 ComPort, void *data, INT32 datalength)
{
INT32 retval = 0;
FD_ZERO (&fs_read);
FD_SET (fd[ComPort], &fs_read);
tv_timeout.tv_sec = 0;//TIMEOUT_SEC (datalength, GetBaudrate ());
tv_timeout.tv_usec = 150000;//TIMEOUT_USEC;
retval = select (fd[ComPort] + 1, &fs_read, NULL, NULL, &tv_timeout);
if (retval)
return (read (fd[ComPort], data, datalength));
else
return (-1);
}
{
INT32 retval = 0;
FD_ZERO (&fs_read);
FD_SET (fd[ComPort], &fs_read);
tv_timeout.tv_sec = 0;//TIMEOUT_SEC (datalength, GetBaudrate ());
tv_timeout.tv_usec = 150000;//TIMEOUT_USEC;
retval = select (fd[ComPort] + 1, &fs_read, NULL, NULL, &tv_timeout);
if (retval)
return (read (fd[ComPort], data, datalength));
else
return (-1);
}