usb转串口异步读取数据

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


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


#include
#include 

#include 
#include 
#include 

#include 

#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;
}



执行效果:

usb转串口异步读取数据_第1张图片



你可能感兴趣的:(linux,c)