if you don't see [101_idle] but you see [101_idle(+ some other bs)] ;Code:printf( "[%s]", packetBuffer );
that could be your comparison problem.
if you don't see [101_idle] but you see [101_idle(+ some other bs)] ;Code:printf( "[%s]", packetBuffer );
that could be your comparison problem.
Last edited by Structure; 09-11-2020 at 09:30 AM.
"without goto we would be wtf'd"
also i believe when you do this:
Code:packetBuffer[len] = 0;
you actually want do do this:
Code:packetBuffer[len-1] = 0;
also, you are going to want to zero terminate both strings.
Last edited by Structure; 09-11-2020 at 09:31 AM.
"without goto we would be wtf'd"
char situationOne = "_available"
considering what this code does, this is disturbing.
Last edited by Structure; 09-11-2020 at 09:40 AM.
"without goto we would be wtf'd"
unknown_072: Your code in #11 still uses "+" for string concatenation. That does not work as laserlight mentioned in post #2. That needs to be fixed.
Structure: There's no need to reinvent the wheel. Your "compare" function in #5 can be simplified to something like this:
(I also added "const" to the parameters which you forgot to do.)Code:int compare(const char *string1, const char *string2) { return strcmp(string1, string2) == 0; }
which you forgot to do.
Last edited by Structure; 09-11-2020 at 09:51 AM.
"without goto we would be wtf'd"
I checked how to use strcat, but here also, I'm not sure how to read the PacketBuffer, I just want to verify if the 3 digits of the string that is received on the UDP packet are the same as tableID;
Structure:Code:if (packetBuffer, strcat(str1, str2)
I've rewinded my code back to basic, thought it was helpful to share a bit more code.
So monday I'll work on this again, first thing, since I really want to finish this piece of code.
It's clear to me that the "+" does not work like this in C. Hopefully I can work this out with your help.
Once again, I'm trying to simply read only the first 3 digits of the udp packet and want to compare them to the variable 'tableID'
Thanks so far!
You're already linking to libc, right? Then you have all of the string functions.
Sure, it can be useful as an academic exercise, but (IMO) it's pointless if you just want to write an application. Just use existing functions in libc. They're likely better tested and optimized than something you write yourself. Less code means less to debug.Although i used strlen, i'm saying it could be useful sometimes to create the one function you need.
When I saw that !strcmp(packetBuffer, "101_available")) works but you "Somehow (...) can't get the code correctly to make a correct comparison", and then I see tableID + "_available", it looked like you were trying to do string concatenation using operator + and that's why your comparison failed: you aren't constructing the resulting string correctly. I'm afraid that I didn't notice that you also inexplicably changed from using strcmp to using operator == instead, which would be another reason why your comparison failed: unlike your original test, you aren't comparing strings correctly either. I surmised that this is why Structure suggested a custom comparison function, but as both your own original test and christop noted, strcmp will do.Originally Posted by unknown_072
I may have made a mistake in mentioning strcat, even though generally it is a workable approach. In this case, you mentioned that "tableID is defined as byte". Presumably this means something like unsigned char, but you don't want to interpret it as a character; you want to interpret it as an integer string that is prepended to "_available". Hence, a better solution would be to use snprintf:Originally Posted by unknown_072
(If you're compiling with respect to pre-C99, you won't be able to use snprintf, but you can still use sprintf. However, then the onus is on you to prevent buffer overflow by checking that tableID really is in the range [0, 1000).)Code:char expected[14]; // NNN_available snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID); if (strcmp(packetBuffer, expected) == 0) { modeAvailable(); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi laserlight,
Thank you!
So I've changed my code:
I've declared expected
and declared expected;Code:snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID); if (strcmp(packetBuffer, expected) == 0) { modeAvailable(); Serial.println("This works!"); }
And when I upload this I do recieve the message, but it won't return true since I'm not seeing the Serial Print "This Works!"Code:char expected[14]; // NNN_available
This is what I'm seeing in my monitor when sending the packet:
08:02:05.419 -> Received packet of size 13
08:02:05.419 -> From 10.168.1.100, port 9101
08:02:05.419 -> Command: 101_available
08:02:05.454 -> --END--
All correct information.
But still it won't return true
Okay. Temporarily change this:
to this:Code:if (strcmp(packetBuffer, expected) == 0) { modeAvailable(); Serial.println("This works!"); }
Compile and run your code with exactly the same test input. Do you get "This works!" as the actual output?Code:if (strcmp(packetBuffer, "101_available") == 0) { modeAvailable(); Serial.println("This works!"); }
If the answer is yes, then it implies that there's still something wrong with how you're constructing the string. So, one place to start is to insert this before you do the string comparison and see what you get:
If the answer is no, then it implies that there's something else wrong, i.e., some other change you made has made your code become incorrect in between the time you wrote that "I'm sure the message arrives correctly, for debug I did a simple string comparison`!strcmp(packetBuffer, "101_available"));`, which works" in post #1 and now.Code:Serial.println(expected);
Last edited by laserlight; 09-14-2020 at 12:37 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay so I've tested this, and yes it works.
09:28:03.457 -> Received packet of size 13
09:28:03.457 -> From 10.168.1.100, port 9101
09:28:03.457 -> Command: 101_available
09:28:03.457 -> --END--
09:28:03.457 ->
09:28:03.457 -> Confirmed
09:28:03.457 -> This works!
If I add:
Nothing is printed, it is a loop so it should print constantCode:Serial.println(expected);
I wrote a quick program to check that I didn't make some silly mistake:Originally Posted by unknown_072
Compiling and running the above program results in this output, which is exactly what I expected:Code:#include <stdio.h> int main(void) { unsigned char tableID = 101; char expected[14]; // NNN_available snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID); printf("%s\n", expected); return 0; }
Therefore, what you have described is impossible (unless there is undefined behaviour elsewhere): there can be no value of tableID that will result in expected being an empty string.Code:101_available
To confirm, are you saying that you wrote this:
and it didn't print anything where Serial.println(expected) was supposed to print something?Code:char expected[14]; // NNN_available snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID); Serial.println(expected); if (strcmp(packetBuffer, expected) == 0) { modeAvailable(); Serial.println("This works!"); }
Last edited by laserlight; 09-14-2020 at 02:45 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This actually works now! I do have to clean up a bit but this my result from the serial monitor.
So, the tableID is currently 215, which is correct.Code:11:12:19.369 -> Received packet of size 13 11:12:19.369 -> From 10.168.1.100, port 9101 11:12:19.369 -> Command: 215_available 11:12:19.404 -> --END-- 11:12:19.404 -> 215_available 11:12:19.404 -> Confirmed 11:12:19.404 -> This works! 11:12:19.404 -> Expected: 215_available 11:12:25.782 -> Received packet of size 13 11:12:25.782 -> From 10.168.1.100, port 9101 11:12:25.782 -> Command: 101_available 11:12:25.782 -> --END-- 11:12:25.782 -> 215_available
With the 101_available it does print 'Expected' which obviously is correct when you look at the code, it's printing what it expects.
This is what I have;
Code:snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID); Serial.println(expected); if (strcmp(packetBuffer, expected) == 0) { modeAvailable(); Serial.println("This works!"); Serial.print("Expected: "); Serial.println(expected); }
I do think this did the trick, I'll work my code out today to see if it works as supposed with all the functions.
Really thanks for all the help! I'm happy now!