void *thread1(void *)
{
//线程 1
while(1)
{
rbuf[0]='\0';
/*超时太长,会丢失数据包,如果连续*/
rc=readport(fd,rbuf,5,50); //读取5个字节,超时时间为100,500毫秒
if(rc!=-1)
{
rbuf[5]='\0';
//writeport(fd,wbuf,rc);
writeport(fd,rbuf,rc); //把收到的数据 发回
printf("Rev id:%s\n",rbuf);
}
//else
//{
// printf("Lost recv:%d\t Lost Text:%s\n",rc,rbuf);
// writeport(fd,wbuf,rc);
//}
}
close(fd); //关闭串口
pthread_exit(0);
}
void *thread2(void *)
{
QPEApplication a( argc, argv );
HelloForm f(0,"yunfly",Qt::WStyle_Customize|Qt::WStyle_NoBorder);
a.showMainWidget( &f );
//int result=a.exec();
a.exec();
pthread_exit(0);
}
int main( int a_rgc, char *a_rgv[] )
{
argc=a_rgc;
argv=a_rgv;
/********************初始化串口*************************/
// for(ii=0;ii<256;ii++)wbuf[ii]=ii;
fd=openport(dev); //打开串口
if(fd>0){
ret=setport(fd,115200,8,1,'N'); //设置串口,波特率,数据位,停止位,校验
if(ret<0){
printf("Can't Set Serial Port!\n");
exit(0);
}
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
/*****************************************************/
pthread_t id1,id2;
int ret;
/*Create Thread 1: RS232 Serial */
ret=pthread_create(&id1,NULL,thread1,NULL);
if(ret!=0){
printf("Create pthread error!\n");
exit(1);
}
/*Create Thread 2:QT Window*/
ret=pthread_create(&id2,NULL,thread2,NULL);
if(ret!=0){
printf("Create pthread error!\n");
exit(1);
}
pthread_join(id1,NULL);
pthread_join(id2,NULL);
exit(0);
}