1

I have to send an array of data between a nucleo f446re and a pc with ubuntu using the UARTSerial class.

The code that I'm using on the mbed is the following:

int main() {
    UARTSerial pc(USBTX, USBRX, 921600);
    uint8_t buff[256] = {
        5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4
    };

    pc.sync();

    while(true) {
        pc.write(buff, 23);
        pc.sync();
        wait(1);
    }

    return 0;
}

The code that I'm running on the pc is:

int main() {
    struct termios tattr{0};

    // open the device in read/write sync
    int com = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_SYNC );

    if (com == -1)
        throw std::runtime_error("ERROR: can't open the serial");

    tcgetattr(com, &tattr);

    tattr.c_iflag &= ~(INLCR|IGNCR|ICRNL|IXON);

    tattr.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); 

    tattr.c_cflag = CS8 | CREAD | CLOCAL; 

    tattr.c_lflag &= ~(ICANON|ECHO);    

    tattr.c_cc[VMIN] = 1;

    tattr.c_cc[VTIME] = 0;

    tattr.c_ispeed = 921600;
    tattr.c_ospeed = 921600;

    tcsetattr (com, TCSAFLUSH, &tattr);

    while (true) {
        usleep(1000);
        tcflush(com, TCIOFLUSH);
        uint8_t buff[24];
        ::read(com, buff, 23);

        printf("reading frame... ");
        for (auto b : buff) {
            printf("%02X ", b);
        }
        puts("\n");
    }
}

The output that I receive on the pc is:

[...]
reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00 

reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00  
[...]

As you can see the result is not the same that I'm expecting.

I've already tried to send one byte at a time with a loop, but the result is the same.

I cannot understand why I can't read the USB I've tried to flush the usb both on the pc and on the nucleo board.

Noè Murr
  • 496
  • 4
  • 11

2 Answers2

0

you have to use a decoder to decode the bytes from the serial port see the link below: https://codereview.stackexchange.com/questions/200846/a-simple-and-efficient-packet-frame-encoder-decoder

Saif Faidi
  • 509
  • 4
  • 15
  • I don't understand why I have to use a decoder while I'm not using any protocol to send the data. – Noè Murr Jul 08 '19 at 14:18
  • in fact, when reading from the serial port it's only bites because it's converted to bytes when sending data, so to get human-readable information you should decode this data. – Saif Faidi Jul 08 '19 at 15:14
0

I found the problem. It was the setting of the baudrate, I have to use this lines:

// receive speed
cfsetispeed (&tattr, B921600);
// transmit speed
cfsetospeed (&tattr, B921600);

instead of this:

// receive speed
tattr.c_ispeed = 921600;
tattr.c_ospeed = 921600;
Noè Murr
  • 496
  • 4
  • 11