Arduino Mega and RS485

Hello everybody,

I am working on a project where I am trying to utilize an existing RS485 communication system to control my Arduinos. I purchased a MAX 485 which is a board that can read RS485 signals and transfer them to the Arduino.

I was attempting to see what kind of signals the current system is sending when a command is issued so that I could use these same signals to control the Arduino. I tried a few different methods of retrieving the data, but it is always very strange. Below is one piece of a code i tried:

void loop() {

  String serialData1;

  if (Serial1.available() > 0) {
    Serial.println(F("Serial data found"));
    serialData1 = Serial.readString();
    Serial.println(serialData1);
  }
}

I didn't think this way would work very well, and it returned things, but nothing that made any sense. I got values like, ˆ8$þ˜Šÿ, and ˆ˜¬˜Šÿ.

I then tried this:

void loop() {

  char serialData1;

  if (Serial1.available() > 0) {
    Serial.println(F("Serial data found"));
    serialData1 = Serial.read();
    Serial.println(serialData1);
  }
}

This just prints ÿ over and over again. I also tried have many Serial.read() statments to grab all of the data that is printed. One thing that was always consistent was that there were 7 values/letters printed for all commands, and there were the same group of values/letters when the same command was inputted.

I'm not sure how I should go about grabbing the data from the external RS485 network. The data doesn't necessarily need to make sense, it just needs to something that I can use to compare. For example if the data output was 5jqw35, I would want to be able to say something like:

if (serialData1 == "5jqw35") {
  code
}

If anyone has any suggestions or could let me know what I'm doing wrong it would be greatly appreciated. If you need to know any more information, please let me know. Thank you.

michaelsm:

if (serialData1 == "5jqw35") {

code
}

You declared serialData1 as a char.

The above code is wrong and will not work.

ieee488:
You declared serialData1 as a char.

The above code is wrong and will not work.

In a situation like that, serialData1 would be a String and not a char. That's my mistake for not correcting that.

michaelsm:
In a situation like that, serialData1 would be a String and not a char. That's my mistake for not correcting that.

serialData1 should stay a char

It is your other code that needs to change.

Looks like a baud rate mismatch. Have you verified that you're using the right speed?

I was using a mega and go the same issue. The wired chanters are caused by the baud rate.

Make sure that you have the same baud rate in Serial.begin(baud rate); and the baud rate in your serial monitor.

135843:
I was using a mega and go the same issue. The wired chanters are caused by the baud rate.

Make sure that you have the same baud rate in Serial.begin(baud rate); and the baud rate in your serial monitor.

Graynomad:
Looks like a baud rate mismatch. Have you verified that you're using the right speed?

In response to both of you, it was baud rate related, but I did have the my baud rates the same in my code and in the serial monitor. The issue was actually that my baud rate wasn't high enough. I had it set to 9600 before, and I changed it to 115200. Having it on this baud rate gave me highs and lows, which is exactly what I was looking for.