Hello,
i am using an arduino mega 2560 board to try and read serial data from an encoder, which communicates via a RS485 bus. I have connected a MAX485 module to the arduino with pin 18 connecting to DI, pin 19 connecting to RO and pin 8 connecting to DE/RE. Writing the command to read the current position works without problems and the answer is written to pin 19 clearly. When trying to read the answer into the arduino, however, i am not able to read it in correctly.
This is the code i am using:
void setup() {
Serial.begin(2000000);
Serial1.begin(2000000); //Using Serial1 Port
pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
Serial.println("Starting");
//setting position of encoder to zero
digitalWrite(8,HIGH);
Serial1.write(0x56);
Serial1.write(0x5E);
digitalWrite(8,LOW);
}
void loop() {
char getdata='m';
int buffersize = 4;
char buf[buffersize];
//making sure the input buffer is not filled with other data
while(Serial1.available() > 0) {
char t = Serial1.read();
Serial.print("inbetween1: ");
Serial.println(t, BIN);
}
digitalWrite(8,HIGH); //DE/RE=HIGH Transmit Enabled M1
Serial1.write(0x54); //request current position from encoder
//Serial1.flush();
digitalWrite(8,LOW); //DE/RE=LOW Receive Enabled M1
delay(50);
if(Serial1.available()){ //If Serial Data is available
while(Serial1.available() && getdata!='d')
{
int length = Serial1.readBytes(buf, buffersize);
Serial.print("I received: ");
for (int i = 0; i < length; i++) {
Serial.println(buf[i], BIN);
}
Serial.println("");
}
}
delay(1000);
}
i have also tried reading the data with the following while loop. It results in the same data read.
while(Serial1.available())
{
getdata=Serial1.read();
Serial.print("I received: ");
Serial.println(getdata, BIN);
}
Also, i have connected the arduino to itself with an additional MAX485 module, using Serial2 and its corresponding pins for that. Serial 2 was able to correctly read messages sent from Serial1.
Here are 2 example images with the message on the bus read with an oscilloscope and the incorrect data printed to the serial monitor (channel 3 was connected to pin 19, channel 2 to line B of the RS485 bus and channel 4 to pin 8):
The communication and especially the reading of the data to the input buffer should still be working fine at 2MHz, which i am using, correct? Writing the command to the bus at 2MHz works just fine after all.
Am i making a silly mistake somewhere in the code?
Is there a problem with the format the answer of the encoder is written in?
Here is a link to the datasheet of the encoder in case there are questions about it: https://www.mouser.de/datasheet/2/670/amt21_v-1775847.pdf
If i understood correctly, one Serial.read() command should only read one byte of data from the input buffer. Why are some of the lines printed on the serial monitor much longer than 8 bit?
The shorter ones should just be shorter due to zeroes in the front not being printed, correct?
Sorry if i mistyped something or did something else wrong in the post, first time posting here, please point it out to me ![]()
I appreciate any help/advice you have.
Thanks,
Fabian

