Linux 多线程串口通信

大概流程就是打开一个串口、然后进行串口设置。开启二个线程,一个线程写数据,另一个线程读数据。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <sys/time.h>

#define MAX 2
pthread_t thread[2];
pthread_mutex_t mut;
int fd;

int set_port(int fd,int  nbits)
{
	struct termios newtio,oldtio;
	if(tcgetattr(fd,&oldtio)!=0)
	{
		perror("pei zhi cuo wu1\n");
		return -1;
	}

	bzero(&newtio,sizeof(newtio)); //清零
	newtio.c_cflag |=CLOCAL|CREAD;//用于本地连接和接收使能

	newtio.c_cflag &=~CSIZE;//设置数据位
	switch(nbits)
	{
	case 7:
		newtio.c_cflag |=CS7;break;
	case 8:
		newtio.c_cflag |=CS8;break;
	}

	//设置奇校验位
		newtio.c_cflag |=PARENB;

	//设置波特率
		cfsetispeed(&newtio,B115200);
		cfsetospeed(&newtio,B115200);

		//设置停止位
		newtio.c_cflag &=~PARENB;

		if((tcsetattr(fd,TCSANOW,&newtio))!=0)
		{
			perror("pei zhi cuo wu2\n");
			return -1;
		}

		printf("bao cun wan bi \n");
		return 0;
	}


void *thread1()
{
	int i;
	printf ("thread1 \n");
	for( i=0;i<MAX;i++){
		pthread_mutex_lock(&mut);
		if(i==0){
			printf("write  %d\n",i+1);
			char buf1[]="AT+FCLASS=0\r\n";
			int length=sizeof(buf1);
			int j=write(fd,buf1,length);
			puts(buf1);
			if(j<0)printf("fa song shi bai\n");
			printf("%d   \n",j);
		}
		else if(i==1){
			printf("write  %d\n",i+1);
			char buf2[]="AT+CBST=7,0,0\r\n";
			int length=sizeof(buf2);
			int j=write(fd,buf2,length);
			puts(buf2);
			if(j<0)printf("fa song shi bai\n");
			printf("%d   \n",j);

				}
		sleep(3);
		pthread_mutex_unlock(&mut);
	}
	printf("thread1 stop\n");
	pthread_exit(NULL);

	}


void *thread2()
{
	int j;
		sleep(1);
        printf("thread2\n");
        char buf[100];
        for (j = 0; j< MAX; j++)
        {
        pthread_mutex_lock(&mut);
        sleep(3);
        printf("read  %d\n",j+1);
        int k=read(fd,buf,100);
        printf("k+%d\n",k);
        puts(buf);
        pthread_mutex_unlock(&mut);
        sleep(2);
        }
        printf("thread2 :stop\n");
        pthread_exit(NULL);

}


void thread_create(void)
 {
	int temp;
     memset(&thread, 0, sizeof(thread));          //comment1
      /*创建线程*/
     if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2
      printf("xian chegn 1 faile\n");
      else
       printf("xian cheng 1 chegn gong\n");

       if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3
         printf("2 faile\n");
       else
            printf("2 surcess\n");
        }



void thread_wait(void)
{
        /*等待线程结束*/
        if(thread[0] !=0) {                   //comment4
                pthread_join(thread[0],NULL);
                printf("1 stop \n");
        }
        if(thread[1] !=0) {                //comment5
                pthread_join(thread[1],NULL);
                printf("2 stop \n");
        }
}


int main(void) {
		int i,j,k;
		fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
		if(-1==fd)printf("mei da kai tong xin duan kou hao\n");
		else
		{
			i=set_port(fd, 8);
			if(i<0)
			{
				perror("pei zhi cuo wu3\n");
				return 0;
			}
	        pthread_mutex_init(&mut,NULL);
	        printf("creat preadth\n");
	        thread_create();
	        printf("chu li \n");
	        thread_wait();
	    	close(fd);
		}
	        return 0;

}

  

posted @ 2013-08-09 11:00  蓝夜  阅读(4462)  评论(0编辑  收藏  举报