Using max485 to interface energy meter with arduino.

Look at the link you just posted and then look at the URL at the top of your browser right now.
See that number 227808.15;
Notice that is the same number as the link.(ie: you posted the link to this post)

oh sorry sorry my bad here is the link

http://arduino-info.wikispaces.com/RS485-Brick

Here is the code for the host S:

/* YourDuino RS485 Master Node S Example
 terry@yourduino.com */

/*-----( Declare Variables )-----*/
int EN = 2;

void setup() /****** SETUP: RUNS ONCE ******/
{
  pinMode(EN, OUTPUT );
  Serial.begin (19200);
}//--(end setup )---

void loop()    /****** LOOP: RUNS CONSTANTLY ******/
{
  // Send Data
  digitalWrite(EN, HIGH ); // enable send
  Serial.print ( 'A' );
  delay(1000);
  Serial.print ( 'B' );
  delay (1000);
}//--(end main loop )---

Here is the code for the slave CA:

/* YourDuino RS485 Slave Node A Example
 terry@yourduino.com */

/*-----( Declare Variables )-----*/
int ledPin=13;
int EN = 2;
int Val;

void setup()/****** SETUP: RUNS ONCE ******/
{
  pinMode(ledPin, OUTPUT );
  pinMode(EN, OUTPUT );
  Serial.begin (19200);
}//--(end setup )---

void loop()  /****** LOOP: RUNS CONSTANTLY ******/
{
  // receive Data
  digitalWrite (EN, LOW ); // enable receive
  Val = Serial.read ();
  if (-1 != Val) 
  {
    if ( 'A' == Val) 
    {
      digitalWrite (ledPin, HIGH );
      delay (500);
      digitalWrite (ledPin, LOW );
      delay (500);
    }
  }

}//--(end main loop )---

Here is your code:

 int EN = 2;
int Val;

void setup() /****** SETUP: RUNS ONCE ******/
{
  pinMode(EN, INPUT );
  Serial.begin (19200);

}
void loop()    /****** LOOP: RUNS CONSTANTLY ******/
{
  digitalWrite (EN, LOW ); // enable receive
  Val = Serial.read ();
  if (-1 != Val) 
  {
    if ( 'A' == Val) 
    {
      Serial.print ('A');
    }
    if ( 'B' == Val)
    {
      Serial.print ('B');
    }
  }
}
« Last Edit: March 22, 2014, 08:40:24 pm by AES-ASH »

Attached is an image of your circuit:
Attached is an image of the energy meter.
Do you know what HOST and SLAVE mean ?
Can you tell me which one the meter is and which one the arduino is ?

Come on man I already said im terrible at this stuff you can tell by my replies, I swear im doing my best.i majored in electrical engineering not in communication

I think the host here is the arduino and the slave is the energy meter

AES-ASH, If I were you I'd try first talking to the energy meter with the PC. Yes, the pc can emulate a MODBUS MASTER you only need and USB-RS485 adapter and the software MODBUS TESTER (or compatible). If you can talk properly to the device, then I would move to the next step: communicating Arduino with the energy meter.

Regards.

I might be wrong but I think the Host is the Sender and the Slave is the receiver. Your are using the Host code to Receive. Not only thatI don't know if the Host needs the Address of the Slave. If so and the Recdiver can be the Host you might in the correct mode (Host) butstill need the Slave Address. That is something you can only know by knowing how RS485 works, which I don,t.
You could try running the Slave code or better yet, look at the code to see which code has arduino commands. If I am not mistaken,
thise programs were written for two arduinos Each driving an RS485 chip. Check the code to confirm/deny that.

I found this at the link you got your code:

Below are example Software Sketches that show the Master sending data to the two slaves, and controlling their LEDs. Each slave is in receive mode and receives the same data.

In order for that to happen BOTH the HOST and the SLAVES must have MAX485 chips or equivilent
Read this:

I have a 75176 RS485 Transceiver chip here. Looking at the application
notes, it seems pretty straight forward to hook up, however I'm wondering.

Should I add a Jumper and a termination 120 ohm Resistor Between A and B?
Should I install any pull ups or Pull downs on the 485 data signal lines?
Does A pull down and B pulls Up?

Just wanting to do this 1 time. I will be using it strickly in half duplex
(For ModBus Mode).

Thanks for any pointers.
Richard

Don't add any pull up or pull down
There needs to be a termination resistor at the end of the cable run.

