RS485 working example

Hi,

I need sample code for the RS485 module. I have Rx and Tx wired to 10 and 11 and 3 is the control. Ideally separate codes for the transmitting and receiving sides. I read a few tutorials, but they I can't get them to work. I won't post them because they are bits and pieces from other people's projects that they were struggling with.

Thanks

https://www.gammon.com.au/forum/?id=11428

sed003: Hi,

I need sample code for the RS485 module. I have Rx and Tx wired to 10 and 11 and 3 is the control. Ideally separate codes for the transmitting and receiving sides. I read a few tutorials, but they I can't get them to work. I won't post them because they are bits and pieces from other people's projects that they were struggling with.

Thanks

Which example code? Where did you get it?

Wired to pins 10 and 11 on which Arduino model?

ard_newbie: https://www.gammon.com.au/forum/?id=11428

Thanks. Looks complete. I will try it this evening.

adwsystems: Which example code? Where did you get it?

Wired to pins 10 and 11 on which Arduino model?

It doesn't matter. They are all the same. Whichever example I was going to adjust my wiring accordingly. Anyway, I have the example of Nick Gammon. Will try that and see how it works.

sed003: It doesn't matter. They are all the same. Whichever example I was going to adjust my wiring accordingly. Anyway, I have the example of Nick Gammon. Will try that and see how it works.

Then you have something else wrong. The examples from the libraries I tested all worked fine. Pick one. Tell us which one and detail the wiring. I don't know why you would connect to pins 9 and 10. They do not have a serial port connected to them.

I am using Uno and Mega 2560.
I spent a lot of time trying different codes, but not sure what is wrong. I am not using a 120ohm resistor since I am only using two converters (I hope this is not the issue).

A & B wires are about 20cm.

Details:
Uno
Pin 2 → R0
Pin 3 → D1
Pin 4 → DE & RE (jumpered together)

Mega
Pin 2 → R0
Pin 3 → D1
Pin 4 → DE & RE (jumpered together)

A on converter 1 connected to A on converter 2
B on converter 1 connected to B on converter 2
VCC to +5V (External source)
GND to GND to arduino GND on both units

Tried different codes, but these are the ones I am interested in:
Note: I see activity on the Uno (Tx), but nothing on the Mega (Rx).
I tried to swap roles, but got the same thing. No activity on the receiving.

Your help is appreciated.

Sending

#include <RS485_non_blocking.h>

size_t fWrite (const byte what)
{
  return Serial.write (what);  
}

RS485 myChannel (NULL, NULL, fWrite, 0);

void setup ()
{
  Serial.begin (115200);
  myChannel.begin ();
}  // end of setup

const byte msg [] = "Hello world";

void loop ()
{
  myChannel.sendMsg (msg, sizeof (msg));
  Serial.write(msg, sizeof(msg));
  Serial.println();
  delay (1000);   
}  // end of loop

Receiving

#include <RS485_non_blocking.h>

 int fAvailable ()
   {
   return Serial.available ();  
   }
 
 int fRead ()
   {
   return Serial.read ();  
   }
 

RS485 myChannel (fRead, fAvailable, NULL, 20);

void setup ()
  {
  Serial.begin (115200);
  myChannel.begin ();
  }  // end of setup

void loop ()
  {
  if (myChannel.update ())
    {
    Serial.write (myChannel.getData (), myChannel.getLength ()); 
    Serial.println ();
    }
  }  // end of loop

You don't say where you got the libraries from, so no one can download and assist you.

Where are the pins numbers for RS-485 TX, RX, and DE/RE specified. Obviously no the portion of the program you have posted.

https://www.gammon.com.au/forum/?id=11428

I already specified the pinouts

Uno Pin 2 (D2) -> R0 Pin 3 (D3) -> D1 Pin 4 (D4) -> DE & RE (jumpered together)

Mega Pin 2 (D2) -> R0 Pin 3 (D3) -> D1 Pin 4 (D3) -> DE & RE (jumpered together)

sed003:
I already specified the pinouts

Not where you have them wired, where do you have them specified, defined, in the program!?

How do you know the pins you have them connected to are the pins the program is expecting you to have them connected to?

adwsystems:
How do you know the pins you have them connected to are the pins the program is expecting you to have them connected to?

Ah, I see what you mean now. Actually I didn’t. I just wired them according to his example, but having said this I just double checked the cpp file and found those two lines

const byte STX = '\2';
const byte ETX = '\3';

I don’t see anything related to the control line.

I also tried other codes that manually does what Nick’s library does with the same result.

For example:

This is my Source:
http://qqtrading.com.my/blog/rs485-serial-communication-between-arduino-mega-and-arduino-pro-mini

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  char getdata = 'c';

  digitalWrite(13, LOW);
  digitalWrite(8, LOW);

  if (getdata == '9') {
    digitalWrite(8, HIGH);
    Serial.print("AcruxTek");
    Serial.print("IsId");
  }
}

and

#include <SoftwareSerial.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(8, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  char getdata = 'm';
  digitalWrite(8, HIGH);
  Serial.print('9');

  digitalWrite(8, LOW);
  delay(1000);

  if (Serial.available()) {

    while (Serial.available() && getdata != 'd')
    {
      getdata = Serial.read();
      Serial.print(getdata);
    }
    Serial.println("");
  }
}

You have posted you have Tx/Rx connected to pins 2 and 3. The link to the blog site shows pins 0 and 1. I have no idea what

const byte STX = '\2';
const byte ETX = '\3';

is for plus you can't find a definition for the pin of the control.

I suggest searching around here and select a RS-485 library that appears to be better supported.

If you want to avoid the control line (with the benefit of full duplex), you can use two pairs of modules, on for Tx only and one for Rx only. Then you will be able to see if you have other problems.

Did you see the other link? It doesn't use any libraries and it clearly mentions pins D13 and D8, but same result.

Do you have a posted example of how to wire those 4 units and the code for them? Actually all I wanted for starting is to have one unit as the sender and one as the receiver and try to send text from one to the other and see how it works, then I can expand from there.

sed003: Did you see the other link? It doesn't use any libraries and it clearly mentions pins D13 and D8, but same result.

Different library. Different setup. You can't take the wiring from one setup and program and use it directly with another. This stuff rarely/doesn't work that way.

sed003: Do you have a posted example of how to wire those 4 units and the code for them?

You don't need (specific RS-485) code for for that setup. That's the point.

sed003: Actually all I wanted for starting is to have one unit as the sender and one as the receiver and try to send text from one to the other and see how it works, then I can expand from there.

You say want to start simple, but are starting complex. So let's back up and do what you say.

Step 1: Put aside the RS-485 modules. Can you

sed003: have one unit as the sender and one as the receiver and try to send text from one to the other

and get that to work? Just connect the Tx/Rx pins you intended to use with the RS-485 modules, together and connect the grounds. When you can get data sent between them, then go to step 2. You need to actually do this, not just say you can.

Step 2: Replace one TX/RX line with a pair of modules, connecting DE/RE to 5V or ground. This replaces one of the wires between the modules, with a RS-485 differential pair. There is not code change required. If step one works and this step doesn't then there is a wiring problem.

Step 3: Repeat step 2 for the other RX/TX wire. There is not code change required. If step two works and this step doesn't then there is a wiring problem.

Step 4: Now that you know you have the wiring correct, connect the DE/RE wires to the Arduinos (this will be two wires to one arduino pin). Now you can work to get the code for the DE/RE pins working.

Step 5: With step 4 working, remove one pair of the modules which will put the setup in final form. There is no code change required to get this working. If step four works and this step doesn't then there is a wiring problem.

sed003: Hi,

I need sample code for the RS485 module. I have Rx and Tx wired to 10 and 11 and 3 is the control. Ideally separate codes for the transmitting and receiving sides. I read a few tutorials, but they I can't get them to work. I won't post them because they are bits and pieces from other people's projects that they were struggling with.

Thanks

How to connect Uno With Max 485 module to read energy meter reading is any one post the code Sadanand