- 论坛徽章:
- 0
|
10可用积分
本帖最后由 js001sdx 于 2010-03-12 11:42 编辑
第一次做线程!现在遇到如下问题!!请各位赐教!!不生感激!!
我在主函数里做了三个线程!!分别是显示和按键、存储、串口收发!!
如下:
int main(int argc, char **argv)
{
int fd,fd3;
int i;
pthread_t thread1, thread2, thread3;
//初始化部分
fd = open("/dev/hgo", O_RDWR);
if (fd < 0)
{
printf("open hgo device error00! %d\n", fd);
}
ioctl(fd, 0,10);
ioctl(fd, 1, 5);
close(fd);
result0=pthread_create(&thread1, NULL, (void *)task1,NULL);
if(result0)
{
perror("pthread_create: task1.\n");
exit(0);
}
result0=pthread_create(&thread2, NULL, (void *)task2,NULL);
if(result0)
{
perror("pthread_create: task2.\n");
exit(0);
}
result0=pthread_create(&thread3, NULL, (void *)task3,NULL);
if(result0)
{
perror("pthread_create: task3.\n");
exit(0);
}
pthread_join(thread3, NULL);
pthread_join(thread2, NULL);
pthread_join(thread1, NULL);
}
void task1()
{
while(1)
{
usleep(1000*30);
显示和按键的处理程序。
}
}
void task2()
{
while(1)
{
usleep(1000*50);
存储部分代码。
}
}
void task3()
{
while(1)
{
usleep(1000*30);
串口收发代码,这里要说明一下:我的串口外面接了485的片子,所以采用了485通信方式,传输的代码量有点大,我单独跑了一下,把所有的命令收发一边大约需要10分钟。
}
现在的问题是:
1:加入线程后,我的串口收回来的数据总是错误!!一般都是丢掉了数据串的前几个字符!!(单独把串口收发做子程序是正常的)
2:整个程序跑起来后,过一段时间串口收发那个线程好像会停止不运行了!!
我把我的串口部分主要代码也贴一下:
fd1 = open(SERIAL1, O_RDWR);//O_RDWR|O_NONBLOCK);//打开串口
if (fd1 < 0)
{
printf("open serial 1 error! %d\n", fd1);
}
set_speed(fd1, 2400);
set_other_attribute(fd1, 8, 1, 'e');//设置串口
fd0 = open("/dev/485_IO", O_RDWR);//打开485使能引脚驱动
if (fd0 < 0)
{
printf("open 485_IO error! %d\n", fd0);
}
ioctl(fd0, 0,0);//485使能置高
i = write(fd1, temp, 16);//向串口发命令
usleep(1000*80);
i=0;
ioctl(fd0, 1,0);//485使能置低
while(nread=read(fd1, test+i, 1)>0)//读串口数据
{
if(*(test+i)==0x16)
{
end=i;
break;
}
i++;
}
for(i=0;i<end;i++)
{
printf("-%x-",*(test+i));
}
printf("\n");
} |
|