Pages: [1]   Go Down
Author Topic: Modbus protocol Implimentation  (Read 501 times)
0 Members and 1 Guest are viewing this topic.
DELHI
Offline Offline
God Member
*****
Karma: 8
Posts: 821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all,

I have Modbus library . I have below code . Is there any way to software to check Master slave Configuration.How to Configure Master. Since i am new to modbus protocol . Can some tell me How to test below code.

http://electronics.stackexchange.com/questions/49097/arduino-as-modbus-master-with-max485-doesnt-get-any-response




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

/*
This example code shows a quick and dirty way to get an
arduino to talk to a modbus master device with a
device ID of 1 at 9600 baud.
*/

//Setup the brewtrollers register bank
//All of the data accumulated will be stored here
modbusDevice regBank;
//Create the modbus slave protocol handler
modbusSlave slave;

void setup()
{   

//Assign the modbus device ID. 
  regBank.setId(1);

/*
modbus registers follow the following format
00001-09999  Digital Outputs, A master device can read and write to these registers
10001-19999  Digital Inputs, A master device can only read the values from these registers
30001-39999  Analog Inputs, A master device can only read the values from these registers
40001-49999  Analog Outputs, A master device can read and write to these registers

Analog values are 16 bit unsigned words stored with a range of 0-32767
Digital values are stored as bytes, a zero value is OFF and any nonzer value is ON

It is best to configure registers of like type into contiguous blocks.  this
allows for more efficient register lookup and and reduces the number of messages
required by the master to retrieve the data
*/

//Add Digital Output registers 00001-00016 to the register bank
  regBank.add(1);
  regBank.add(2);
  regBank.add(3);
  regBank.add(4);
  regBank.add(5);
  regBank.add(6);
  regBank.add(7);
  regBank.add(8);
  regBank.add(9);
  regBank.add(10);
  regBank.add(11);
  regBank.add(12);
  regBank.add(13);
  regBank.add(14);
  regBank.add(15);
  regBank.add(16);

//Add Digital Input registers 10001-10008 to the register bank
  regBank.add(10001); 
  regBank.add(10002); 
  regBank.add(10003); 
  regBank.add(10004); 
  regBank.add(10005); 
  regBank.add(10006); 
  regBank.add(10007); 
  regBank.add(10008); 

//Add Analog Input registers 30001-10010 to the register bank
  regBank.add(30001); 
  regBank.add(30002); 
  regBank.add(30003); 
  regBank.add(30004); 
  regBank.add(30005); 
  regBank.add(30006); 
  regBank.add(30007); 
  regBank.add(30008); 
  regBank.add(30009); 
  regBank.add(30010); 

//Add Analog Output registers 40001-40020 to the register bank
  regBank.add(40001); 
  regBank.add(40002); 
  regBank.add(40003); 
  regBank.add(40004); 
  regBank.add(40005); 
  regBank.add(40006); 
  regBank.add(40007); 
  regBank.add(40008); 
  regBank.add(40009); 
  regBank.add(40010); 
  regBank.add(40011); 
  regBank.add(40012); 
  regBank.add(40013); 
  regBank.add(40014); 
  regBank.add(40015); 
  regBank.add(40016); 
  regBank.add(40017); 
  regBank.add(40018); 
  regBank.add(40019); 
  regBank.add(40020); 

/*
Assign the modbus device object to the protocol handler
This is where the protocol handler will look to read and write
register data.  Currently, a modbus slave protocol handler may
only have one device assigned to it.
*/
  slave._device = &regBank; 

// Initialize the serial port for coms at 9600 baud 
  slave.setBaud(9600);   
}

void loop()
{
//put some data into the registers
  regBank.set(1, 1); 
  regBank.set(2, 1); 
  regBank.set(3, 0); 
  regBank.set(4, 1); 
  regBank.set(5, 1); 
  regBank.set(6, 0); 
  regBank.set(7, 1); 
  regBank.set(8, 0); 

  regBank.set(10001, 1);
  regBank.set(10002, 1); 
  regBank.set(10003, 1); 
  regBank.set(10004, 1); 
  regBank.set(10005, 0); 
  regBank.set(10006, 0); 
  regBank.set(10007, 0); 
  regBank.set(10008, 0); 


  regBank.set(30001,1);
  regBank.set(30002,2);
  regBank.set(30003,3);
  regBank.set(30004,4);
  regBank.set(30005,5);
  regBank.set(30006,6);
  regBank.set(30007,7);
  regBank.set(30008,8);
  regBank.set(30009,9);
  regBank.set(30010,10);

  regBank.set(40001,1);
  regBank.set(40002,2);
  regBank.set(40003,2);
  regBank.set(40004,4);
  regBank.set(40005,5);
  regBank.set(40006,6);
  regBank.set(40007,7);
  regBank.set(40008,8);
  regBank.set(40009,9);
  regBank.set(40010,10);
 
 while(1)
  {
    //put a random number into registers 1, 10001, 30001 and 40001
    regBank.set(1, (byte) random(0, 2));
    regBank.set(10001, (byte) random(0, 2));
    regBank.set(30001, (word) random(0, 32767));
    regBank.set(40001, (word) random(0, 32767));
   
     slave.run(); 
  }
}


Logged

AMPS

DELHI
Offline Offline
God Member
*****
Karma: 8
Posts: 821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



How to configure Slave Configuration Here.How to assign the Slave ID, SLAVE address, How to check output.I have uploaded Code. I have r2232 to rs 485 device. But i could not able to get commiicate

Code:
byte  data[12];
byte  address;
byte  function;
byte  function_code;
unsigned int data_received;
byte  byte_receive;
byte  state=0;
byte  cont1=1;
byte  trace_OK=0;
unsigned int checksum;
unsigned int checksum_trace;

