Getting serial interface to work

I want to make serial interface on Arduino mega .

I attached serial on pin 10 and 11

trying to do echo test but i only get rubisch on the recieving end :frowning:

my code look likes this :

#include <SoftwareSerial.h>
int incomingByte = 0;
SoftwareSerial mySerial(10, 11);
char receivedChar;
boolean newData = false;


void setup() {
  Serial.begin(115200); // opens serial port, sets data rate to 9600 bps
  mySerial.begin(115200);
  Serial.println("<Arduino is ready>\r\n");
  mySerial.println("<Arduino is ready>\r\n");
  char buffer[50];
  mySerial.readBytes(buffer, sizeof(buffer));
  Serial.println(buffer);

}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
     if (mySerial.available() > 0) {
     receivedChar = Serial.read();
     newData = true;
    }
}
void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
//        mySerial.print("OK");
        Serial.println(receivedChar);
        while (Serial.available() > 0) {
    Serial.read();
}
        newData = false;
        
    }
}

the out punt on serial side is of my pc is

<Arduino is ready>

<Arduino is ready>

and on the adrino side:

This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮
This just in ... ⸮

so the input is rubbisch

any ideas

The mega has 4 hardware serial ports, why would you want to use Software Serial?

To your question the code is weird….

No. Just no.
SoftwareSerial at 115200 just isn’t going to happen.

Also tryed 9600

As mentioned the code is weird, not sure what you are trying to achieve

You could send something to both ports in the setup and the loop could check if there is a byte waiting on a serial port (for both) and print it

Also you should not open the Serial monitor if you use Serial for the loop back so better take Serial1 and Serial2 and keep Serial for showing what’s going on and possibly as a source of text to send from one Serial to the other

I want to test my serial connection.

So i want send back what was recieved didn't know i could use both pyshcal serial and serial monitor.
en didn't know there where multilple serial interfaces.

so simplify the code now

int incomingByte = 0;
char receivedChar;
boolean newData = false;


void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial.println("<Arduino is ready>\r\n");
}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
     if (Serial.available() > 0) {
     receivedChar = Serial.read();
     newData = true;
    }
}
void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChar);
       newData = false;
        
    }
}

The problem was in serial0 now i use serial works directly

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