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
");
		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
");
			return -1;
		}

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


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

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

	}


void *thread2()
{
	int j;
		sleep(1);
        printf("thread2
");
        char buf[100];
        for (j = 0; j< MAX; j++)
        {
        pthread_mutex_lock(&mut);
        sleep(3);
        printf("read  %d
",j+1);
        int k=read(fd,buf,100);
        printf("k+%d
",k);
        puts(buf);
        pthread_mutex_unlock(&mut);
        sleep(2);
        }
        printf("thread2 :stop
");
        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
");
      else
       printf("xian cheng 1 chegn gong
");

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



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


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
");
		else
		{
			i=set_port(fd, 8);
			if(i<0)
			{
				perror("pei zhi cuo wu3
");
				return 0;
			}
	        pthread_mutex_init(&mut,NULL);
	        printf("creat preadth
");
	        thread_create();
	        printf("chu li 
");
	        thread_wait();
	    	close(fd);
		}
	        return 0;

}

  

原文地址:https://www.cnblogs.com/lanye/p/3247669.html