Hello everyone
I have such a problem here. Please help me solve it.
void setup() {
Serial.begin(9600);
Serial.println("Start!");
}
void loop() {
if (Serial.read == "NO CARRIER") { //there's a mistake here, I can't think of the logic
Serial.println("ekinshi nomer");
}
}
The idea is this. If the Serial receives the text "NO CARRIER", then the code will find out and do something. For example
Serial.println("ERROR");
Thank you!
if (Serial.read == "NO CARRIER")
The first mistake is that Serial.read is a function so that the line should read
if (Serial.read() == "NO CARRIER")
However, Serial.read() reads a single character so that won't work either
See Serial input basics - updated
for some ideas as to how to read a string of characters before comparing them with a constants
Or look at the Serial.readString() function if you don't mind using Strings
Please tell me where I made a mistake?
String a;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
while (Serial.available()) {
a = Serial.readString(); // read the incoming data as string
if (a == "NO CARRIER") {
Serial.println("ERROR");
}
}
}
What have you got the Line ending set to in the Serial monitor ?
works in no end of line mode
Use the command Serial.readStringUntil('\r\n'). And let the serial monitor as default
After adding to my code, it lags a lot. That is, even the usual inscription in the serial "at" response is not returned
a = Serial.readStringUntil();
It's right?
Put Serial.readStringUntil('\r\n') complete, you are telling to read until a couple ASCII characters, this \r\n
is a line separator.
Slightly changed the code. But please tell me what is wrong?
void setup() {
Serial.begin(9600);
Serial.setTimeout(50);
}
void loop() {
if (Serial.available()) {
String str = Serial.readStringUntil('NO CARRIER');
Serial.println(str);
if (str) {
Serial.println("norm");
}
}
}
Serial outputs this text
NO CA
norm
norm
IE
norm
norm
Do you mean code like this?
String readString;
void setup() {
Serial.begin(9600);
Serial.println("Start!");
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() > 0) {
Serial.print(readString);
if (readString == "NO CARRIER") {
Serial.println("norm");
}
readString = "";
}
}
Closer, but not there yet
Look at the examples in the link. Do you see any Strings used ?
Is it possible to find out which example suits us best?
Updated Version
Please note that this is a revised version of the advice in this earlier Thread which has become very long. As far as possible I have kept the code examples identical or simplifed them slightly. It should not be necessary to refer to the older Thread, but feel free to do so.
Contents
The following sections are in this Tutorial
Introduction
Serial data is slow by Arduino standards
Example 1 - Receiving single characters
Why code is organized into functions
Exampl…
Fully studied. I tried it in practice. But I didn't notice a special and suitable code. Please help me figure it out?)
come on.
Which of the examples have you put in your IDE, compiled, uploaded to your Arduino, tested with the serial monitor?
Yes. Tested all the examples. Do you mean that example 5 suits me?
But I don't see what logic suits me yet.
noiasca
October 8, 2022, 10:22am
20
based on example 2 I have added one single line:
/*
based on Example https://forum.arduino.cc/t/serial-input-basics-updated/382007
https://forum.arduino.cc/t/value-in-serial-arduino/1039522/16
will be deleted 2022-11
*/
// Example 2 - Receive with an end-marker
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
if (!strcmp(receivedChars, "NO CARRIER")) Serial.println(F("matched")); // One single line added
}
}
and here you can read about strcmp
1 Like
Thanks!
But does this code work in esp8266?
Because everything works on the site. But in practice, no.
It works on the site with esp 32.