invalid conversion from 'byte' to 'const void*'

Hey, i am working with a adafruit nfc/rfid shield. I am trying to send a value from pc to arduino through serial.read() but i keep getting this error. what am i doing wrong?

//I2C:

#include <PN532_I2C.h>

#define IRQ   2
#define RESET 3

PN532 * board = new PN532_I2C(IRQ, RESET);
#include <Wire.h>
//end I2C -->



#include <Mifare.h>
Mifare mifare;
//init keys for reading classic
uint8_t Mifare::useKey = KEY_B;
uint8_t Mifare::keyA[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
uint8_t Mifare::keyB[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uint32_t Mifare::cardType = 0; //will get overwritten if it finds a different card

#include <NDEF.h>

#define PAYLOAD_SIZE 236
uint8_t payload[PAYLOAD_SIZE] = {};

void setup(void) {
  Serial.begin(115200);

  board->begin();

  uint32_t versiondata = board->getFirmwareVersion();
  if (! versiondata) {
    Serial.println("err");
    while (1); // halt
  }
  
  // Got ok data, print it out!
  Serial.print("5");Serial.println((versiondata>>24) & 0xFF, HEX); 
//  Serial.print("v: "); Serial.println((versiondata>>16) & 0xFF, DEC); 
//  Serial.println((versiondata>>8) & 0xFF, DEC);
//  Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX);
  


  if(mifare.SAMConfig()){
    Serial.println("ok");  
  }else{
    Serial.println("er");
  }
  
  
}



void loop(void) {
 uint8_t * uid = mifare.readTarget();
 if(uid){

    
    memset(payload, 0, PAYLOAD_SIZE);




//write plain text
byte stuff = Serial.read();

      memcpy(payload, stuff , 17);
      uint8_t len = NDEF().encode_TEXT((uint8_t *)"en", payload);
      

      
      boolean success = mifare.writePayload(payload, len);
      if(success == 1)
      {
      Serial.print ("success printing ");
      Serial.println (stuff);
      }
      else
      {
      Serial.println ("fail");
      }
      

 }
 delay(5000);
}

Hey, i am working with a adafruit nfc/rfid shield.

Sorry. That link doesn't work.

uint8_t payload[PAYLOAD_SIZE] = {};

What is the point of supplying an empty list of initializers?

byte stuff = Serial.read();

      memcpy(payload, stuff , 17);

You read one byte, then you lie to memcpy(), and expect it to copy 17 bytes. stuff is supposed to be an array.

Sorry about that

I am just using an example from this:

So i don't know what these initializers are for.

Oh. i thought it would just add in the bytes. So what should i do?

Once you make stuff an array, the error should clear (array names are pointers). FYI, assuming stuff was some other type of variable that was 17 bytes long, you would use memcpy like this...

memcpy(payload, &stuff , 17);

DavidOConnor:
Once you make stuff an array, the error should clear (array names are pointers). FYI, assuming stuff was some other type of variable that was 17 bytes long, you would use memcpy like this...

memcpy(payload, &stuff , 17);

oh okay. i haven't worked alot with arrays. So how would i do this?
i know there is a serial.readuntil function, which i should use i guess, but i don't know alot about arrays. i have looked at the arduino page about arrays, and been looking through google, but i don't quite understand it :confused:

kasperhangard:

DavidOConnor:
Once you make stuff an array, the error should clear (array names are pointers). FYI, assuming stuff was some other type of variable that was 17 bytes long, you would use memcpy like this...

memcpy(payload, &stuff , 17);

oh okay. i haven't worked alot with arrays. So how would i do this?
i know there is a serial.readuntil function, which i should use i guess, but i don't know alot about arrays. i have looked at the arduino page about arrays, and been looking through google, but i don't quite understand it :confused:

There are plenty of array tutorials out there. Take a look at this one...

http://www.cplusplus.com/doc/tutorial/arrays/

DavidOConnor:

kasperhangard:

DavidOConnor:
Once you make stuff an array, the error should clear (array names are pointers). FYI, assuming stuff was some other type of variable that was 17 bytes long, you would use memcpy like this...

memcpy(payload, &stuff , 17);

oh okay. i haven't worked alot with arrays. So how would i do this?
i know there is a serial.readuntil function, which i should use i guess, but i don't know alot about arrays. i have looked at the arduino page about arrays, and been looking through google, but i don't quite understand it :confused:

There are plenty of array tutorials out there. Take a look at this one...

http://www.cplusplus.com/doc/tutorial/arrays/

Aight' so i got this working

int  serIn;             
char serInString[100];                          
int  serInIndx  = 0;    
int  serOutIndx = 0;    
 

void readSerialString () {
    int sb;   
    if(Serial.available()) {  
       while (Serial.available()){ 
          sb = Serial.read();             
          serInString[serInIndx] = sb;
          serInIndx++;                              
       }
    }  
}
void printSerialString() {
   if( serInIndx > 0) {
      Serial.print("Arduino memorized that you said: ");     
      for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
          Serial.print( serInString[serOutIndx] );      
      }        
      serOutIndx = 0;
      serInIndx  = 0;
      Serial.println();
   } 
}
 
void setup() {
  Serial.begin(9600);  
}
 
void loop () {
  readSerialString();
  printSerialString();
  delay(2000);
}

I just don't know how to transfer this into the memcpy. :confused:
Also i don't really understand what memcpy is anyways. exept it unloads memory.

kasperhangard:
Also i don't really understand what memcpy is anyways. exept it unloads memory.

It's a standard 'C' runtime library function and the name gives you a pretty good clue what it does. If it isn't obvious enough then you could look it up using your favorite search engine with less effort than it took to ask the question here.

exept it unloads memory.

No, it doesn't. You need to follow up on PeterH's suggestion, and find out what memcpy() actually does.

Aight. So it copies memory, but what does it copy, and to where, in my code?
I have been spending 8 hours on this today, and i might have entered a tunnel with it..

kasperhangard:
Aight. So it copies memory, but what does it copy, and to where, in my code?
I have been spending 8 hours on this today, and i might have entered a tunnel with it..

This link should provide all the details about memcpy

http://www.cplusplus.com/reference/cstring/memcpy/

kasperhangard:
Aight. So it copies memory, but what does it copy, and to where, in my code?
I have been spending 8 hours on this today, and i might have entered a tunnel with it..

The documentation tells you what arguments memcpy() takes and what they are each used for. There are only three, and each of them is used in a very simply way which relates directly to what the function does, so I feel that if you looked at a specific call to memcpy() you ought to be able to work out what it's doing.

OKAY, so i now understand how memcpy works, i have also found strcpy, which i also know how works. i think.
I am trying to do this:

      text = Serial.readString();
      strcpy(from, text );
      memcpy(payload, from, 17);

where

String text;
char from[17];

this gives me:
error: cannot convert 'String' to 'const char*' for argument '2' to 'char* strcpy(char*, const char*)'

What i do not understand here, is that in
strcpy(from, text );
text is supposed to be a const char*. this means that if i write:
strcpy(from, "test" );
It Works perfectly, as it is infact const.
but how do i make it work with serial.read? this can't be a const. :confused:

Don't mix Strings and strings; either use one or the other. Most here will recommend using strings, as the hardware you are working on has a limited amount of memory and can't handle fragmentation nearly as well as your average desktop computer.

As far as your issue goes, string functions expect an array of chars that is null-terminated. Nowhere in your code do you explicitly append a null terminator to your string. You should be doing that after you add a char to your buffer and increment the index. So for example, say you sent "Hello" through the serial monitor. This is what your current code would do with your array:

{ 'H' }
{ 'H', 'e' }
{ 'H', 'e', 'l' }
{ 'H', 'e', 'l', 'l' }
{ 'H', 'e', 'l', 'l', 'o' }

Appending the null will make it look like this:

{ 'H', '\0' }
{ 'H', 'e', '\0' }
{ 'H', 'e', 'l', '\0' }
{ 'H', 'e', 'l', 'l', '\0' }
{ 'H', 'e', 'l', 'l', 'o', '\0' }

Functions like strcpy, strcmp, etc., require that null so that they know when to stop parsing. It would also be a good idea to remove that delay and only print after you've received some sort of a delimiter character, such as a new line.

Arrch:
Don't mix Strings and strings; either use one or the other. Most here will recommend using strings, as the hardware you are working on has a limited amount of memory and can't handle fragmentation nearly as well as your average desktop computer.

As far as your issue goes, string functions expect an array of chars that is null-terminated. Nowhere in your code do you explicitly append a null terminator to your string. You should be doing that after you add a char to your buffer and increment the index. So for example, say you sent "Hello" through the serial monitor. This is what your current code would do with your array:

{ 'H' }

{ 'H', 'e' }
{ 'H', 'e', 'l' }
{ 'H', 'e', 'l', 'l' }
{ 'H', 'e', 'l', 'l', 'o' }




Appending the null will make it look like this:



{ 'H', '\0' }
{ 'H', 'e', '\0' }
{ 'H', 'e', 'l', '\0' }
{ 'H', 'e', 'l', 'l', '\0' }
{ 'H', 'e', 'l', 'l', 'o', '\0' }




Functions like strcpy, strcmp, etc., require that null so that they know when to stop parsing. It would also be a good idea to remove that delay and only print after you've received some sort of a delimiter character, such as a new line.

Hi. Thanks alot for your answer! :slight_smile:
whenever i declare string, instead of String it gives me an error.
string without the capital S gives me: 'string' does not name a type

How would i go about adding the null-terminator?
I am very eager to learn all of this! :slight_smile:

Also, are you talking about the println command? it doesn't print after a delay, it prints when success has been triggered. which is when the NFC shield writes to a nfc chip. :slight_smile:

You declare a string like this...

char myString[16];

This gives you one string able to hold 15 characters.

DavidOConnor:
You declare a string like this...

char myString[16];

This gives you one string able to hold 15 characters.

Oh right! a string is just an array of characters?

So i tried this now

while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
       if(index < 16) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
      memcpy(payload, inChar, 17);
      uint8_t len = NDEF().encode_TEXT((uint8_t *)"en", payload);

where

char inData[17]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

but that gives me:
TrackR_Write.ino: In function 'void loop()':
TrackR_Write:102: error: invalid conversion from 'char' to 'const void*'
TrackR_Write:102: error: initializing argument 2 of 'void* memcpy(void*, const void*, size_t)'

i don't get it :confused:

kasperhangard:
Hi. Thanks alot for your answer! :slight_smile:
whenever i declare string, instead of String it gives me an error.
string without the capital S gives me: 'string' does not name a type

Strings are actual objects:

String my_String;

strings are a concept that are implemented by char arrays:

char my_string[10];

kasperhangard:
How would i go about adding the null-terminator?

Keep track of the index at which you are at and use that index:

my_string[index] = '\0';

I am very eager to learn all of this! :slight_smile:

kasperhangard:
Also, are you talking about the println command? it doesn't print after a delay, it prints when success has been triggered. which is when the NFC shield writes to a nfc chip. :slight_smile:

No, it prints after it has more than a single character; the delay just masks the symptom of printing indiscriminately.

kasperhangard:
but that gives me:
TrackR_Write.ino: In function 'void loop()':
TrackR_Write:102: error: invalid conversion from 'char' to 'const void*'
TrackR_Write:102: error: initializing argument 2 of 'void* memcpy(void*, const void*, size_t)'

i don't get it :confused:

memcpy expects two arrays; you're giving it an array and a char. The question, though, is why do you feel the need to copy data from inChar or inData into payload?

Arrch:

kasperhangard:
but that gives me:
TrackR_Write.ino: In function 'void loop()':
TrackR_Write:102: error: invalid conversion from 'char' to 'const void*'
TrackR_Write:102: error: initializing argument 2 of 'void* memcpy(void*, const void*, size_t)'

i don't get it :confused:

memcpy expects two arrays; you're giving it an array and a char. The question, though, is why do you feel the need to copy data from inChar or inData into payload?

well, i thought that inChar or inData would hold the characters? i suppose that isn't the case.
how would i then take data from serial.read() and put it into payload?

thanks alot for your time :slight_smile: