select串口通讯

来源:互联网 发布:台湾年金改革 知乎 编辑:程序博客网 时间:2024/06/05 16:35

开始也不怎么了解select,查了很多资料以后,才稍微用了起来

串口的初始化,配置波特率,数据位,校验位等等

这里只贴出了,select的简单用法,(//   后面是  发送函数)

检测效果是: 效果是:接收到了之后,把串口线拔了,超过了时间,然后break,打印while循环结束之后的main函数里的hel

头文件:

#include <stdio.h>
#include <sioLib.h>
#include <ioLib.h>
#include <iostream>
#include "selectLib.h"
#include <vxworks.h>

#include "strLib.h"

//void sendRs();

int accept_com1(void);
char buff[12] = {0};
int fd;
int main()
{
// buff[0] = 1;
// buff[1] = 2;
// buff[2] = 3;
// buff[3] = 4;
// buff[4] = 5;
// buff[5] = 6;
// buff[6] = 7;
// buff[7] = 8;


printf("222");
accept_com1();
printf("hel");

// int i=0;
// while(1)
// {
// i++;
// sendRs();
// printf(">>>%d",i);
// taskDelay(1000);
// }



}


//void sendRs()
//{
//
// fd = tty_Open(0,9600,8,1,0);
// tty_Send(fd,(char*)buff,12);
// printf("send >>>");
// tty_Close(fd);
//}

int accept_com1(void)
{
struct timeval tv;  
fd = tty_Open(0,9600,8,1,0);
fd_set rd;
while(1)
{
FD_ZERO(&rd);//位置码清零
FD_SET(fd,&rd);//初始化位置码
tv.tv_sec = 20; //超时就会break,可以把串口线拔了
tv.tv_usec = 0;
if (select(fd+1,&rd,NULL,NULL,&tv) > 0)//任务阻塞
{
tty_Recv(fd,(char*)buff,12);//接收串口信息
printf("%d,%d,%d,%d,%d,%d,%d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6]);
}else
{
tty_Close(fd);
printf("000");
break;
}
}  
}

1 0
原创粉丝点击