My point ? (Is the termination resistor necessary for short distances like yours ?
Also , when you look at your code, you have to ask yourself :
"What are the chances that right now the meter is sending a signal like this ?

 // Send Data
  digitalWrite(EN, HIGH ); // enable send<= SEE THIS ? IT SAYS SEND. (The Host IS the SENDER)
  Serial.print ( 'A' );
  delay(1000);
  Serial.print ( 'B' );

and then there is this:

  // receive Data
  digitalWrite (EN, LOW ); // enable receive<= SEE THIS ? It says receive. (This is the SLAVE code)

So what does this mean for the future of your project ?
Well the good news is you are using the SLAVE code to receive.:

digitalWrite (EN, LOW ); // [b]enable receive[/b]
  Val = Serial.read ();

The bad news is you are waiting for the meter to send you the character 'A', which I think it is safe to say, is never going to happen. So what can you do ?
I would start by changing the code to ECHO ANYTHING and EVERYTHING sent by the meter.
Have you installed the MODBUS library and tried the Basic sketch yet ? See attached. (you need to install library first)

Try using this example to modify your code:

MAX485.pdf (440 KB)

SYS600_IEC 1107 ProtocolENc.pdf (434 KB)

Basic.pde (1.8 KB)

Here's your problem, this thing most likely wont just respond to random characters. It looks to me to be a Modbus slave. Like all Modbus slaves it will not respond unless it receives a valid Modbus request. You will need a Modbus Master library to get anything done. Second, you are going to need a register map to know what registers hold what data. More than likely you are going to have multiple registers to hold a single value like total kWh as these values tend to grow much larger than what a standard 16bit modbus register holds. Usually you will see two registers used to hold a 32bit integer or sometimes a float.

Using a meter to check the RS485 port is probably going to be pointless. Modbus is a polling protocol where devices which are generally slaves do not do any type of broadcasting. The slave devices will only speak when spoken to.

The required information you will need to obtain is:
Baud Rate
Slave ID (address)
Register Map

With that you will be able to communicate with the device. The ModbusMaster library works pretty good. With RS485 you will also need to make sure you're shifting the RE-DE line on the level shifter. I've found that once you shift to TX and do your modbus request there are many times you need to delay() for 1-2ms before going low again to allow the TX buffer to clear or you might drop the last character and the slave wont respond. I've used the ModbusMaster with good success on a fair amount of industrial equipment with only a slight mod to the library regarding slave timeout.

Hope this helps a little.

Have you installed the MODBUS library and tried the Basic sketch yet ? See attached. (you need to install library first)

I have mentioned the library a couple of times already...

Ok i have installed the modbus library, thanks raschemmel for uploading the basic.pde file, now all i did to the pde file was add serial.begin and serial print as shown below:

 /*

  Basic.pde - example using ModbusMaster library
  
  This file is part of ModbusMaster.
  
  ModbusMaster is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  ModbusMaster is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with ModbusMaster.  If not, see <http://www.gnu.org/licenses/>.
  
  Written by Doc Walker (Rx)
  Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
  
*/

#include <ModbusMaster.h>


// instantiate ModbusMaster object as slave ID 2
// defaults to serial port 0 since no port was specified
ModbusMaster node(2);


void setup()
{
  // initialize Modbus communication baud rate
  node.begin(19200);
 Serial.begin(19200);
}


void loop()
{
  static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];
  
  i++;
  
  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
  
  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));
  
  // slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);
  
  // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  result = node.readHoldingRegisters(2, 6);
  
  // do something with data if read is successful
  if (result == node.ku8MBSuccess)
  {
    for (j = 0; j < 6; j++)
    {
      data[j] = node.getResponseBuffer(j);
      Serial.print (data[j]);
     delay(1);
    }
  }
}

Now the serial monitor displays some weird characters instead of nothing. Ok we have some progress,
than i read the comment that madpenguin8 posted
and here is the link for the:

Regmap :http://labjack.com/support/modbus/map
SlaveID (address) i don;t even know what this is
the baud rate (How can i find the appropriate baud rate to read from my meter, im still searching though)

S

Check out my post i did one interface between Arduino one and a power meter using RS485

http://forum.arduino.cc/index.php?topic=241019.msg1728702#msg1728702

But not the power meter you show here, with another one called Acuvim-DL

I was reading your code I thing you must use a library like Easy-modbus-master, because modbus protocol has some standard things that it really need to exist before any response from the device.

Did anyone find the solution to the problem? If yes, then please share.

Hola,

Tal vez alguien me pueda ayudar, estoy creando un proyecto donde comunico dos arduinos nano con los módulos MAX485, mi problema es que el modulo max485 que se encuentra al lado del transmisor se calienta demasiado, trasmito por un cable con una longitud de 50 metros, intente corregir utilizando resistencias de 120 ohm en los dos extremos pero no tuve éxito.

Si alguien me puede ayudar les quedare muy agradecido