How do I get a string from the com port and check it?

how do I get a string from the com port and check it ?

#include <MD5.h>              // connect the library
int val =12345;               // the variable is equal to five characters 12345
String stringOne, stringTwo;  // declaration of 2 lines, to address in any part of the code???

void setup(){
Serial.begin(9600);           // enable port monitor, set speed
}

void loop() {
char buff[5];                              // char = int val
itoa(val, buff, DEC);                      // char = int val
unsigned char* hash=MD5::make_hash(buff);  // hashing 12345
char *md5str = MD5::make_digest(hash, 16); // hashing 12345
free(hash);                                // hashing 12345
Serial.println(md5str);                    // we print the hash 12345 it is equal to = 827ccb0eea8a706c4c34a16891f84e7b

stringTwo = Serial.readString();           // stringTwo must be equal to "827ccb0eea8a706c4c34a16891f84e7b" 
Serial.println(stringTwo);                 // for tracking, you can specify stringTwo = "827ccb0eea8a706c4c34a16891f84e7b"; that's how it works, but you need to read from the port

stringOne = "827ccb0eea8a706c4c34a16891f84e7b";   // line 1 == hash that is needed
if (stringOne == stringTwo) {                     // compare hashes 1 and 2, and if there are matches // ANALOG if (stringOne.equals(stringTwo)) {
Serial.println(stringOne + " == " + stringTwo); } // print there is a match
}

See Serial input basics - updated

Another serial overview:
Arduino Tutorial - Lesson 4 - Serial communication and playing with data (ladyada.net)

With the Arduino standard core, one typically acquire serial data one-character-at-a-time.

Characters can be assembled into Strings or strings.
Some of my old stuff:

Don't Cross The Streams (FP scientific calculator serial co-processor) - Community / Exhibition / Gallery - Arduino Forum

The QBF - "The Quick Brown Fox..." For Serial Diags - Community / Exhibition / Gallery - Arduino Forum

You’re very. rave expecting members to jump to an unascribed anonymous link…

Better to post what you have to show, or at the very least state what the link is.

Most of the competent helpers simply won’t look at a link like that.

I do not see an answer there, I tried to rewrite this code many times as indicated in the lessons, but there is no result, this part of the code does not work, stringtwo = Serial.ReadString();
it works stringTwo = "827ccb0eea8a706c4c34a16891f84e7b"; but you need not specify, but read from the com port

first of all post your latest complete sketch. The complete sketch. It doesn't matter if the sketch is 2000 lines long. You can add some hints where you assume where the problem might be.

You can post code by using this method that adds the code-tags
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

If your code does not work there are two ways to analyse the problem:

  1. adding a second serial interface and listening to serial-debug-output with an USB-to-TTL-adapter.

If you just send you could use the standard serial monitor to send and receive the serial debug-output

going up from a small testprogram that simply sends a few characters and print the received characters to the serial-monitor and working up to the complete complexity from there

best regards Stefan

Using the String class can cause hard to diagnose memory problems. See the evils of Strings.

The readString() function is blocking which can make your code less responsive.

Using the methods in Robin2's serial input basics tutorial (linked by @UKHeliBob) will avoid both of those problems.

There is another library that can do non-blocking-receiving of strings / characters.
And here is a link to a short tutorial that has an example-code

thank you, you should probably give up string comparison or compare every character, which is inconvenient... and the speed will decrease .. but there is probably no other way

hm user 3f_x is the thread-opener and gives himself the advice

To me this seems a little bit strange.... hm what could be the reason ......

these are not tips, these are my thoughts

#include <MD5.h>
int val =12345;
char target[] = "827ccb0eea8a706c4c34a16891f84e7b";

void setup(){
Serial.begin(9600);
}

void loop() {
if (Serial.find(target))
Serial.println("found");
char buff[5];
itoa(val, buff, DEC);
delay(1000);
unsigned char* hash=MD5::make_hash(buff);
char *md5str = MD5::make_digest(hash, 16);
free(hash);
Serial.println(md5str); // 827ccb0eea8a706c4c34a16891f84e7b
free(md5str);
delay(1000);
if (Serial.print(target)) {
Serial.println("found"); }
}

probably the best option of all that I have tried, but unfortunately it prints the hash twice, if you have other options, please show the code, prove the code

I highly recommend as the first step to use a very simple test-code that does just test if serial receiving works at all. No hash-calculation no use of Serial.find().

The reference explains about find()
Serial.find() reads data from the serial buffer until the target is found. The function returns true if target is found, false if it times out.

it is very likely that there is happening a timeout everytime.
You should really do a primary test with simply receiving whatever character come in and print the received characters 1:1 to the serial monitor

best regards Stefan

My five cents:
When you get a string from UART - it will potentially have the NEWLINE (NL) or CARRIAGE RETURN (CR) (when you hit enter to finish the input for the line).
If you compare now two strings - they never match (one has a CR or NL as input string) - the other not. I might never match, except: you limit the number of characters to compare or you remove any unwanted characters you get from UART (such as NL and/or CR).

Debug (print) what you get after UART call...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.