0
votes

I am receiving serial communication through the USB port to my Arduino (ATmega2560) by using the RX0 pin. In the receive function I want to compare the register that receives information UDR0 with unsigned char pckaffe[4]. The data that is being sent through the USB port onto the Arduino comes from a pc and if everything works correctly, it should be sending unsigned char arrays, which is why I am comparing UDR0 to unsigned char pckaffe.

However at the if statement, the compiler is saying Error ISO C++ forbids comparison between pointer and integer [-fpermissive]

Why is that?

unsigned char pckaffe[4] = { 0x0C, 0x0A, 0x0F, 0x0E };

void USART_Receive(){
    while(!(UCSR0A & (1<<RXC0)) );
    if(UDR0 == pckaffe){
        PORTB ^= (1 << PB1);
    }
}
3
You should learn the difference between an array and a single byte. ( Even when using the arduino read function ) BTW: why don't you use that? - datafiddler
@datafiddler So you are saying that UDR0 only receives one bit at a time? Does that mean that I need to use a for loop? - user12511437
... and you should learn the difference between bit and byte ... :) SCNR. If you expect four bytes, you need to read them one by one. (Even with Arduino Serial.read();) - datafiddler

3 Answers

1
votes

Why is that?

unsigned char pckaffe[4] = { 0x0C, 0x0A, 0x0F, 0x0E };

pckaffe is a unsigned char pointer, which you are trying to compare to an integer here

if(UDR0 == pckaffe)
0
votes

pckaffe is a pointer to an array of chars, so the if statement is comparing the value of UDR0 (an int) to the address of pckaffe, which are incompatible types.

0
votes

I want to compare the register that receives information UDR0 with unsigned char pckaffe[4]

URD0 contains one byte of data. pckaffe[4] - four of them. how you want to compare them?

Of course if(UDR0 == pckaffe) has no sense, pckaffe without index is implicitly converted to a pointer to the array. That gives you compilation error.

Probably you want something like that:

unsigned char pckaffe[4] = { 0x0C, 0x0A, 0x0F, 0x0E };
unsigned int pckaffe_pos = 0;

void USART_Receive(){
    while(!(UCSR0A & (1<<RXC0)) ); // Wait for next received byte
    uint8_t data = UDR0; // Get received byte
    if (data == pckaffe[pckaffe_pos]) { // Compare the byte with next position in the array
        pckaffe_pos++; // if matches, increase position
        if (pckaffe_pos >= 4) { // If all bytes matched
             pckaffe_pos = 0; // reset the pointer
             PORTB ^= (1 << PB1); // do something
        }
    } else if (data == pckaffe[0]) { // next byte does not match, but matches the first
       pckaffe_pos = 1; // next byte assumed to be the second in the sequence
    } else { // does not match anything
       pckaffe_pos = 0; // wait for the first byte in the sequence
    }
}