Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 11 months ago.
How can i make this code work?
Hi everyone,
I have been working around for several weeks in this code. I am trying to establish a connection between a X_NUCLEO_NFC01A1 and nucleo-L432KC to send text from the NFC to the microcontroller but when i try to do a modification the code stops working. This code works: I have two files, the first one is the main.cpp when there is only a call for the other function, the other file contains a buffer that contains text previously converted from an int using sprint command.
//Main file int main (void) { sampleSync_writeAndChangeAll(); } //The other file static void write_nfc_tag(NDefLib::NDefNfcTag &tag) { bool writeStatus=false; bool closeStatus=false; if(tag.open_session()) { printf("Open session\r\n"); NDefLib::Message msg; char buffer [50]; int n, a=6, b=3; n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);//buffer to print NDefLib::RecordText rText3(NDefLib::RecordText::UTF8,"it",buffer); msg.add_record(&rText3);
What i would like to do is creating the buffer in the main file and pass it to the other file. But when i try to do it this way i obtain several errors. This code was already posted as an answer for a previous question i asked here in the forum.
//main file int main(void) { char buffer[50]; int size = sprintf(buffer, "blablabla"); write_nfc_tag(buffer, size + 1); // size+1 because sprintf() does not return the last \0 character in the count } //the other file static void write_nfc_tag(NDefLib::NDefNfcTag &tag) { bool writeStatus=false; bool closeStatus=false; if(tag.open_session()) { printf("Open session\r\n"); NDefLib::Message msg; int write_nfc_tag(char *buffer, int size) { NDefLib::RecordText rText3(NDefLib::RecordText::UTF8,"it",buffer); msg.add_record(&rText3); } }
This produces several erros: "Expected a ;" after the function and problems related to block scope
Thank you in advance for your time.
1 Answer
6 years, 11 months ago.
It's a little hard to comment seeing only part of the code. In code block one, your main() function is suspiciously short. But I don't know what function, sampleSync_writeAndChangeAll(), does. If this function returns then you will also end up exiting main() which is normally not a good idea. Normally expect at least an infinite while(1); loop at the end of main(). And then the second function you are showing, write_nfc_tag() is not the function you are calling from main(). So I don't know what to make of it.
In Block 1, Function write_nfc_tag() has formatting errors. You are missing closing braces in two locations.
In Block 2 , the function write_nfc_tag() should not be declared static. Static means you can only call it from within the same module. Main() won't be able to call it. You seem to have two definitions for write_nfc_tag(), is this intentional? Function overloading is a thing but probably not a good idea if functions are fundamentally doing different things. You still have problems with Closing Braces "}" in second block function write_nfc_tag(). You declared write_nfc_tag() as returning an int, but it is not returning anything at the moment. The two boolean variables appear to not be used anywhere here. Your passing of buffer as a char * looks right.
In terms of missing braces, it should look more like this.
//the other file static void write_nfc_tag(NDefLib::NDefNfcTag &tag) { bool writeStatus=false; // These two bools are not used. bool closeStatus=false; if(tag.open_session()) { printf("Open session\r\n"); NDefLib::Message msg; // Made a new variable here but do not do anything with it. } } void write_nfc_tag(char *buffer, int size) { NDefLib::RecordText rText3(NDefLib::RecordText::UTF8,"it",buffer); msg.add_record(&rText3); }
Best of luck.