Trying to compare data: error: invalid use of void expression

Hello,

I am trying to work on comparing a block of data from an RFID card.

So far all I have it doing is printing a set text when an RFID card is picked up.

I keep getting this error when trying to compare code: "NFC_working_sketch:132: error: invalid use of void expression"

/**
  @file    nfc_mifare_mf1s50_reader.ino
  @author  www.elechouse.com
  @brief   example of reading mf1s50 card for NFC_MODULE
  
    For this demo, waiting for a MF1S50 card or tag, after reading a card/tag UID,
    then try to read the block 4/5/6/7 ..
  
  @section  HISTORY
  
  V1.0 initial version
  
    Copyright (c) 2012 www.elechouse.com  All right reserved.
*/

/** include library */
#include "nfc.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

/** define a nfc class */
NFC_Module nfc;

void setup(void)
{
   lcd.begin(16, 2);
  Serial.begin(9600);
  nfc.begin();
  Serial.println("MF1S50 Reader Demo From Elechouse!");
  
  uint32_t versiondata = nfc.get_version();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  /** Set normal mode, and disable SAM */
  nfc.SAMConfiguration();
}

void loop(void)
{
  
  
  
  u8 buf[32],sta;
  
  
  /** Polling the mifar card, buf[0] is the length of the UID */
  sta = nfc.InListPassiveTarget(buf);
  
  lcd.setCursor(0,0);
  lcd.print("Waiting for card...");
 
  
  /** check state and UID length */
  if(sta && buf[0] == 4){
    /** the card may be Mifare Classic card, try to read the block */  
    Serial.print("UUID length:");
    Serial.print(buf[0], DEC);
    Serial.println();
    Serial.print("UUID:");
    nfc.puthex(buf+1, buf[0]);
    Serial.println();
    /** factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF */
    u8 key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    u8 blocknum = 4;
    /** Authentication blok 4 */
    sta = nfc.MifareAuthentication(0, blocknum, buf+1, buf[0], key);
    if(sta){
      /** save read block data */
      
      u8 block[16];
      Serial.println("Authentication success.");
              lcd.setCursor(0,1);      // First line
        lcd.print("Lewis NFC");
      
      // uncomment following lines for writing data to blok 4
/*      
      strcpy((char*)block, "Elechoues - NFC");
      sta = nfc.MifareWriteBlock(blocknum, block);
      if(sta){
        Serial.println("Write block successfully:");
      }
*/  

      /** read block 4 */
      sta = nfc.MifareReadBlock(blocknum, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        Serial.println();
                lcd.setCursor(0,1);      // First line
        lcd.print("Lewis NFC");
      }
      
      /** read block 5 */
      sta = nfc.MifareReadBlock(blocknum+1, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        Serial.println();
      }
      
      /** read block 6 */
      sta = nfc.MifareReadBlock(blocknum+2, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        Serial.println();
                lcd.setCursor(0,1);      // First line
        lcd.print("Lewis NFC");
      }
      
      /** read block 7 */
      sta = nfc.MifareReadBlock(blocknum+3, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        
        char buffer[] = "BL BL BL BL BL BL BL";
        int r;
        r=memcmp ( buffer , nfc.puthex(block, 16), 16);
        
        if(r > 0){
         
        char print='This is bigger';  
        }
        else
        {
          char print='This is not bigger';
        }
  
      
        Serial.println();
        lcd.setCursor(0,1);      // First line
        lcd.print("Lewis NFC");
      }
      lcd.clear();
    }  
  }
}

PLEASE NOTE: I have googled this problem because I was initially trying to set the variable as a char. Then I switched to including it directly as part of the memcmp() function. I don't know where to start in repairing the issue so any advice is gratefully received.

Replace void setup(void) with void setup().
Replace void loop(void) with void loop().

BoylesWhite:
NFC_working_sketch:132: error: invalid use of void expression

The error message says that something is wrong in line 132 of "NFC_working_sketch." The error is that something that's declared as "void" is referenced improperly.

The 132nd line of the code is:r=memcmp ( buffer , nfc.puthex(block, 16), 16);That line exhibits exactly the issue that the error message describes. memcmp() expects that its first two arguments will be pointers. buffer is indeed a pointer; nfc.puthex is not a pointer. In the file nfc.h - the header file of the NFC library - are these statements:

void puthex(u8 *buf, u32 len);
void puthex(u8 data);

They both define a function puthex(), declared as void. Neither returns a pointer.

puthex() writes some data to the serial port, and returns nothing. Its first argument is a pointer, though, and it points to the data that puthex() writes to the serial port. Maybe you wanted to use block - the pointer argument to puthex() - in memcmp(), rather than the void function puthex().

tmd3:

