Go Down

Topic: Error RFID invalid conversion (Read 10640 times) previous topic - next topic

carloshm

Hello i need help, i am stuck.

I am using a code from github to read/write the RC522 shield, for using Mifare cards. I supposed everything was going to be fine as it's a code from that web page but i am getting this message:



exit status 1
invalid conversion from 'byte {aka unsigned char}' to 'MFRC522::StatusCode' [-fpermissive]



And this is shown for more than 1 of the functions like this:



\Documents\Arduino\libraries\MFRC522/MFRC522.h:384:29: error:   initializing argument 1 of 'const __FlashStringHelper* MFRC522::GetStatusCodeName(MFRC522::StatusCode)' [-fpermissive]

  const __FlashStringHelper *GetStatusCodeName(StatusCode code);



I have been looking for on internet about this error but not being lucky.

If anyone has ever seen this error, may you please tell me if I am missing some code or I need to correct something.

Thank you in advance

ShapeShifter

invalid conversion from 'byte {aka unsigned char}' to 'MFRC522::StatusCode' [-fpermissive]
This is telling you that you have a byte value, which is actually a unsigned char. You are trying to use it as a parameter to a function that is expecting the MFRC522::StatusCode type, and it can't implicitly convert the value for you.

A quick search for MFRC522::StatusCode gives this definition, which is probably what you have in MFRC522.h:
Code: [Select]
// Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more.
 // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered
 enum StatusCode : byte {
 STATUS_OK , // Success
 STATUS_ERROR , // Error in communication
 STATUS_COLLISION , // Collission detected
 STATUS_TIMEOUT , // Timeout in communication.
 STATUS_NO_ROOM , // A buffer is not big enough.
 STATUS_INTERNAL_ERROR , // Internal error in the code. Should not happen ;-)
 STATUS_INVALID , // Invalid argument.
 STATUS_CRC_WRONG , // The CRC_A does not match
 STATUS_MIFARE_NACK = 0xff // A MIFARE PICC responded with NAK.
 };


So, the type is an enumerated type. While it's encoded as a byte, and you are passing the function a byte, the compiler is giving you that message because it's not necessarily valid to convert any old byte value to the enumerated type. Using the wrong type of parameter is common coding mistake, and the compiler is telling you that you need to make sure that what you're doing is valid.

You can do that by explicitly converting the type yourself:
Code: [Select]
mfrc522.GetStatusCodeName((MFRC522::StatusCode)status)
This says that the compiler should take the byte value status and convert it to MFRC522::StatusCode before passing the value to the function. You are telling the compiler that the value is status really is a MFRC522::StatusCode value, and that the compiler should treat it as one.

That's one way to handle it, but not the best way in this case. Your status value is coming to you as the return value from several different MFRC522 library functions. You are assigning that return value to a variable named status, that is of type byte. That requires an implicit type conversion from the enumerated status type to the generic numeric byte type. That's a valid implicit conversion from a more specific type to a more general type, and the compiler will do that for you without complaining. But then you are calling GetStatusCodeName() with that byte value, but the compiler won't automatically make the conversion back to the more specific type.

The real answer is to not make that implicit conversion to byte in the first place, but instead keep it as a MFRC522::StatusCode value. Then there will be no problems passing the value to GetStatusCodeName() since it will already be the correct type. Basically, in your code wherever you are defining you status variable as "byte status" you should change it to "MFRC522::StatusCode status" and that should get rid of your error messages.

carloshm

You are so great!!!! it sounds simple what you told me but it worked perfectly. Thank you very much. Where can I subscribe to your "web" or anything?? It would be good to follow you. I am doing a big proyect and although Im trying to do it by myself, I know I will face obstacles.

ShapeShifter

it sounds simple what you told me but it worked perfectly.
It's the simple things that can really trip you up, especially when you are just beginning. You've really got to pay attention to the details. I've been programming for almost 40 years now, and I still slap myself in the forehead far too often when realize what kind of simple mistake has been causing me trouble. The good news is that I don't keep doing the same simple mistakes - I keep devising new ones!

I've often thought of starting a programming tutorial type of blog, but I just don't have the time these days...

spike32

This is telling you that you have a byte value, which is actually a unsigned char. You are trying to use it as a parameter to a function that is expecting the MFRC522::StatusCode type, and it can't implicitly convert the value for you.

A quick search for MFRC522::StatusCode gives this definition, which is probably what you have in MFRC522.h:
Code: [Select]
// Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more.
 // last value set to 0xff, then compiler uses less ram, it seems some optimisations are triggered
 enum StatusCode : byte {
 STATUS_OK , // Success
 STATUS_ERROR , // Error in communication
 STATUS_COLLISION , // Collission detected
 STATUS_TIMEOUT , // Timeout in communication.
 STATUS_NO_ROOM , // A buffer is not big enough.
 STATUS_INTERNAL_ERROR , // Internal error in the code. Should not happen ;-)
 STATUS_INVALID , // Invalid argument.
 STATUS_CRC_WRONG , // The CRC_A does not match
 STATUS_MIFARE_NACK = 0xff // A MIFARE PICC responded with NAK.
 };


So, the type is an enumerated type. While it's encoded as a byte, and you are passing the function a byte, the compiler is giving you that message because it's not necessarily valid to convert any old byte value to the enumerated type. Using the wrong type of parameter is common coding mistake, and the compiler is telling you that you need to make sure that what you're doing is valid.

You can do that by explicitly converting the type yourself:
Code: [Select]
mfrc522.GetStatusCodeName((MFRC522::StatusCode)status)
This says that the compiler should take the byte value status and convert it to MFRC522::StatusCode before passing the value to the function. You are telling the compiler that the value is status really is a MFRC522::StatusCode value, and that the compiler should treat it as one.

That's one way to handle it, but not the best way in this case. Your status value is coming to you as the return value from several different MFRC522 library functions. You are assigning that return value to a variable named status, that is of type byte. That requires an implicit type conversion from the enumerated status type to the generic numeric byte type. That's a valid implicit conversion from a more specific type to a more general type, and the compiler will do that for you without complaining. But then you are calling GetStatusCodeName() with that byte value, but the compiler won't automatically make the conversion back to the more specific type.

The real answer is to not make that implicit conversion to byte in the first place, but instead keep it as a MFRC522::StatusCode value. Then there will be no problems passing the value to GetStatusCodeName() since it will already be the correct type. Basically, in your code wherever you are defining you status variable as "byte status" you should change it to "MFRC522::StatusCode status" and that should get rid of your error messages.

I'm sorry where do I have to put this line of code?

mfrc522.GetStatusCodeName((MFRC522::StatusCode)status)

I'm going crazy

Go Up