Skip to main content
Visitor II
December 10, 2019
Question

How to compare bytes received in a uint8_t?

  • December 10, 2019
  • 2 replies
  • 2645 views

I have a STM32F103C8T card, I am developing an application that receives a byte array by usb / serial.

I have a variable "uint8_t USBBuffer;" in "usbd_cdc_if.c" which stores the bytes.

My question is: how can a non-vector variable receive an external multi-byte vector?

One of the vectors I get is this: {0x16, 0x16, 0x02, 0x00, 0x10, 0x03, 0x00, 0x00, 0x03}

I realized that I can store these bytes in a single index of a vector, eg: USBBuffer2 [0] = USBBuffer;

With this I can easily transmit USBBuffer2 with the CDC_Transmit function.

My question is, how is this vector stored in a uint8_t that is not a vector? how can i compare with another uint8_t variable?

Thanks in advance.

And I'm sorry if noob's question is that I'm just starting out in this universe.

    This topic has been closed for replies.

    2 replies

    Graduate II
    December 10, 2019

    Perhaps review chapter on pointers in C?

    &USBBuffer​ would be the address for a uint8_t one byte array

    Y​ou can use memcmp() to compare one or multiple bytes in memory.

    Or do

    if (a == x[i]) puts("same");

    Visitor II
    December 10, 2019

    I have tried memcmp and have not been successful.

    I've tried comparing with "if (var1 [i] == USBBuffer [i])" but as I mentioned earlier all bytes are in USBBuffer [0] and var1 [] = {0x16, 0x16, 0x02, 0x00, 0x10, 0x03 , 0x00, 0x00, 0x03}.

    The problem is, I can't print USBBuffer to know how bytes are saved.

    This would be easily solved if I have been using the Arduino IDE, but unfortunately my external application does not recognize my STM32 card if it is not recorded with STM32cubeIDE, I still can't figure out why.

    Graduate II
    December 10, 2019

    >>I have tried memcmp and have not been successful.

    Probably because you're trying to compare one thing against a dozen things.

    uint8_t USBBuffer = 0x12;

    printf("%02X\n", USBBuffer);

    Or make a memory dumping function

    dumpdata(16, &USBBuffer); // The byte that USBBuffer holds, and those surrounding it

    >>I've tried comparing with "if (var1 [i] == USBBuffer [i])" but as I mentioned earlier all bytes are in USBBuffer [0] and var1 [] = {0x16, 0x16, 0x02, 0x00, 0x10, 0x03 , 0x00, 0x00, 0x03}.

    Ok, but there's only ONE byte in your application of USBBuffer, you're going to have to accumulate bytes if you want to compare against multiples.

    // Does byte match vector/array index i

    if (var1 [i] == USBBuffer) printf("Byte Level match at %d\n", i);

    // Accumulate a byte into an array

    uint8_t BiggerBuffer[100], *p = BiggerBuffer;

    ...

    *p++ = USBBuffer; // For each received byte to fill array

    Trick here is

    a) Understand data representation in memory properly

    b) Learn C, variables, arrays

    c) Learn Pointers

    Visitor II
    December 10, 2019

    I really have a lot to learn yet, probably USBBuffer is just a pointer, I will deepen my studies in C.

    Thank you very much.