usb转串口异步读取数据

该实验是通过usb转串口线连接了开发板的 uart3和pc。


在pc上编译下面代码并执行


#include<stdio.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h>

#define MODEMDEVICE "/dev/ttyUSB0"
#define FALSE 0
#define TRUE 1

int wait_flag=TRUE;

void signal_handler(int i){

    printf("%s\n", __func__);
    wait_flag=TRUE; 
}

int main(){
    
    int fd=0;
    int res;
    char buf[512]={0};
    int STOP = 1;

    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
            if (fd <0) {perror(MODEMDEVICE); return -1; }

    signal(SIGIO, signal_handler);
     /* allow the process to receive SIGIO */
    fcntl( fd, F_SETOWN, getpid() );
    
    int flags = fcntl( fd, F_GETFL );
        
    fcntl( fd, F_SETFL, flags|FASYNC );
    
    while(STOP){

        if(wait_flag){
            res = read(fd,buf,512);
            buf[res]='\0';
            printf("%d : %s\n", res, buf);
            if(buf[0]=='s' && buf[1] =='t'){
                STOP = 0;
            }
            wait_flag=FALSE;
        }
        //sleep(1);
    }

    printf("exit...\n");
    
    close(fd);

    return 0;
}



执行效果:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值