本帖最后由 hidiy 于 2010-06-25 19:07 编辑 项目有个需求:通过板载串口向外部设备发送一条指令,然后等待外部设备响应返回。 所以希望通过设置select,当有数据可读取时,读取数据。 可是设置了select后发现即使串口上没有任何设备,select返回也为1.在每次select之前通过write发送了一帧数据。调试发现如果不调用write函数 select函数不会返回1。是正常阻塞了的,请问是什么原因???
int serial; int fdmax; fd_set readfs,writefs; char buffer[8]; serial=open("/dev/ttyS0",O_RDWR|O_NONBLOCK); FD_ZERO(&writefds); FD_ZERO(&readfds); FD_SET(serial,&readfs); FD_SET(serial,&writefs); fdmax=serial+1; while(1) { select(fdmax,&readfs,&writefs,NULL); if(FD_ISSET(serial,&readfs)) read(serial,buffer,; if(FD_ISSET(serial,&writefs)) write(serial,buffer,; } 我想让串口可以随时读写,这样设置行...
使用select等待串口是否可读,而且我设置了select的最后一个参数为NULL. 在串口没有写入任何字符的情况下,select依然返回了一个大于0的值.也就是select根本无法阻塞住.
fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY); FD_ZERO(&wset); FD_SET(fd,&wset); select(fd+1,NULL,&wset,NULL,NULL) if(FD_ISSET(fd,&wset)) 。。。。。 { 为什么,select总是能成功返回,不管,我的串口上有没有接设备? 而且每次,Open也成功 [ 本帖最后由 xdshting 于 2009-11-14 13:39 编辑 ]
int serial; int fdmax; fd_set readfs,writefs; char buffer[8]; serial=open("/dev/ttyS0",O_RDWR|O_NONBLOCK); FD_ZERO(&writefds); FD_ZERO(&readfds); FD_SET(serial,&readfs); FD_SET(serial,&writefs); fdmax=serial+1; while(1) { select(fdmax,&readfs,&writefs,NULL); if(FD_ISSET(serial,&readfs)) read(serial,buffer,; if(FD_ISSET(serial,&writefs)) write(serial,buffer,; } 我想让串口可以随时读写,这样设置行...
我想同时监听串口和一个有名管道, FD_ZERO(&readset); //fdset清空,如果多个,参照 FD_SET(fifo_fd,&readset); //加入fifo_fd FD_SET(tty_fd,&readset); //加入tty_fd while(1) { if(select(max_fd,&readset,NULL,NULL,NULL)>0) //select 如果有变化 { //串口有数据进来。 if(FD_ISSET(tty_fd,&readset)) { printf("Data Received from TTY_FD !!!n"); ...
Chapter 4, Advanced Serial Programming 第四章,高级串口编程 This chapter covers advanced serial programming techniques using the ioctl(2) and select(2) system calls. Serial Port IOCTLs In Chapter 2, Configuring the Serial Port we used the tcgetattr and tcsetattr functions to configure the serial port. Under UNIX these functions use the ioctl(2) system call to do their magic. The ioctl system call...
关于用select接收串口的问题 写了一个线程来接收串口消息,刚开始还一切正常,但是过了一阵子select的结果就都是0了。 串口是用硬件一直发送过来的消息,在windows下测试没问题。 我想问一下,如果有消息过来,可能在什么情况下我的select会是0呢? 代码如下: reciveThread_0::reciveThread_0(int fd) { stopped = false; maxfd = fd + 1; FD_ZERO(&readsets); FD_SET(fd,&readsets); FD_ZERO(&writesets); tvptr....
本帖最后由 鬼鬼一哈 于 2011-11-08 10:25 编辑 不过数据还是能接受的到了,就是在限定时间内,鼠标不能用,超时后就又可以用了。如何解决select阻塞的问题?程序读写代码如下: if( write (gprs_fd, gsm_data,71) == 71) { fd_set inset; FD_ZERO(&inset); FD_SET(gprs_fd, &inset ); struct timeval tv; tv.tv_sec = 24; tv.tv_usec = 0; memset(buff,0,2048 ); ...
portCount = select(maxPorts, &readfs, NULL, NULL, &timeout); 就是这条语句出问题,返回数字是0,也就是timeout。这里maxports=3(第二个串口)。程序在linux下运行正常,但是在开发板上就出问题了,请教用过串口2收发数据的大侠,这方面的问题有人遇到吗?是不是开发板和PC机上有一点需要特别注意的地方呢?