BoylesWhite:
NFC_working_sketch:132: error: invalid use of void expression

The error message says that something is wrong in line 132 of "NFC_working_sketch." The error is that something that's declared as "void" is referenced improperly.

The 132nd line of the code is:r=memcmp ( buffer , nfc.puthex(block, 16), 16);That line exhibits exactly the issue that the error message describes. memcmp() expects that its first two arguments will be pointers. buffer is indeed a pointer; nfc.puthex is not a pointer. In the file nfc.h - the header file of the NFC library - are these statements:

void puthex(u8 *buf, u32 len);

void puthex(u8 data);


They both define a function puthex(), declared as void. Neither returns a pointer. 

puthex() writes some data to the serial port, and returns nothing. Its first argument is a pointer, though, and it points to the data that puthex() writes to the serial port. Maybe you wanted to use block - the pointer argument to puthex() - in memcmp(), rather than the void function puthex().

Thanks for the considered response. Ok so from that I understand that the function is void because there is no data returned (although some is written to serial port)

Ok the pointer argument to puthex() I'm assuming the pointer argument in this instance is simply block and does the 16 refers to the bytes perhaps? I can't find any syntax docs on the puthex() function.

Is there any chance you could give an example of how I could compare that data I am looking for?

I will try this:

char cardId=block;
char someVar[]="OTHER ID";
if(block == someVar)
{
run some code
}
else
{
run some other code
}

Would this be the correct way?

BoylesWhite:
I can't find any syntax docs on the puthex() function.

You can find all there is to know about puthex() in nfc.h and nfc.cpp. It's defined in those files. That's where I looked, using this the library link at this url: http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2205.

Would this be the correct way?

I'd bet that it's not. The first line sets a character variable equal to a pointer; I can't see that you'll get the results you expect.

I can't determine whether any particular approach is the "correct way." I don't understand your code, I'm not familiar with the NFC library, and I don't even know what you hope to accomplish. I surmise that you want to compare two things in this code. Only you know what they are. Ask yourself: What are you trying to compare to what? That will take you a long way toward figuring out how to do it.

Hello,

I am trying to print a string to my LCD. The data I am trying to print is currently in u8* encoding. And I need to get it formatted as a const char*.

I currently get the error:

NFC_working_sketch:97: error: invalid conversion from 'u8*' to 'const char*'

