zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    我心中的核心组件(可插拔的AOP)~第十三回 实现AOP的拦截组件Unity.Interception
    .NET 使用unity实现依赖注入
    AOP技术基础
    PowerShell 远程管理之 about_Remote_Troubleshooting
    PowerShell远程连接主机进行会话
    PowerShell_零基础自学课程_9_高级主题:静态类和类的操作
    PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件
    PowerShell 中的目录文件管理
    解决360浏览器兼容模式不兼容,极速模式兼容问题
    reportng之测试报告升级美化
  • 原文地址:https://www.cnblogs.com/qiujiahong/p/3148773.html
Copyright © 2011-2022 走看看