0
点赞
收藏
分享

微信扫一扫

select 实现对串口的读取

phpworkerman 2022-01-17 阅读 55
int  SerialPort::read_port(char *buf)
{
	int  result = 0;
	fd_set  rset;
	struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 100;

	FD_ZERO(&rset);
    FD_SET(fd, &rset);
    if(select(fd+1, &rset, NULL, NULL, &timeout) > 0)
	{
	    if (FD_ISSET(fd, &rset)) {
		    result = ::read(fd, buf, 100);				         	
        }
	}

    return result;
}


int  SerialPort::write_port(char *buf, unsigned len)
{
	int result = write(fd, buf, len);
	// Wait until all data has been written
	tcdrain(fd);

	return result;
}
举报

相关推荐

0 条评论