AnsweredAssumed Answered

uart + xmodem1k. Get incorrect char in the beginning of data

Question asked by mordasov.ilya on Dec 7, 2015
Latest reply on Dec 9, 2015 by Clive One
Hello everybody.

I'm trying to send file from my pc to stm32 using uart and xmodem1k protocol.

When I send it from my PC,  my debugger shows me all sector's data which it sent

2 1 254 0 0 2 32 185 2 0 8 193 2 0 8 197 2 0 8 57 3 0 8 61 3 0 8 133 3 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 205 3 0 8 209 3 0 8 0 0 0 0 213 3 0 8 73 24 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 0 0 0 0 233 3 0 8 233 3 0 8 0 181 131 176 1 240 164 249 56 75 57 74 19 96 57 74 19 96 57 73 12 224 10 104 75 104 1 241 12 4 136 104 3 224 17 104 25 96 4 51 4 50 152 66 249 216 33 70 50 75 153 66 239 211 46 75 26 104 49 75 154 66 4 209 44 75 26 104 47 75 154 66 0 208 254 231 39 75 46 74 19 96 46 74 19 96 46 75 10 224 25 70 81 248 8 43 91 104 2 224 0 32 16 96 4 50 154 66 250 211 11 70 40 74 147 66 241 211 36 75 27 104 19 185 35 75 27 104 3 177 254 231 1 240 174 250 1 168 105 70 1 240 236 248 33 76 34 75 28 27 164 16 0 37 4 224 30 75 83 248 37 48 152 71 1 53 172 66 248 220 29 76 29 75 28 27 164 16 0 37 4 224 26 75 83 248 37 48 152 71 1 53 172 66 248 220 1 152 0 153 1 240 148 250 5 70 22 76 22 75 28 27 164 16 4 224 1 60 19 75 83 248 36 48 152 71 0 44 248 220 40 70 1 240 144 248 186 186 222 202 0 0 0 32 132 0 0 32 236 3 0 8 4 4 0 8 120 86 52 18 50 84 118 152 136 0 0 32 64 1 0 32 4 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 8 181 255 247 101 255 0 191 0 190 254 231 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 65 74 16 71 45 233 240 65 130 176 4 70 136 70 19 75 94 107 157 107 159 106 27 107 19 240 2 15 13 208 15 75 219 106 0 43 9 218 75 246 171 97 1 240 103 249 32 177 79 240 0 66 9 75 218 98 12 224 9 72 1 240 130 248 205 248 0 128 32 70 57 70 50 70 43 70 1 240 222 248 0 190 254 231 2 176 189 232 240 129 0 191 0 237 0 224 80 49 0 8 0 190 254 231 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 36 74 16 71 45 233 240 65 130 176 7 70 136 70 8 75 93 107 156 107 158 106 7 72 1 240 88 248 205 248 0 128 56 70 49 70 42 70 35 70 1 240 180 248 0 190 254 231 0 237 0 224 96 49 0 8 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 19 74 16 71 45 233 240 65 130 176 7 70 136 70 8 75 93 107 156 107 158 106 7 72 1 240 52 248 205 248 0 128 56 70 49 70 42 70 35 70 1 240 144 248 0 190 254 231 0 237 0 224 108 49 0 8 0 190 254 231 0 190 254 231 0 190 254 231 112 71 0 0 217 2 0 8 81 3 0 8 153 3 0 8 0 190 254 231 180 49 0 8 0 0 0 32 136 0 0 32 180 49 0 8 0 0 0 16 85 89 0

First char is STX, the second char is block number, the third char is inverted block number. It means 3 bytes, after that I have 1024 bytes of data and last 3 bytes of crc16_high, crc16_low and 0x00. In general I have 1030 bytes to send.

