Sabtu, 23 Januari 2016

Pengetahuan Dasar Modbus RTU dengan Arduino dan VB net

Berikut ini contoh kode slave modbus dengan Arduino



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

// initialize the library with the numbers of the interface pins
#define delaylcd 20

int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor1 = 6;
const int sensor2 = 7;
const int sensor3 = 8;
const int sensor4 = 9;

// variables will change:
int sensor1State = 0;
int sensor2State = 0;
int sensor3State = 0;
int sensor4State = 0;
/*
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()
{
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
lcd.begin(16, 2);
//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 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);

/*
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(40001,1);
regBank.set(40002,2);
regBank.set(40003,2);
regBank.set(40004,4);
regBank.set(40005,5);
regBank.set(40006,6);
lcd.setCursor(0, 0);
lcd.print(“in-out1 in-out2”);

while(1)
{
delay(delaylcd);

if (digitalRead(sensor1) == LOW)
{
delay(delaylcd);
counter1++;
lcd.setCursor(0, 1);
lcd.print(counter1);

lagi:
delay(delaylcd);
if(digitalRead(sensor1) == LOW)
{ goto lagi;}
delay(delaylcd);

}
//======================================
//===========================

if (digitalRead(sensor2) == LOW)
{
delay(delaylcd);
counter2++;
lcd.setCursor(4, 1);
lcd.print(counter2);

lagi2:
if(digitalRead(sensor2) == LOW)
{ goto lagi2;}
delay(delaylcd);

}
//======================================

//===========================
if (digitalRead(sensor3) == LOW)
{
delay(delaylcd);
counter3++;
lcd.setCursor(9, 1);
lcd.print(counter3);
lagi3:
if(digitalRead(sensor3) == LOW)
{ goto lagi3;}
delay(delaylcd);

}
//======================================
//=========================================
//===========================
if (digitalRead(sensor4) == LOW)
{
delay(delaylcd);
counter4++;
lcd.setCursor(13, 1);
lcd.print(counter4);
regBank.set(40001, (word) random(0, 32767));
lagi4:
if(digitalRead(sensor4) == LOW)
{ goto lagi4;}
delay(delaylcd);

}
//======================================
//regBank.set(40001, (word) random(0, 32767));
// lcd.setCursor(1, 1);
// lcd.print(regBank.get(40003));

slave.run();
}
}

Tidak ada komentar:

Posting Komentar