Here is the relevant code:

      /** read block 4 */
      sta = nfc.MifareReadBlock(blocknum, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        const char* blockData = block;
        strcpy(dataToPrint , blockData);//cannot redeclare/edit the sting as the array is set.
        //print=block; This didn't work causes invalid array assingment error (array set)

Here is the full code:

/**
  @file    nfc_mifare_mf1s50_reader.ino
  @author  www.elechouse.com
  @brief   example of reading mf1s50 card for NFC_MODULE
  
    For this demo, waiting for a MF1S50 card or tag, after reading a card/tag UID,
    then try to read the block 4/5/6/7 ..
  
  @section  HISTORY
  
  V1.0 initial version
  
    Copyright (c) 2012 www.elechouse.com  All right reserved.
*/

/** include library */
#include "nfc.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

/** define a nfc class */
NFC_Module nfc;

void setup(void)
{
   lcd.begin(16, 2);
  Serial.begin(9600);
  nfc.begin();
  Serial.println("MF1S50 Reader Demo From Elechouse!");
  
  uint32_t versiondata = nfc.get_version();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  /** Set normal mode, and disable SAM */
  nfc.SAMConfiguration();
}

void loop(void)
{
  
  
  u8 buf[32],sta;
  
  
  /** Polling the mifar card, buf[0] is the length of the UID */
  sta = nfc.InListPassiveTarget(buf);
  
  lcd.setCursor(0,0);
  lcd.print("Waiting for card...");
  //char print[6]={'L' , 'e' , 'w' , 'i' , 's' , '\0'}; //Declaring the string as an array to print
  char dataToPrint[16]=""; //Using double quotes on the "Literal" string and * will like (explode by byte essentially)
  
  /** check state and UID length */
  if(sta && buf[0] == 4){
    /** the card may be Mifare Classic card, try to read the block */  
    Serial.print("UUID length:");
    Serial.print(buf[0], DEC);
    Serial.println();
    Serial.print("UUID:");
    nfc.puthex(buf+1, buf[0]);
    Serial.println();
    /** factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF */
    u8 key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    u8 blocknum = 4;
    /** Authentication blok 4 */
    sta = nfc.MifareAuthentication(0, blocknum, buf+1, buf[0], key);
    if(sta){
      /** save read block data */
      
      u8 block[16];
      Serial.println("Authentication success.");
              lcd.setCursor(0,1);      // First line
      
      // uncomment following lines for writing data to blok 4
/*      
      strcpy((char*)block, "Elechoues - NFC");
      sta = nfc.MifareWriteBlock(blocknum, block);
      if(sta){
        Serial.println("Write block successfully:");
      }
*/  

      /** read block 4 */
      sta = nfc.MifareReadBlock(blocknum, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        const char* blockData = block;
        strcpy(dataToPrint , blockData);//cannot redeclare/edit the sting as the array is set.
        //print=block; This didn't work causes invalid array assingment error (array set)
        Serial.println();
                lcd.setCursor(0,1);      // First line
        lcd.print(dataToPrint);
      }
      
      /** read block 5 */
      sta = nfc.MifareReadBlock(blocknum+1, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        Serial.println();
      }
      
      /** read block 6 */
      sta = nfc.MifareReadBlock(blocknum+2, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        Serial.println();
                lcd.setCursor(0,1);      // First line

      }
      
      /** read block 7 */
      sta = nfc.MifareReadBlock(blocknum+3, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        
  
      
        Serial.println();
        lcd.setCursor(0,1);      // First line
      }
      lcd.clear();
    }  
  }
}

Also later I will be using strcmp to compare the actual "block" with the expected "block" (stored locally). I will need to provide both these as const char* so will need to be converting fron char* to const char* also.

Thanks for any advice.

P.S. The reason I need to convert to char in the first place to print is because the lcd.print function will return an error like:

call of overloaded ‘’ is ambiguous

What is U8 encoding exactly? Is it UTF-8? If not can you post a link to it?

Where does "nfc.h" come from? (link please)

nfc.h is from here: nfc-pn532/nfc.h at master · elechouse/nfc-pn532 · GitHub

I'm not too sure myself what U8 encoding is here is the most relevant thing I can find: c++ - Is the u8 string literal necessary in C++11 - Stack Overflow

From that .h file:

typedef uint8_t u8;

So it appears you may as well call u8 "byte". Or "char" even. That might eliminate most of the warnings/errors.

Yeah looking at the link here: http://www.flightlab.com/~joe/gutter/doc/nap-4.1.0/html/data_type.html

U8 described as an: 8-bit unsigned integer

Not too sure what you mean in the reply. Do you mean to add the typdef? Will I have to change anything in the .h file?

In your sketch change u8 to byte. It amounts to the same thing anyway.

error: invalid conversion from 'u8*' to 'const char*'

I think there are two issues here. One is it is const, the other is that byte (u8) is unsigned whereas char is signed.

Thanks for your answers Nick, I will look into both those issues separately. My plan is to somehow convert it to signed then to declare a new const car* with the value of the result.

Cheers.

The easiest way to do this is simply typecast your array:

strcpy(dataToPrint , (const char*)block);

Both types are 8 bit variables, the only difference is signed-ness.

Thanks,

when I run that code I get the following error:

NFC_working_sketch.ino: In function 'void loop()':
NFC_working_sketch:104: error: expected primary-expression before 'const'
NFC_working_sketch:104: error: expected `)' before 'const'

previously set dataToPrint as:

 char dataToPrint[16]="";

Any ideas? I'm a little stuck.

I got your original sketch to compile by just changing one line:

      sta = nfc.MifareReadBlock(blocknum, block);
      if(sta){
        Serial.println("Read block successfully:");
        
        nfc.puthex(block, 16);
        const char* blockData = (const char*) block;   // <------- add cast here
        strcpy(dataToPrint , blockData);//cannot redeclare/edit the sting as the array is set.

I was wondering if the topic was ever resolved I am having the same problem.

I am having the same problem. The nfc.puthex(block, 16); function down not return a value it just prints it to the serial port. I tried to change the library but I ran into a ton of problems. Does anyone have any ideas or want to try to fix the library?

Here is the code in the library that I believe is relevant.

/*****************************************************************************/
/*!
	@brief  Send a byte by hex format
	@param  data - the byte
	@return NONE
*/
/*****************************************************************************/
void NFC_Module::puthex(u8 data)
{
    Serial.write(hextab[(data>>4)&0x0F]);
    Serial.write(hextab[data&0x0F]);
    Serial.write(' ');
}

/*****************************************************************************/
/*!
	@brief  Send hexadecimal data through Serial with specified length.
	@param  buf - pointer of data buffer.
	@param  len - length need to send.
	@return NONE
*/
/*****************************************************************************/
void NFC_Module::puthex(u8 *buf, u32 len)
{
    u32 i;
    for(i=0; i<len; i++)
    {
        Serial.write(hextab[(buf[i]>>4)&0x0F]);
        Serial.write(hextab[buf[i]&0x0F]);
        Serial.write(' ');
    }
}

This is incredibly confusing. Both of you are posting to two threads, about the same problem.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator