bask185
November 26, 2022, 11:04pm
1
I am setting up 2 wemos D1 mini in a mesh. The connection and sending works, but reading the message keeps crashing on sscanf() and I can't tell why
void incommingMessage( uint32 from, String msg )
{
uint16_t tokenState ;
uint16_t tokenID ;
char char_array[32];
Serial.printf("echoNode: Received from %u msg=%s\n", from, msg.c_str());
strcpy(char_array, msg.c_str());
Serial.println("string copied to char array") ;
Serial.println(char_array) ;
sscanf( msg.c_str(), "%d,%d", &tokenID, &tokenState ) ; // <-- catastrophic failure
Serial.println("array scanned") ;
token[ tokenID ] = tokenState ; // update token with state
printNumber_("token ", tokenID ) ; // macro to print txt and number
printNumberln("= ",tokenState ) ; // macro to print txt and number
}
AFAIK I am doing it right. I use 16 bit variables for %d. And I did not forgot the &. The message also has the correct format. In this case 50,50
The output upon receiving is:
echoNode: Received from 2485300209 msg=50,50
string copied to char array
50,50
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Without sscanf(), the wemos does not crash.
Dit I do something wrong or am I looking at a bug?
Regards,
Bas
bask185:
sscanf( msg.c_str(), "%d,%d", &tokenID, &tokenState ) ; // <-- catastrophic failure
Quick glance. Shouldn't it be
sscanf(char_array, "%d,%d", &tokenID, &tokenState ) ; // <-- catastrophic failure
?
alto777
November 26, 2022, 11:37pm
3
I agree, but only because it would be clear to me. And I suggest doing it as a test anyway.
I don't use Strings, but shouldn't
msg.c_str()
return a pointer to char ?
a7
sscanf( msg.c_str(), "%d,%d", &tokenID, &tokenState ) ;
%d scans a 4 byte integer, you need bigger targets, or a different format.
I think %hd is used for 16 bit entities.
https://en.cppreference.com/w/cpp/io/c/fscanf
bask185
November 27, 2022, 8:01am
6
I have been unclear.
This..
sscanf( msg.c_str(), "%d,%d", &tokenID, &tokenState ) ; // <-- catastrophic
and this
sscanf( char_array), "%d,%d", &tokenID, &tokenState ) ; // <-- catastrophic
I both tried. I started out with the char array as that is indeed the proper way like you point out. Later I changed it to msg.c_str() simply to see if it would make a change.
We got a winner though! (me because my wemos does not crash anymore).
jfjlaros:
%d scans a 4 byte integer, you need bigger targets, or a different format.
I think %hd is used for 16 bit entities.
Using uint32_t instead of uint16_t and using the char array seems to work. Apparently sscanf behaves differently with an 8 bit uController than on a Wemos. For an arduino Nano, I had to use 16 bit variables
The output:
echoNode: Received from 2485300209 msg=51,50
string copied to char array
51,50
array scanned <-- Got a winner!
token 51 = 50
Now I just need me to change the printing so that 51 and 50 become 3 and 2 respectively. That I can do.
Tnx for the assist!
Regards,
Bas