Skip to content

Commit

Permalink
feat: ModBus Hardware Testing Folder
Browse files Browse the repository at this point in the history
Codes and manual for Modbus hardware testing
  • Loading branch information
Otrebor671 committed Apr 23, 2024
1 parent 27b9b67 commit 37cd394
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
51 changes: 51 additions & 0 deletions FLIPPER_MOD_BUS/Testing/Master/Master.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//#include <SoftwareSerial.h>
#include <ModbusRTUMaster.h>

const uint8_t rxPin = 10;
const uint8_t txPin = 11;
const uint8_t dePin = 2;
const uint8_t ledPin = 13;

ModbusRTUMaster modbus(Serial1, dePin); // serial port, driver enable pin for rs-485 (optional)

bool coils[4] = {0};
bool discreteInputs[4]= {0};
uint16_t holdingRegisters[1]= {0};
uint16_t inputRegisters[1]= {0};

const bool testCoils[4] = {1,0,1,0};
const uint16_t testInputs[4] = {1,1,1,1};
const uint16_t testInputRegs[1] = {100};
const uint16_t testRegs[1] = {500};

void setup() {
modbus.begin(9600); // baud rate, config (optional)
pinMode(13,OUTPUT);
}
void showResult(bool err = false){
digitalWrite(ledPin, 1);
if(err){
while(1){
digitalWrite(ledPin,0);
delay(100);
digitalWrite(ledPin,1);
delay(100);
}
}

}
void loop() {
modbus.readCoils(1,0, coils, 4);
delay(100);
modbus.readHoldingRegisters(1,0, holdingRegisters, 1);
delay(100);
modbus.readDiscreteInputs(1, 0, discreteInputs, 4); // slave id, starting data address, bool array to place discrete input values, number of discrete inputs to read
delay(100);
modbus.readInputRegisters(1, 0, inputRegisters, 1);
for(int i =0; i<4; i++){
if(coils[i] != testCoils[i]) showResult(true);
if(discreteInputs[i] != testInputs[i]) showResult(true);
}
if(holdingRegisters[0]!= testRegs[0] || inputRegisters[0]!= testInputRegs[0]) showResult(true);
showResult();
}
27 changes: 27 additions & 0 deletions FLIPPER_MOD_BUS/Testing/Slave1/Slave1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <ModbusRTUSlave.h>
#define SLAVE 1

const byte dePin = 13;
ModbusRTUSlave modbus(Serial1, dePin); // serial port, driver enable pin for rs-485

bool discreteInputs[4] = {1,1,1,1};
uint16_t inputRegisters[1] = {100};

bool coils[4] = {1,0,1,0};
uint16_t holdingRegisters[1] = {500};

void setup() {

modbus.configureCoils(coils, 4); // bool array of coil values, number of coils
modbus.configureDiscreteInputs(discreteInputs, 4); // bool array of discrete input values, number of discrete inputs
modbus.configureHoldingRegisters(holdingRegisters, 1); // unsigned 16 bit integer array of holding register values, number of holding registers
modbus.configureInputRegisters(inputRegisters, 1); // unsigned 16 bit integer array of input register values, number of input registers

modbus.begin(SLAVE, 9600);
}

void loop() {

modbus.poll();

}
Binary file added FLIPPER_MOD_BUS/Testing/Testing .pdf
Binary file not shown.
Binary file added FLIPPER_MOD_BUS/Testing/Testing.docx
Binary file not shown.
Binary file added FLIPPER_MOD_BUS/Testing/~$esting.docx
Binary file not shown.

0 comments on commit 37cd394

Please sign in to comment.