My receiver code is here

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    __HAL_UART_FLUSH_DRREGISTER(&huart3); // Clear the buffer to prevent overrun
 
    // if rxIndex is equal buffer length, I calculate CRC16 and send ACK or NAK (I missed the part of code to show the main issue)
 
    if (rxindex == MAXCLISTRING) // MAXCLISTRING = 1030
    {
        uint8_t xbuf[1];
        if (checkCRC16(&rxString[3], SIZE) == 1) // SIZE = 1024
        {
            char tmp[SIZE];
            for (int i=0; i<SIZE; i++)
            {
                tmp[i] = rxString[3+i]; // copy every char from buffer to new buffer
            }
            f_puts(tmp, &fil); // save sector to sd card
            led_toggle(GPIO_PIN_15);
            xbuf[0] = ACK; // send ACK
        }
        else
            xbuf[0] = ACK; // if error send NAK
 
        // reset index and clear buffer
        rxString[rxindex] = 0;
        rxindex = 0;
        for (i = 0; i < MAXCLISTRING; i++) rxString[i] = 0; // Clear the string buffer
 
        HAL_UART_Transmit(&huart3, &xbuf, 1, 5); // send answer
    }
    else
    {
        // if we don't have the end of sector we just copy new char into buffer and increase index
        rxString[rxindex] = rxBuffer;
        rxindex++;
    }
}


So, I set breakpoint in the block of (reindex == MAXCLISTRING) and I see that the second block, the third and other have 0 in the beginning. I think it's the last character which I send

0 2 1 254 0 0 2 32 185 2 0 8 193 2 0 8 197 2 0 8 57 3 0 8 61 3 0 8 133 3 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 205 3 0 8 209 3 0 8 0 0 0 0 213 3 0 8 73 24 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 233 3 0 8 0 0 0 0 233 3 0 8 233 3 0 8 0 181 131 176 1 240 164 249 56 75 57 74 19 96 57 74 19 96 57 73 12 224 10 104 75 104 1 241 12 4 136 104 3 224 17 104 25 96 4 51 4 50 152 66 249 216 33 70 50 75 153 66 239 211 46 75 26 104 49 75 154 66 4 209 44 75 26 104 47 75 154 66 0 208 254 231 39 75 46 74 19 96 46 74 19 96 46 75 10 224 25 70 81 248 8 43 91 104 2 224 0 32 16 96 4 50 154 66 250 211 11 70 40 74 147 66 241 211 36 75 27 104 19 185 35 75 27 104 3 177 254 231 1 240 174 250 1 168 105 70 1 240 236 248 33 76 34 75 28 27 164 16 0 37 4 224 30 75 83 248 37 48 152 71 1 53 172 66 248 220 29 76 29 75 28 27 164 16 0 37 4 224 26 75 83 248 37 48 152 71 1 53 172 66 248 220 1 152 0 153 1 240 148 250 5 70 22 76 22 75 28 27 164 16 4 224 1 60 19 75 83 248 36 48 152 71 0 44 248 220 40 70 1 240 144 248 186 186 222 202 0 0 0 32 132 0 0 32 236 3 0 8 4 4 0 8 120 86 52 18 50 84 118 152 136 0 0 32 64 1 0 32 4 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 20 4 0 8 8 181 255 247 101 255 0 191 0 190 254 231 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 65 74 16 71 45 233 240 65 130 176 4 70 136 70 19 75 94 107 157 107 159 106 27 107 19 240 2 15 13 208 15 75 219 106 0 43 9 218 75 246 171 97 1 240 103 249 32 177 79 240 0 66 9 75 218 98 12 224 9 72 1 240 130 248 205 248 0 128 32 70 57 70 50 70 43 70 1 240 222 248 0 190 254 231 2 176 189 232 240 129 0 191 0 237 0 224 80 49 0 8 0 190 254 231 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 36 74 16 71 45 233 240 65 130 176 7 70 136 70 8 75 93 107 156 107 158 106 7 72 1 240 88 248 205 248 0 128 56 70 49 70 42 70 35 70 1 240 180 248 0 190 254 231 0 237 0 224 96 49 0 8 30 240 4 15 12 191 239 243 8 128 239 243 9 128 113 70 19 74 16 71 45 233 240 65 130 176 7 70 136 70 8 75 93 107 156 107 158 106 7 72 1 240 52 248 205 248 0 128 56 70 49 70 42 70 35 70 1 240 144 248 0 190 254 231 0 237 0 224 108 49 0 8 0 190 254 231 0 190 254 231 0 190 254 231 112 71 0 0 217 2 0 8 81 3 0 8 153 3 0 8 0 190 254 231 180 49 0 8 0 0 0 32 136 0 0 32 180 49 0 8 0 0 0 16 85 89

So I can't find why I get 0 in the beginning, I checked my count of sent data and count of received data - 1030. So, rxindex = 1030

The order of another characters is correct, but my buffer data moves to right for one byte

What is the problem could me?

Outcomes