Receiving data over Serial

Hi!
I have a little problem receiving data over serial.
I never used the Serial.read() function before so i made a simple circuit in Tinkercad to test this for my use.
The circuit is just 2 Arduino Uno's:
GND -> GND
TX -> D10
RX -> D11

Transmitter:

void setup()
{
  Serial.begin(9600);
  pinMode(12, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    Serial.println("§1.1$");
  	digitalWrite(LED_BUILTIN, HIGH);
  	delay(1000); // Wait for 1000 millisecond(s)
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
  
}

Receiver:

#include <SoftwareSerial.h>

boolean espRead = false;
boolean stringReady = false;
String readString;
String charString;

SoftwareSerial ESPSerial(10,11);

void setup()
{
  Serial.begin(19200);
  pinMode(LED_BUILTIN, OUTPUT);
  ESPSerial.begin(9600);
}

void loop(){
  while (ESPSerial.available()){
    delay(10);
    if (ESPSerial.available()>0){
      char c = ESPSerial.read();
      charString = c;
      //Serial.println(charString); //             TEST
      if (charString == "$"){
        espRead = false;
        stringReady = true;
      }else if (charString == "§"){
        espRead = true;
        readString = "";
      }
      if (espRead == true){
      	readString += c;
      }
    }
  }
  if (stringReady == true){
    Serial.println(readString);
    stringReady = false;
  }

}

The Transmitter prints "§1.1$" every 2 seconds to serial monitor. If i run the code, it's printing a "A" with cedil on top befor the expected signs. Where did this come from? ^^
2)
The Receiver should read the incomming signs so i can work with it. But the serial monitor dont't shows anything. If i uncomment the "TEST" line in Receiver code it just shows me strange signs but not the expected "§1.1$".

The §$ signs are just for recognizing start and end of the sended code. The point is also just vor seperating.
I also tried it an other way with Serial.readString or Serial.readStringUntil but can't get it to work.

Please, can anyone help me "repairing" my code? I don't want a copy-paste-answer because i want to learn how to use the Serial.read function.

Thank you all and a happy new year.

Maybe the carriage return/linefeed that you are sending after the string

SoftwareSerial ESPSerial(10,11)

A strange name for a link to a Uno

https://forum.arduino.cc/t/the-qbf-the-quick-brown-fox-for-serial-diags/229468

Probably needs a refresh for the old code, but allows you to Rx, TX, or loopback.

this approach seems awkward. while the delay should insure that a complete message is received within one iteration of the while loop, if an incomplete message is received, stringReady will be set to false preventing complete reception. and you i doubt you want the delay in a final application.

consider

#include <SoftwareSerial.h>
boolean espRead = false;
boolean stringReady = false;
String readString;
String charString;
SoftwareSerial ESPSerial(10,11);
void setup()
{
    Serial.begin (9600);
    pinMode(LED_BUILTIN, OUTPUT);
    ESPSerial.begin(9600);
}

void loop()
{
#if 1
    if (ESPSerial.available()>0){
        char c = ESPSerial.read();
#else
    if (Serial.available()>0){
        char c = Serial.read();
#endif
        readString += c;

        if ('$' == c)
            stringReady = true;
    }

    if (stringReady == true){
        Serial.println (readString);
        readString  = "";
        stringReady = false;
    }
}

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