This code works with Arduino Uno, but not with MKR 1010 Wifi

Hello,

This is a sample code to extract a RFID tag with Arduino Uno. With Uno board works fine, but when I try to use the same code with Arduino MKR 1010 Wifi, doesn't run... :confused: can anyone help me to solve it, or have an explanation why that happens? Thanks a lot!!

Error message:

Arduino:1.8.8 (Windows 10), Tarjeta:"Arduino MKR WiFi 1010"

C:\Users\usuario\Desktop\Coloms_alfa\Coloms_alfa.ino: In function 'unsigned int extract_tag()':

Coloms_alfa:54:59: error: invalid conversion from 'uint8_t* {aka unsigned char*}' to 'char*' [-fpermissive]

     long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);

Coloms_alfa:64:6: error: initializing argument 1 of 'long int hexstr_to_value(char*, unsigned int)' [-fpermissive]

 long hexstr_to_value(char *str, unsigned int length) { // converts a hexadecimal value (encoded as ASCII string) to a numeric value

Coloms_alfa:59:43: error: invalid conversion from 'uint8_t* {aka unsigned char*}' to 'char*' [-fpermissive]

       long val = hexstr_to_value(msg_data + i, CHECKSUM_SIZE);

Coloms_alfa:64:6: error: initializing argument 1 of 'long int hexstr_to_value(char*, unsigned int)' [-fpermissive]

 long hexstr_to_value(char *str, unsigned int length) { // converts a hexadecimal value (encoded as ASCII string) to a numeric value

C:\Users\usuario\Desktop\Coloms_alfa\Coloms_alfa.ino: In function 'long int hexstr_to_value(char*, unsigned int)':

Coloms_alfa:65:50: error: invalid conversion from 'void*' to 'char*' [-fpermissive]

   char* copy = malloc((sizeof(char) * length) + 1); 

exit status 1
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'char*' [-fpermissive]

Code:

const int BUFFER_SIZE = 14; // RFID DATA FRAME FORMAT: 1byte head (value: 2), 10byte data (2byte version + 8byte tag), 2byte checksum, 1byte tail (value: 3)
const int DATA_SIZE = 10; // 10byte data (2byte version + 8byte tag)
const int DATA_TAG_SIZE = 8; // 8byte tag
const int CHECKSUM_SIZE = 2; // 2byte checksum
uint8_t buffer[BUFFER_SIZE]; // used to store an incoming data frame 
int buffer_index = 0;

void setup() {
 Serial.begin(9600); 
 Serial1.begin(9600);
 Serial.println("INIT DONE");
}

void loop() {
  if (Serial1.available() > 0){
    bool call_extract_tag = false;
    
    int ssvalue = Serial1.read(); // read 
    if (ssvalue == -1) { // no data was read
      return;
    }
    if (ssvalue == 2) { // RDM630/RDM6300 found a tag => tag incoming 
      buffer_index = 0;
    } else if (ssvalue == 3) { // tag has been fully transmitted       
      call_extract_tag = true; // extract tag at the end of the function call
    }
    if (buffer_index >= BUFFER_SIZE) { 
      Serial.println("Error: Buffer overflow detected!");
      return;
    }
    
    buffer[buffer_index++] = ssvalue; // everything is alright => copy current value to buffer
    if (call_extract_tag == true) {
      if (buffer_index == BUFFER_SIZE) {
        unsigned tag = extract_tag();
      } else { // something is wrong... start again looking for preamble (value: 2)
        buffer_index = 0;
        return;
      }
    }    
  }    
}
unsigned extract_tag() {
    uint8_t msg_head = buffer[0];
    uint8_t *msg_data = buffer + 1; // 10 byte => data contains 2byte version + 8byte tag
    uint8_t *msg_data_version = msg_data;
    uint8_t *msg_data_tag = msg_data + 2;
    uint8_t *msg_checksum = buffer + 11; // 2 byte
    uint8_t msg_tail = buffer[13];
    
    long tag = hexstr_to_value(msg_data_tag, DATA_TAG_SIZE);
    Serial.print("Extracted Tag: ");
    Serial.println(tag);
    long checksum = 0;
    for (int i = 0; i < DATA_SIZE; i+= CHECKSUM_SIZE) {
      long val = hexstr_to_value(msg_data + i, CHECKSUM_SIZE);
      checksum ^= val;
    }
    return tag;
}
long hexstr_to_value(char *str, unsigned int length) { // converts a hexadecimal value (encoded as ASCII string) to a numeric value
  char* copy = malloc((sizeof(char) * length) + 1); 
  memcpy(copy, str, sizeof(char) * length);
  copy[length] = '\0'; 
  // the variable "copy" is a copy of the parameter "str". "copy" has an additional '\0' element to make sure that "str" is null-terminated.
  long value = strtol(copy, NULL, 16);  // strtol converts a null-terminated string to a long value
  free(copy); // clean up 
  return value;
}

use char not uint8_t for char arrays

Thanks for your help Juraj, but I replaced all variables and the error is located at following lines. In any case, I can't explain why is working on Uno and not over MKR 1010 :frowning:

Code line:

char* copy = malloc((sizeof(char) * CHECKSUM_SIZE) + 1);

Error:

error: invalid conversion from 'void*' to 'char*' [-fpermissive]

       char* copy = malloc((sizeof(char) * CHECKSUM_SIZE) + 1);

                                                             
At global scope:

81:1: error: expected declaration before '}' token

 }

exit status 1
invalid conversion from 'void*' to 'char*' [-fpermissive]

It seems like different board packages invoke the compiler with different setting with regard how picky they are about sloppy type conversions. Try casting the return value from 'malloc' as a 'char *'.