Hi All,
I’m trying to communicate with an RS485 by going from a hardware serial port on my Arduino Mega2560 to a MAX488e chip to a tiltmeter. I give the command and I can see some data coming back, but I should be getting back more data than I am and what I do get back is gibberish. I checked the baud rate and that is correct. I know that the tiltmeter is working when wired through a store-bought RS485->USB dongle using hyperterm. And since I’m getting some response from the instrument, I think the wiring is correct. Maybe I’m doing something wrong with the programming, or…? Any help is appreciated.
Thank you,
Eli
Here is my program
// #include <SoftwareSerial.h>
#define LED 13
// #define RTS 31
// SoftwareSerial sensor = SoftwareSerial(11,10); // RX, TX
void setup() {
Serial.begin(19200);
Serial.println("Setup...");
Serial.flush();
// sensor.begin(19200);
Serial1.begin(19200);
// pinMode(RTS, OUTPUT);
delay(3000);
Serial.println("Sending '*9900XY-DUMP2' to Lilly...");
// digitalWrite(RTS, HIGH);
Serial1.println("*9900XY-DUMP2");
// digitalWrite(RTS, LOW);
Serial.println("command sent");
delay(100);
/*
for (int index = 0; index < 1000; index++) {
byte character = (byte) Serial3.read();
Serial.print("character is: ");
Serial.write(character);
Serial.println();
// delay(10);
}
*/
int character = 0;
// for (int index = 0; index < 1000; index++) {
while(Serial1.available() > 0) {
character = Serial1.read();
Serial.print("character is: ");
Serial.write(character);
Serial.println();
delay(10);
}
}
void loop() {
}
Here is the output I currently get
Setup…
Sending ‘*9900XY-DUMP2’ to Lilly…
command sent
character is: Ö
character is: ¶
character is: ¿
character is: W
character is: {
character is: -
character is: =
character is:
character is:
character is: ¿
character is:
character is: £
character is:
character is:
character is: ¿
character is: å
character is: ë
character is:
character is:
character is:
character is: ¿
character is: }
character is: ;
character is: !
character is:
character is: 5
character is: ¿
character is:
character is: £
character is:
character is:
character is: ¯
character is: i
character is: K
character is: e
character is: -
character is: #
character is: W
character is: 5
character is: %
character is:
character is:
character is:
character is: ¿
character is: R
character is:
character is:
character is:
character is: E
character is:
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is: ¿
character is:
character is: §
character is: ¿
character is: ’
character is: %
character is: [
character is:
character is:
character is:
character is: ~
character is: -
character is: e
character is: U
character is: ’
character is: ’
character is:
character is: u
character is:
character is: å
character is: ¯
character is: !
character is: å
character is: j
character is: 9
character is:
character is: 7
character is:
character is: ¿
character is:
character is: -
character is: ê
character is: ë
character is: Y
character is:
character is: =
character is: #
character is: w
character is:
character is: 3
character is:
character is: §
character is: ¿
character is:
character is: z
character is:
character is:
character is:
character is: -
character is:
character is:
character is:
character is:
character is: 1
character is: !
character is: ¿
character is: #
character is: 3
character is: u
character is: e
I’m a little concerned about this.
while(Serial1.available() > 0) {
character = Serial1.read();
Serial.print("character is: ");
Serial.write(character);
Serial.println();
delay(10);
}
For every character you receive from the tiltmeter, you’re asking the Arduino to send 15, and they’re being sent at the same baud rate as being received. I wonder if the input buffer on the Arduino is gettting overwritten as I don’t think the Arduino’s write buffer is 15 characters deep (meaning the code waits a bit to send those 15 chars).
Three things spring to mind. Can you use the USB-485 adapter that worked when tying the PC to tiltmeter, and use it to read the Arduino’s output ? And then to send, via hyperterminal, “data” from the PC to the Arduino serial port #1 ? That way you’d be sure the wiring is correct and the Arduino code can receive data … though perhaps only when sent 1 character at a time from the PC acting as tiltmeter.
Could you redo the code to store the whole message from the tiltmeter into RAM and then send it all, after it’s been received, to the PC ? Perhaps at least see if you can read and store “N” characters from the tiltmeter this way and see if those “N” characters look OK ?
Lastly you could increase the baud rate btw the Arduino and PC. Then those characters being sent get out quicker and the code, if it is waiting for the send buffer, won’t wait as long. Also reduce the number of characters sent, if only for this experiment (perhaps just echo the char received). Then the wait to send “all” the characters will be short enough so as not to cause an overrun the port1 receive buffer.
ps - I know the ATmega2560 has framing and overrun error flags. I don’t know how to read them. If you can figure this out, you can see if you’re having buffer overruns.
Hi,
Thanks. I just thought of your idea about trying to communicate with the computer over the RS485 connection a little bit before I read your post. YES. Good idea. I hadn’t thought that I was trying to send more data to the computer than I was reading and the Arduino maybe not keeping up. I think that’s a good notion. I’ll give that a shot as soon as I can and let you know what happens.
Thanks Again,
Eli
Hi All,
I don’t know if anyone has any ideas about this, but I’m stumped. I tried a test as suggested to go from a computer->RS485 dongle->RS485 chip->Arduino, and it can send and receive OK. However, we tried going from computer->RS485 dongle->RS485 chip->RS485 chip->tiltmeter (so we could test the entire chain using hyperterm) and while the command seems to go to the tiltmeter, the output to the computer in hyperterm comes back as a line of gibberish. This is the same result as when we connect the Arduino to the tiltmeter through our RS485 chip. So, while the chip looks like it is working OK in one configuration, it doesn’t seem to handling the receive correctly when connected to the tiltmeter. Maybe something going on there? Again the chip is a MAX488e. Any help would be appreciated.
Thanks,
Eli
Sounds like a polarity reversal in the lines going from the tiltmeter’s RS485 xmitter to the Arduino R485 receiver. Swap the + and - lines to verify or disprove this.
Hi again,
Thanks. We resolved the problem. It was indeed a polarity problem in the line. Good call! Also, we needed a longer delay between the send and receive, so the entire message could get transmitted out of the UART before waiting for the reply. Once we got that figured out, we got the message reply just fine. Also made sure the baud rate to the terminal was much greater than the baud rate of the tiltmeter and I reduced the message size to the computer to just characters that were coming back from the tiltmeter.
Thanks again for the suggestions!
Eli
Kewl ! :dance:
As the OP I think you can mark the thread solved … somehow. It’s good etiquette to do so as then others, looking to help, need not read the thread.
As for polarity, whenever there’s a serial link and the polarity could possibly be reversed; my experience is there’s an 87.3% probability that it’ll be wrong the first time you try it ! :lol:
If everyone followed the same format for labels, the A wire is - and the B wire is +. I can’t remember if this is called for in the actual RS485 standard, or if it became the de facto standard.