Почему при последовательной связи UART произошла неожиданная задержка? - PullRequest
0 голосов
/ 12 марта 2020

У меня есть программа на C:

#include <stdio.h>
#include <string.h> 
#include <stdlib.h>
#include <time.h>   // clock_gettime
#include <signal.h> // obsluha signalu

// Linux headers
#include <fcntl.h>      // Contains file controls like O_RDWR
#include <errno.h>      // Error integer and strerror() function
#include <termios.h>    // Contains POSIX terminal control definitions
#include <unistd.h>     // write(), read(), close()

// Global variable
int ser_1;          // Serial port
bool cykl=true;     // Podmínka trvani nekonecneho cyklu

void init_serial_1()
{
    // Nastaví sériový port ttyS1
    // 8bit
    // None parita
    // 1 start
    // 1 stop
    // 9600

    // Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device)
    ser_1 = open("/dev/ttyS1", O_RDWR);

    // Create new termios struc, we call it 'tty' for convention
    struct termios tty;
    memset(&tty, 0, sizeof tty);

    // Read in existing settings, and handle any error
    if(tcgetattr(ser_1, &tty) != 0) {
        printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
    }

    tty.c_cflag &= ~PARENB; // Clear parity bit, disable parity
    tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
    tty.c_cflag |= CS8; // 8 bits per byte (most common)
    tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
    tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

    tty.c_lflag &= ~ICANON;
    tty.c_lflag &= ~ECHO; // Disable echo
    tty.c_lflag &= ~ECHOE; // Disable erasure
    tty.c_lflag &= ~ECHONL; // Disable new-line echo
    tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes

    tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
    tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
    // tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
    // tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)

    tty.c_cc[VTIME] = 10;    // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
    tty.c_cc[VMIN] = 0;

    // Set in/out baud rate to be 9600
    cfsetispeed(&tty, B9600);
    cfsetospeed(&tty, B9600);

    // Save tty settings, also checking for error
    if (tcsetattr(ser_1, TCSANOW, &tty) != 0) {
        printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
    }



    /*
    // Write to serial port
    unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o', '\r' };
    write(ser_1, "Hello, world!", 13);

    // Allocate memory for read buffer, set size according to your needs
    char read_buf [256];
    memset(&read_buf, '\0', sizeof(read_buf));

    // Read bytes. The behaviour of read() (e.g. does it block?,
    // how long does it block for?) depends on the configuration
    // settings above, specifically VMIN and VTIME
    int num_bytes = read(ser_1, &read_buf, sizeof(read_buf));

    // n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error.
    if (num_bytes < 0) {
    printf("Error reading: %s", strerror(errno));
    }

    // Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and
    // print it to the screen like this!)
    printf("Read %i bytes. Received message: %s", num_bytes, read_buf);

    close(ser_1);*/
}

// Obsluha signálu
void sigint_handler(int sugnum)
{
    cykl=false;
}

float difftimespec(struct timespec *ts1, struct timespec *ts2)
{
    return difftime(ts1->tv_sec,ts2->tv_sec) + (ts1->tv_nsec - ts2->tv_nsec)*1e-9;
}

// Hlavní program
int main()
{
    // Mereni casu, zacatek
    struct timespec time_start, time_actual;
    clock_gettime(CLOCK_MONOTONIC, &time_start);

    // Inicializace obsluhy signalu
    signal(SIGINT, sigint_handler);

    // Inicializace serioveho portu
    init_serial_1();
    // Buffer pro cteni
    char read_buf [256];
    memset(&read_buf, '\0', sizeof(read_buf));
    int num_bytes;  // Nuber of read charakters

    // Otevreni souboru pro data
    FILE *file;
    file=fopen("data.txt","a+t");   //apend, read, write, text

    // Cyklicky se opakujíci cast
    while(cykl)
    {
        // Get actual time
        clock_gettime(CLOCK_MONOTONIC, &time_actual);
        fprintf(file, "Start: %.9f\n",difftimespec(&time_start,&time_actual));

        printf("Dalsi kolo: %.9f\n", difftimespec(&time_start,&time_actual));

        write(ser_1,"Ra\0",3);  // write to serial command

        clock_gettime(CLOCK_MONOTONIC, &time_actual);
        fprintf(file, "-----: %.9f\n",difftimespec(&time_start,&time_actual));


        num_bytes = read(ser_1, &read_buf, sizeof(read_buf));

        clock_gettime(CLOCK_MONOTONIC, &time_actual);
        printf("Vymena dat: %.9f\n", difftimespec(&time_start,&time_actual));

        printf("ReadMessage:%s %d\n", read_buf, num_bytes);

        fprintf(file, "After: %.9f  %s\n",difftimespec(&time_start,&time_actual),read_buf);
    }

    close(ser_1);
    fclose(file);
    printf("\n\n Konec ...\n");
}

Программа связывается с процессором AVR. Программа работает на одноплатной плате P C. Время связи здесь: Время связи

  • Почему задержка между приемом и передачей?
  • Проблема в read(ser_1, &read_buf, sizeof(read_buf)); Эта команда заканчивается через 4 мс после последний персонаж Почему?
  • Неправильная настройка порта?

1 Ответ

0 голосов
/ 12 марта 2020

При получении данных от UART передача данных приложению обычно инициируется следующим образом:

  • достижение определенного уровня в RX FIFO в контроллере UART
  • тайм-аут, когда новые данные не были получены; общие интервалы для таких тайм-аутов составляют около 3 байтов

Тайм-аут из 3 байтов почти соответствует вашим 4 мс.

...