Kepware-Modbus Rtu-Arduino TTL Converter.

Hi guys. :frowning:

In my company we get information from electric meters with modbus protocol on Kepware OPC Server. Some devices do not have this protocol. So i think we can use arduino for these situations.

By now I can get analog values from device. I have to send these values to Kepware Opc Server.

In our system we are using RS485 cables for get information from devices and end of the line we have TCP/ IP Converter to switch.

I m using RS485 TTL Convertor.

https://urun.n11.com/adaptor-ve-cevirici/arduino-rs-485-ttl-to-rs485-4287a-modul-max485-cevirici-adaptor-s-P62510821?cid=604001&gclid=EAIaIQobChMI_e6c-ML_1gIVJjPTCh1oOgavEAQYAiABEgJmRfD_BwE&gclsrc=aw.ds

When I use below code other devices on this line can't read from OPC server and so my system with arduino is crushed.

Other devices link

The library I use is located here:
http://code.google.com/p/arduino-modbus-slave/downloads/detail?name=MODBUS.zip&can=2&q=

My code is:

#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>

/* PINS
Add more registers if needed
Digital input pins 2,3,4,5,6,7
Digital output pins 8,9,12,13
Analog output pins 10,11 (PWM)
Analog input pins 0,1,2,3,4,5
*/


modbusDevice regBank;
modbusSlave slave;

int AI0,AI1,AI2,AI3,AI4,AI5;


void setup()
{   
regBank.setId(5); ///Set Slave ID

//Add Digital Input registers
regBank.add(10002);
regBank.add(10003);
regBank.add(10004);
regBank.add(10005);
regBank.add(10006);
regBank.add(10007);
// Add Digital Output registers
regBank.add(8);
regBank.add(9);
regBank.add(12);
regBank.add(13);
//Analog input registers
regBank.add(30001);
regBank.add(30002);
regBank.add(30003);
regBank.add(30004);
regBank.add(30005);
regBank.add(30006);
//Analog Output registers
regBank.add(40010);  
regBank.add(40011);  

slave._device = &regBank;  
slave.setBaud(9600);   

pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

}
void loop(){

while(1){   
//Digital Input
 byte DI2 = digitalRead(2);
 if (DI2 >= 1)regBank.set(10002,1);
 if (DI2 <= 0)regBank.set(10002,0);
 byte DI3 = digitalRead(3);
 if (DI3 >= 1)regBank.set(10003,1);
 if (DI3 <= 0)regBank.set(10003,0);
 byte DI4 = digitalRead(4);
 if (DI4 >= 1)regBank.set(10004,1);
 if (DI4 <= 0)regBank.set(10004,0);
 byte DI5 = digitalRead(5);
 if (DI5 >= 1)regBank.set(10005,1);
 if (DI5 <= 0)regBank.set(10005,0);
 byte DI6 = digitalRead(6);
 if (DI6 >= 1)regBank.set(10006,1);
 if (DI6 <= 0)regBank.set(10006,0);
 byte DI7 = digitalRead(7);
 if (DI7 >= 1)regBank.set(10007,1);
 if (DI7 <= 0)regBank.set(10007,0);
                             
//Digital output
 int DO8 = regBank.get(8);
   if (DO8 <= 0 && digitalRead(8) == HIGH)digitalWrite(8,LOW);
   if (DO8 >= 1 && digitalRead(8) == LOW)digitalWrite(8,HIGH);
 int DO9 = regBank.get(9);
   if (DO9 <= 0 && digitalRead(9) == HIGH)digitalWrite(9,LOW);
   if (DO9 >= 1 && digitalRead(9) == LOW)digitalWrite(9,HIGH);
 int DO12 = regBank.get(12);
   if (DO12 <= 0 && digitalRead(12) == HIGH)digitalWrite(12,LOW);
   if (DO12 >= 1 && digitalRead(12) == LOW)digitalWrite(12,HIGH);
 int DO13 = regBank.get(13);
   if (DO13 <= 0 && digitalRead(13) == HIGH)digitalWrite(13,LOW);
   if (DO13 >= 1 && digitalRead(13) == LOW)digitalWrite(13,HIGH);
         
//Analog input  ***READ Twice deliberately
 AI0 = analogRead(0);
 delay(10);
 AI0 = analogRead(0);
 regBank.set(30001, (word) AI0);
 delay(10);
 
 AI1 = analogRead(1);
 delay(10);
 AI1 = analogRead(1);
 regBank.set(30002, (word) AI1);
 delay(10);
 
 AI2 = analogRead(2);
 delay(10);
 AI2 = analogRead(2);
 regBank.set(30003, (word) AI2);
 delay(10);
 
 AI3 = analogRead(3);
 delay(10);
 AI3 = analogRead(3);
 regBank.set(30004, (word) AI3);
 delay(10);
 
 AI4 = analogRead(4);
 delay(10);
 AI4 = analogRead(4);
 regBank.set(30005, (word) AI4);
 delay(10);
 
 AI5 = analogRead(5);
 delay(10);
 AI5 = analogRead(5);
 regBank.set(30006, (word) AI5);
 delay(10);
     
//Analog output 
 word AO10 = regBank.get(40010);
 analogWrite(10,AO10);
 delay(10);
 word AO11 = regBank.get(40011);
 analogWrite(11,AO11);
 delay(10);
     
slave.run();  
}
}

I try to read slave code. What ı have do wrong. If u have code for this job, Please help me, I m newbie. :frowning:

in Modbus RTU 'slave' is 'server' and 'master' is 'client'. are you sure the Arduino should be slave with id 1?

I guess slave is arduino and opc server kepware is master. I changed id to 5 for arduino, nothing is changed.

so the kepware has a list of slave device ids and makes requests to them to collect data.

why id 5?

bacause id 1 2 3 4 is full. 5 was empty. please help me :=)

atraks:
In my company we get information from electric meters with modbus protocol on Kepware OPC Server. Some devices do not have this protocol. So i think we can use arduino for these situations.

kepware asks the devices?

atraks:
By now I can get analog values from device. I have to send these values to Kepware Opc Server.

send? kepware should ask them. or?

atraks:
When I use below code other devices on this line can't read from OPC server and so my system with arduino is crushed.

what devices read from kepware server?

atraks:
I try to read slave code. What ı have do wrong.

slaves can't read. they have registers for master to read.

Juraj:
kepware asks the devices?

send? kepware should ask them. or?

what devices read from kepware server?

slaves can't read. they have registers for master to read.

i read electrical kW Value from this device with opc server no problem.

Yes Kepware ask devices for their values.

Sorry for my bad english. U can see that on my code, i have registers from analog pins or dijital pins, they are waiting for request from opc server.

thanks for your try.

and the problem is when you connect the RS485 adapter or when you start the slave sketch?

Please post a link to the ModBus library you're using!

Please learn to use the code tags! Take a look at your code and see how it got mangled by the forum system. The button in the editor is labeled </>.

Remove all delay() calls from your code! These delays wait for 140ms per loop and are just wasted time!

pylon:
Please post a link to the ModBus library you're using!

Please learn to use the code tags! Take a look at your code and see how it got mangled by the forum system. The button in the editor is labeled </>.

Remove all delay() calls from your code! These delays wait for 140ms per loop and are just wasted time!

thank u for your advice.

i set my modbus libraries link to topic. i did what u need for code on forum :slight_smile:

i ll delete delays.

Juraj:
and the problem is when you connect the RS485 adapter or when you start the slave sketch?

yes

Juraj:
and the problem is when you connect the RS485 adapter or when you start the slave sketch?

A) the problem is when you connect the RS485 adapter

B) the problem is when you start the slave sketch

A or B?

Juraj:
A) the problem is when you connect the RS485 adapter

B) the problem is when you start the slave sketch

A or B?

Code is working in every arduino device. Problem exist when i use arduino with Rs485 shield on R485 line. Other devices on line is crashed and i cant read value from opc any device , arduino too. I guess i use wrong code or m i have to use enable pin for R485 Ttl shield. I dont know

I guess i use wrong code or m i have to use enable pin for R485 Ttl shield. I dont know

The module you use need the driver enable signal but the library you're using isn't able to generate one. One possibility is to connect RE, DE and TX and puling RX to Vcc with 10k but that's just a workaround, I would not use this in a productive environment.

Please post a wiring diagram of your setup.

i tried it, nothing heppen.

Please read the last sentence of my last post.