generated from ElectronicCats/Template-Project-KiCAD-CI
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ModBus Hardware Testing Folder
Codes and manual for Modbus hardware testing
- Loading branch information
1 parent
27b9b67
commit 37cd394
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.