设为首页 加入收藏

TOP

Linux下串口协议控制51单片机(二)
2014-11-24 12:23:44 来源: 作者: 【 】 浏览:2
Tags:Linux 串口 协议 控制 单片机
read_mutex_lock(&mutex);
read_port();
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}


void *send_thread(void )
{
pthread_mutex_lock(&mutex);
write_port();
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}


void create_thread(void )
{
int temp;
memset(thread, 0, sizeof(thread));
if((temp = pthread_create(&thread[0], NULL,(void *)send_thread, NULL)) != 0)
printf("create send_thread failed!\n");
if((temp = pthread_create(&thread[1], NULL,(void *)recv_thread, NULL)) != 0)
printf("create recev_thread failed!\n");

}


void wait_thread(void )
{
if(thread[0] !=0)
{
pthread_join(thread[0],NULL);
printf("send_thread end\n");
}
if(thread[1] !=0)
{
pthread_join(thread[1],NULL);
printf("recev_thread end\n");
}
}


int main(void )
{
int i;
if((fd=open_port(fd,1))<0){
perror("open_port error");
}
if((i=set_port(fd,9600,8,'N',1))<0){
perror("set_opt error");
}
/*用默认属性初始化互斥锁*/
pthread_mutex_init(&mutex,NULL);
int num = 100;
while (num)
{
create_thread();
wait_thread();
num--;
}
pthread_mutex_destroy(&mutex);
close(fd);
return 0;
}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇自定义ViewGroup实现自动换行的布.. 下一篇Linux网络协议栈之设备初始化

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)