Невозможно прочитать с порта UART, Beaglebone Black - PullRequest
2 голосов
/ 14 июля 2020

Пытаюсь прочитать данные с UART1 Beaglebone Black. Я подключил TX UART1 к RX UART1 На экране печатаются неизвестные символы. Я вводил символы из Minicom ttyO1 терминала

Мой код:


#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>   // using the termios.h library

int main(){
   int file;
   file = open("/dev/ttyO1", O_RDWR | O_NOCTTY | O_NDELAY);

      struct termios options;               //The termios structure is vital
      tcgetattr(file, &options);            //Sets the parameters associated with file
      options.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
      options.c_iflag = IGNPAR | ICRNL;    //ignore partity errors, CR -> newline
      tcflush(file, TCIFLUSH);             //discard file information not transmitted
      tcsetattr(file, TCSANOW, &options);  //changes occur immmediately

      unsigned char recieve[100];  //the string to send
while(1) {
      read(file, (void*)recieve,100);       //send the string
      sleep(2);
      printf("%s ", recieve);
      fflush(stdout);
      close(file);

}
       return 0;
}

1 Ответ

1 голос
/ 14 июля 2020

Поскольку вы инициализируете UART с параметром O_NDELAY, read возвращается немедленно, а printf печатает содержимое неинициализированного массива, то есть мусора.

Чтение последовательной строки является сортировкой хитрого. По крайней мере, проверьте возвращаемое значение read и завершите то, что было прочитано, 0 (помните, что printf ожидает, что данные будут завершены 0, а read не не добавляет терминатор) , по строкам

    int characters = read(file, (void*)recieve,100);
    if (characters == -1) {
        handle_the_error();
    }
    receive[characters] = 0;
    printf("%s", receive);
    ....

Кроме того, читать из закрытого файла нельзя. Выньте close(file); из l oop.

...