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\n",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..\n");
    printf("Send data\n");
    n = write(fd, "1234567890", 10);
    printf("Send:%d\n", n);
    sleep(1);
    printf("close\n");
    close(fd);
    exit(EXIT_SUCCESS);   
}
void *thread_function(void *arg)
{
    int len,fd;
    char buf[100];
    fd = *((int *)arg);
    printf("Run thread function:%d\n",fd);
    while(1)
    {
        len = read_datas_tty( fd,buf,0,100000);
        if(len == -1)
            break;
        else if(len == 0)
            {}
        else
        {
            printf("Recive buffer:%s\n",buf);
        }
    }
    pthread_exit("Thank you for cpu\n");   
}
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()\n");
        close(fd);
        //break;
        return -1;
       }
       else if(retval)
       {
        ret= read(fd,tempchar2++,1);
        len++;
       }
       else
       {
        break;
       }
    }
    //printf("11:%s\n",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;
}

转载于:https://www.cnblogs.com/qiujiahong/p/3148773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值