Linux Reading Data from UART

后端 未结 3 406
一个人的身影
一个人的身影 2021-01-21 16:45

I want to read data from UART, i followed this tutorial, the write function works as expected, however i\'am getting problem with the read function :

This is the uart_in

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 17:20

    Your read() function may be blocked, for whatever reason. Here is a discussion on reading from a serial port, including code for blocked/unblocked settings.

    It may also be possible that there is no data being transferred, leading to nothing being read. Without having access to the hardware setup, it is difficult to go further without very specific information about what you are seeing.

    Also, in addition to passing the read_buffer correctly (another answer), there are at least two additional things that may improve:

    1) check the return of read before using it:

    bytes_read = read(fd,&read_buffer,10); /* Read the data*/
    if(bytes_read > 0)
    {
        ...
    }
    

    2) Change:

    for(i=0;i

    To:

    //after successful read:
    read_buffer[bytes_read]=0;//place null termination after last character read.
    printf("%s",read_buffer);//note format specifier
    

    This will print the number of characters read, without the loop.

提交回复
热议问题