• LINUX下用select实现串口通讯示例


    1.测试条件

    串口发送和接收短接,在主线程发送数据,新建线程接受数据;

    2.代码

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

    #define SPEED B9600
    #define PORT "/dev/ttyS2"

    char message[]="Hello ";

    void *thread_function(void *arg);
    int init_serial(void);
    int read_datas_tty(int fd,char *rcv_buf,int sec,int usec);

    int main()
    {
        int res,fd ,n;
        pthread_t a_thread;
        void *thread_result;
        fd = init_serial();
        printf("main fd= %d ",fd);
        res = pthread_create(&a_thread,NULL,thread_function,(void *)(&fd));
        if(res!=0){
            perror("Thread creation failed.");
            exit(EXIT_FAILURE);
        }
        printf("Waiting for thread to finish.. ");
        printf("Send data ");
        n = write(fd, "1234567890", 10);
        printf("Send:%d ", n);
        sleep(1);
        printf("close ");
        close(fd);
        exit(EXIT_SUCCESS);   
    }
    void *thread_function(void *arg)
    {
        int len,fd;
        char buf[100];
        fd = *((int *)arg);
        printf("Run thread function:%d ",fd);
        while(1)
        {
            len = read_datas_tty( fd,buf,0,100000);
            if(len == -1)
                break;
            else if(len == 0)
                {}
            else
            {
                printf("Recive buffer:%s ",buf);
            }
        }
        pthread_exit("Thank you for cpu ");   
    }
    int read_datas_tty(int fd,char *rcv_buf,int sec,int usec)
    {
        int retval;
        unsigned char * tempchar2=rcv_buf;
        fd_set rfds;
        struct timeval tv;
        int ret,len=0;
        tv.tv_sec = sec;
        tv.tv_usec = usec;
        while(1)
        {
           FD_ZERO(&rfds);
           FD_SET(fd,&rfds);
           retval = select(fd+1,&rfds,NULL,NULL,&tv);
           if(retval ==-1)
           {
            perror("select() ");
            close(fd);
            //break;
            return -1;
           }
           else if(retval)
           {
            ret= read(fd,tempchar2++,1);
            len++;
           }
           else
           {
            break;
           }
        }
        //printf("11:%s ",rcv_buf);
        return len; 
    }

    int init_serial(void)
    {
        int fd = -1;
        struct termios tio ;
        fd  = open(PORT, O_RDWR | O_NOCTTY);
        if(fd < 0)
        {
            perror("InitSerial:open port error");
            return -1;
        }
        tcgetattr(fd, &tio);
        cfsetospeed(&tio, SPEED);
        cfsetispeed(&tio, SPEED);
        tio.c_cflag |= CLOCAL | CREAD;
        tio.c_cflag &= ~PARENB;
        tio.c_cflag &= ~CSTOPB;
        tio.c_cflag &= ~CSIZE;
        tio.c_cflag |=    CS8;
        //tio.c_cflag &= ~CNEW_RTSCTS;
        tio.c_iflag &= ~(IXON | IXOFF | IXANY);
        tio.c_cc[VTIME] = 1;
        tio.c_cc[VMIN] = 20;
        tio.c_lflag &= ~(ICANON | ECHO | ECHOE);
        tio.c_oflag &= ~OPOST;
        tcflush(fd,TCIFLUSH);
        tcsetattr(fd,TCSAFLUSH,&tio);
        tcflush(fd, TCOFLUSH);
        return fd;
    }

    单片机,嵌入式LINUX技术交流群:142282597
  • 相关阅读:
    js全选,全不选,反选练习
    linux查看系统的一些命令,留着用
    JAVA中native方法
    应该被记住的 8 位 Java 人物
    iframe里面的iframe无法左右对齐的解决方法
    sql语句,查找合并后的结果
    地址路径过深时的处理方式
    惠普中国CEO孙振耀退休感言
    js获得readOnly属性
    直接加载错误页面void com.opensymphony.xwork2.ActionSupport.addActionError(String anErrorMessage)
  • 原文地址:https://www.cnblogs.com/qiujiahong/p/3148773.html
Copyright © 2020-2023  润新知