Arduino Modbus TCP Client Example

0% found this document useful (0 votes)
161 views4 pages
The code initializes an Ethernet connection and Modbus protocol implementation. It defines data arrays to store sensor readings and settings. Based on user input, it can read/write data from…

Uploaded by

Lâm Dũng
  • Ethernet Setup
  • Modbus Communication

#include <SPI.

h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;
int inByte = 0; // incoming serial byte

// Ethernet settings (depending on MAC and Local network)


byte mac[] = {0x00, 0x1A, 0xB6, 0x02, 0xD1, 0x14 };
IPAddress ip(192, 168, 0, 35);

void setup()
{

// serial setup
[Link](9600);
[Link]("Serial interface started");

// initialize the ethernet device


[Link](mac, ip); // start etehrnet interface
[Link]("Ethernet interface started");

// print your local IP address:


[Link]("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
[Link]([Link]()[thisByte], DEC);
[Link](".");
}
[Link]();

// slave address
[Link] = (192,168,0,1);

// Fill MbData
// [Link](0,false);
[Link][0] = 1;
[Link][1] = 2;
[Link][2] = 3;
[Link][3] = 4;
[Link][4] = 5;
[Link][5] = 6;
[Link][6] = 0;
[Link][7] = 0;
[Link][8] = 0;
[Link][9] = 0;
[Link][10] = 0;
[Link][11] = 0;

// print MbData
for (int i=0;i<12;i++) {
[Link]("address: "); [Link](i); [Link]("Data: ");
[Link]([Link][i]);
}
// print menu
[Link]("0 - print the first 12 words of the MbData space");
[Link]("1 - FC 1 - read the first 5 coils from the slave and store
them in the lower byte of MbData[1]");
[Link]("2 - FC 2 - read the first 5 discrete inputs from the slave
and store them in the higer of the MbData[1]");
[Link]("3 - FC 3 - read the first 5 registers from the slave and
store them in MbData[3..7");
[Link]("4 - FC 4 - read the first 5 input registers from the slave
and store them in MbData[8..12]");
[Link]("5 - FC 5 - write coil 0 of the slave with the bit valeu of
MbData[0.0]");
[Link]("6 - FC 6 - write register 0 of the slave with MbData[2]");
[Link]("7 - FC 15 - write 5 coils of the slave starting with coil 0
with GetBit(16..20");
[Link]("8 - Fc 16 - write 5 registers of the slave starting on
register 0 with MbData[0..4]");

[Link]([Link]);
}

void loop()
{
if ([Link]() > 0) {
// get incoming byte:
inByte = [Link]();
if (inByte == '0') { // print
MbData
for (int i=0;i<12;i++) {
[Link]("address: "); [Link](i); [Link]("Data: ");
[Link]([Link][i]);
}
}
if (inByte == '1') {[Link](MB_FC_READ_COILS, 6,6,6);} //
1 // ref, count, pos
if (inByte == '2') {[Link](MB_FC_READ_DISCRETE_INPUT, 6,6,6);} // 2
if (inByte == '3') {[Link](MB_FC_READ_REGISTERS, 6,6,6);} // 3
if (inByte == '4') {[Link](MB_FC_READ_INPUT_REGISTER, 6,6,6);} // 4
if (inByte == '5') {[Link](MB_FC_WRITE_COIL, 0,0,0);} //
5 // count can be x
if (inByte == '6') {[Link](MB_FC_WRITE_REGISTER, 7,0,0);} //
6 // count can be x
if (inByte == '7') {[Link](MB_FC_WRITE_MULTIPLE_COILS, 0,6,0);} // 15
if (inByte == '8') {[Link](MB_FC_WRITE_MULTIPLE_REGISTERS, 0,6,0);} // 16
}

[Link]();
// [Link]();
}
#include <SPI.h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;
int inByte = 0; // incoming serial byte

// Ethernet settings (depending on MAC and Local network)


byte mac[] = {0x00, 0x1A, 0xB6, 0x02, 0xD1, 0x14 };
IPAddress ip(192, 168, 0, 35);

void setup()
{

// serial setup
[Link](9600);
[Link]("Serial interface started");

// initialize the ethernet device


[Link](mac, ip); // start etehrnet interface
[Link]("Ethernet interface started");

// print your local IP address:


[Link]("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
[Link]([Link]()[thisByte], DEC);
[Link](".");
}
[Link]();

// slave address
[Link] = (192,168,0,1);

// Fill MbData
// [Link](0,false);
[Link][0] = 1;
[Link][1] = 2;
[Link][2] = 3;
[Link][3] = 4;
[Link][4] = 5;
[Link][5] = 6;
[Link][6] = 0;
[Link][7] = 0;
[Link][8] = 0;
[Link][9] = 0;
[Link][10] = 0;
[Link][11] = 0;

// print MbData
for (int i=0;i<12;i++) {
[Link]("address: "); [Link](i); [Link]("Data: ");
[Link]([Link][i]);
}
// print menu
[Link]("0 - print the first 12 words of the MbData space");
[Link]("1 - FC 1 - read the first 5 coils from the slave and store
them in the lower byte of MbData[1]");
[Link]("2 - FC 2 - read the first 5 discrete inputs from the slave
and store them in the higer of the MbData[1]");
[Link]("3 - FC 3 - read the first 5 registers from the slave and
store them in MbData[3..7");
[Link]("4 - FC 4 - read the first 5 input registers from the slave
and store them in MbData[8..12]");
[Link]("5 - FC 5 - write coil 0 of the slave with the bit valeu of
MbData[0.0]");
[Link]("6 - FC 6 - write register 0 of the slave with MbData[2]");
[Link]("7 - FC 15 - write 5 coils of the slave starting with coil 0
with GetBit(16..20");
[Link]("8 - Fc 16 - write 5 registers of the slave starting on
register 0 with MbData[0..4]");

[Link]([Link]);
}

void loop()
{
if ([Link]() > 0) {
// get incoming byte:
inByte = [Link]();
if (inByte == '0') { // print
MbData
for (int i=0;i<12;i++) {
[Link]("address: "); [Link](i); [Link]("Data: ");
[Link]([Link][i]);
}
}
if (inByte == '1') {[Link](MB_FC_READ_COILS, 6,6,6);} //
1 // ref, count, pos
if (inByte == '2') {[Link](MB_FC_READ_DISCRETE_INPUT, 6,6,6);} // 2
if (inByte == '3') {[Link](MB_FC_READ_REGISTERS, 6,6,6);} // 3
if (inByte == '4') {[Link](MB_FC_READ_INPUT_REGISTER, 6,6,6);} // 4
if (inByte == '5') {[Link](MB_FC_WRITE_COIL, 0,0,0);} //
5 // count can be x
if (inByte == '6') {[Link](MB_FC_WRITE_REGISTER, 7,0,0);} //
6 // count can be x
if (inByte == '7') {[Link](MB_FC_WRITE_MULTIPLE_COILS, 0,6,0);} // 15
if (inByte == '8') {[Link](MB_FC_WRITE_MULTIPLE_REGISTERS, 0,6,0);} // 16
}

[Link]();
// [Link]();
}

#include <SPI.h>
#include <Ethernet.h>
#include "MgsModbus.h"
MgsModbus Mb;
int inByte = 0; // incoming serial byte
// Ethern
Serial.println("0 - print the first 12 words of the MbData space");
  Serial.println("1 - FC 1 - read the first 5 coils fro
IPAddress ip(192, 168, 0, 35);
void setup()
{
  // serial setup
  Serial.begin(9600);
  Serial.println("Serial interface star
Serial.println("5 - FC 5 - write coil 0 of the slave with the bit valeu of 
MbData[0.0]");
  Serial.println("6 - FC 6 - wri

You might also like