#define  pinCONTROL    02
#define  myaddress     01
 
 
 
  void sendACK(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4)
{
 
  unsigned int checksum_ACK;
  checksum_ACK=address1+address2+6+data_type+code1+code2+Sign+data1+data2+data3+data4+3;
 
  digitalWrite(pinCONTROL,HIGH);
  delay(100);
  //Serial.println("Trama ACK");
  //Serial.print("Checksum:");
  //Serial.println(checksum_ACK,HEX);
  Serial.print(0,byte);
  Serial.print(address1,byte);
  Serial.print(address2,byte);
  Serial.print(6,byte);
  Serial.print(data_type,byte);
  Serial.print(code1,byte);
  Serial.print(code2,byte);
  Serial.print(Sign,byte);
  Serial.print(data1,byte);
  Serial.print(data2,byte);
  Serial.print(data3,byte);
  Serial.print(data4,byte); 
  Serial.print(3,byte);
  Serial.print(((checksum_ACK>>8)&255),byte);
  Serial.print(((checksum_ACK)&255),byte);
  delay(100);
  digitalWrite(pinCONTROL,LOW);

 
 
}


void setup() {

   pinMode(13,OUTPUT);
   pinMode(pinCONTROL,OUTPUT);
   digitalWrite(2,LOW);
   Serial.begin(9600);
   Serial.println("Empezamos");

}

void loop()
{

   while (Serial.available() > 0){
     
     byte_receive=Serial.read();
     if (byte_receive==00){
       //Serial.println("Se ha recibido byte Start");
       state=1;
       checksum_trace=0;
       checksum=0;
       trace_OK=0;
       address=0;
       data_received=0;
       cont1=1;
     }else if (state==1 && cont1<=12){
       data[cont1-1]=byte_receive;
       checksum=checksum+byte_receive;
       cont1=cont1+1;
     }else if (state==1 && cont1==13){
       checksum_trace=byte_receive<<8;
       cont1=cont1+1;
       //Serial.print("Primer byte Checksum");     
       //Serial.print(checksum_trace,HEX);
     }else if (state==1 && cont1==14){
       checksum_trace=checksum_trace+byte_receive;
       cont1=cont1+1;
       state=0;
       //Serial.println(byte_receive,HEX);
       //Serial.println("Recibida trama");
       //Serial.print("Checksum Trace= ");
       //Serial.println(checksum_trace,HEX);
       //Serial.print("Checksum= ");
       //Serial.println(checksum,HEX);
       //Serial.println(checksum,byte);
       //Serial.println("Trama= ");
       //Serial.print(data[0]);
       //Serial.print(data[1]);
       //Serial.print(data[2]);
       //Serial.print(data[3]);
       //Serial.print(data[4]);
       //Serial.print(data[5]);
       //Serial.print(data[6]);
       //Serial.print(data[7]);
       //Serial.print(data[8]);
       //Serial.print(data[9]);
       //Serial.print(data[10]);
       //Serial.println(data[11]);       

       if (checksum_trace==checksum){
         trace_OK=1;
         
         address=(hex2num(data[0])<<4)+(hex2num(data[1]));
         function=data[3];
         function_code=(hex2num(data[4])<<4)+(hex2num(data[5]));
         data_received=(hex2num(data[7])<<12)+(hex2num(data[8])<<8)+(hex2num(data[9])<<4)+(hex2num(data[10]));
         
         //Serial.println("TRAZA CORRECTA");
         //Serial.println(address,byte);
         //Serial.println(data_received);
         if (address==myaddress){
           if ((function=='D') && (function_code==0) && data[2]==5){
             if (data_received==1){
               digitalWrite(13,HIGH);
               //Serial.println(data_received,byte);
               sendACK(data[0],data[1],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10]);
             }else if (data_received==0){
               digitalWrite(13,LOW);
               sendACK(data[0],data[1],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10]);
             }
           }
         }
       }else{
         //Serial.println("TRAZA INCORRECTA"); 
         sendNAK(data[0],data[1],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10]);
       }
     }

  }
 
}



void sendNAK(byte address1,byte address2,byte data_type,byte code1,byte code2,byte Sign,byte data1,byte data2,byte data3,byte data4){
 
  unsigned int checksum_ACK;
  checksum_ACK=address1+address2+6+data_type+code1+code2+Sign+data1+data2+data3+data4+3;
 
  digitalWrite(pinCONTROL,HIGH);
  delay(100);
  //Serial.println("Trama ACK");
  //Serial.print("Checksum:");
  //Serial.println(checksum_ACK,HEX);
  Serial.print(0,byte);
  Serial.print(address1,byte);
  Serial.print(address2,byte);
  Serial.print(15,byte);
  Serial.print(data_type,byte);
  Serial.print(code1,byte);
  Serial.print(code2,byte);
  Serial.print(Sign,byte);
  Serial.print(data1,byte);
  Serial.print(data2,byte);
  Serial.print(data3,byte);
  Serial.print(data4,byte); 
  Serial.print(3,byte);
  Serial.print(((checksum_ACK>>8)&255),byte);
  Serial.print(((checksum_ACK)&255),byte);
  digitalWrite(pinCONTROL,LOW);
  delay(100);
 
 
}



byte hex2num(byte x){

  byte result;
 
  if (x>=48 && x<=57){
    result=x-48; 
  }else if (x>=65 && x<=70){
    switch(x){
      case 65:
        result=10;
        break;
      case 66:
        result=11;
        break;
      case 67:
        result=12;
        break;
      case 68:
        result=13;
        break;
      case 69:
        result=14;
        break;
      case 70:
        result=15;
        break;     
    }   
  }
  return result; 
}




Logged

AMPS

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5145
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't flood the forum with almost identical messages!
Logged

Pages: [1]   Go Up
Jump to: