From 364fab188b51a3b1cbfb95f13a4a5f59b791952b Mon Sep 17 00:00:00 2001 From: "Hein (Warky)" Date: Sun, 1 Sep 2024 16:29:01 +0200 Subject: [PATCH] eg --- firmware/libs/simplemodbusng | 1 + firmware/testing/modbus_sender.ino | 99 ++++++++++++++++++++++++++++++ firmware/testing/send1.ino | 81 ++++++++++++++++++++++++ firmware/testing/send2.ino | 81 ++++++++++++++++++++++++ firmware/testing/serial1.ino | 57 +++++++++++++++++ firmware/testing/serial2.ino | 72 ++++++++++++++++++++++ 6 files changed, 391 insertions(+) create mode 160000 firmware/libs/simplemodbusng create mode 100644 firmware/testing/modbus_sender.ino create mode 100644 firmware/testing/send1.ino create mode 100644 firmware/testing/send2.ino create mode 100644 firmware/testing/serial1.ino create mode 100644 firmware/testing/serial2.ino diff --git a/firmware/libs/simplemodbusng b/firmware/libs/simplemodbusng new file mode 160000 index 0000000..1079f44 --- /dev/null +++ b/firmware/libs/simplemodbusng @@ -0,0 +1 @@ +Subproject commit 1079f442a5591a92baa38ddd98668b3ff162bb13 diff --git a/firmware/testing/modbus_sender.ino b/firmware/testing/modbus_sender.ino new file mode 100644 index 0000000..21a1b1c --- /dev/null +++ b/firmware/testing/modbus_sender.ino @@ -0,0 +1,99 @@ +#include +#include + +// Define the pins for SoftwareSerial communication +#define RX_PIN 7 // RX pin for SoftwareSerial +#define TX_PIN 6 // TX pin for SoftwareSerial +#define TX_ENABLE_PIN 4 // Pin to control RS485 direction +#define DE_ENABLE_PIN 5 + +// Create a SoftwareSerial object +SoftwareSerial modbusSerial(RX_PIN, TX_PIN); + +// Create an instance of the ModbusMaster class +ModbusMaster node; + +// Function to control RS485 transmit enable +void preTransmission() +{ + digitalWrite(TX_ENABLE_PIN, HIGH); // Enable RS485 transmit +} + +void postTransmission() +{ + digitalWrite(TX_ENABLE_PIN, LOW); // Disable RS485 transmit +} + +void setup() +{ + // Initialize the built-in serial port for debugging + Serial.begin(9600); + + // Initialize SoftwareSerial for Modbus communication + modbusSerial.begin(9600); + + pinMode(DE_ENABLE_PIN, OUTPUT); + digitalWrite(DE_ENABLE_PIN, HIGH); + + + // Set the pin mode for the RS485 control pin + pinMode(TX_ENABLE_PIN, OUTPUT); + digitalWrite(TX_ENABLE_PIN, LOW); + + + // Modbus communication setup + node.begin(1, modbusSerial); // Slave ID = 1, use modbusSerial for RS485 communication + + // Set callbacks to handle RS485 flow control + node.preTransmission(preTransmission); + node.postTransmission(postTransmission); +} + +void loop() +{ + static uint16_t count = 0; + uint8_t result; + uint16_t data[6]; + + // Read 6 holding registers starting at address 0x0000 + result = node.readHoldingRegisters(0x0000, 6); + + // Check if the read was successful + if (result == node.ku8MBSuccess) + { + Serial.print("Read successful: "); + for (uint8_t j = 0; j < 6; j++) + { + data[j] = node.getResponseBuffer(j); + Serial.print(data[j], HEX); + Serial.print(" "); + } + Serial.println(); + } + else + { + Serial.print("Read error: "); + Serial.println(result, HEX); + } + + // Write the count value to the holding register at address 0x0001 + result = node.writeSingleRegister(0x0001, count); + + + // Check if the write was successful + if (result == node.ku8MBSuccess) + { + Serial.print("Write successful: "); + Serial.println(count); + } + else + { + Serial.print("Write error: "); + Serial.println(result, HEX); + } + + // Increment the count value + count++; + // Delay before the next read cycle + delay(1000); +} diff --git a/firmware/testing/send1.ino b/firmware/testing/send1.ino new file mode 100644 index 0000000..b16dc86 --- /dev/null +++ b/firmware/testing/send1.ino @@ -0,0 +1,81 @@ +#include +#include + +// Define the pins for SoftwareSerial communication +#define RX_PIN 13 // RX pin for SoftwareSerial +#define TX_PIN 12 // TX pin for SoftwareSerial +#define TX_ENABLE_PIN 4 // Pin to control RS485 direction +#define DE_ENABLE_PIN 5 + +// Create a SoftwareSerial object +SoftwareSerial modbusSerial(RX_PIN, TX_PIN); + +// Create an instance of the ModbusMaster class +ModbusMaster node; + +// Function to control RS485 transmit enable +void preTransmission() +{ + digitalWrite(TX_ENABLE_PIN, HIGH); // Enable RS485 transmit + digitalWrite(DE_ENABLE_PIN, HIGH); +} + +void postTransmission() +{ + digitalWrite(TX_ENABLE_PIN, LOW); // Disable RS485 transmit + digitalWrite(DE_ENABLE_PIN, LOW); +} + +void setup() +{ + // Initialize the built-in serial port for debugging + Serial.begin(9600); + + // Initialize SoftwareSerial for Modbus communication + modbusSerial.begin(9600); + + pinMode(DE_ENABLE_PIN, OUTPUT); + digitalWrite(DE_ENABLE_PIN, HIGH); + + + // Set the pin mode for the RS485 control pin + pinMode(TX_ENABLE_PIN, OUTPUT); + digitalWrite(TX_ENABLE_PIN, LOW); + + + // Modbus communication setup + node.begin(1, modbusSerial); // Slave ID = 1, use modbusSerial for RS485 communication + + // Set callbacks to handle RS485 flow control + node.preTransmission(preTransmission); + node.postTransmission(postTransmission); +} + +void loop() +{ + static uint16_t count = 0; + uint8_t result; + uint16_t data[6]; + Serial.print("Loop:"); + Serial.println(count); + // Write the count value to the holding register at address 0x0001 + result = node.writeSingleRegister(0x0001, count); + + + // Check if the write was successful + if (result == node.ku8MBSuccess) + { + Serial.print("Write successful: "); + Serial.println(count); + } + else + { + Serial.print("Write error: "); + Serial.println(result, HEX); + } + + // Increment the count value + count++; + // Delay before the next read cycle + delay(1000); +} diff --git a/firmware/testing/send2.ino b/firmware/testing/send2.ino new file mode 100644 index 0000000..59f8544 --- /dev/null +++ b/firmware/testing/send2.ino @@ -0,0 +1,81 @@ +#include +#include + +// Define the pins for SoftwareSerial communication +#define RX_PIN 3 // RX pin for SoftwareSerial +#define TX_PIN 4 // TX pin for SoftwareSerial +#define TX_ENABLE_PIN 2 // Pin to control RS485 direction +#define DE_ENABLE_PIN 5 + +// Create a SoftwareSerial object +SoftwareSerial modbusSerial(RX_PIN, TX_PIN); + +// Create an instance of the ModbusMaster class +ModbusMaster node; + +// Function to control RS485 transmit enable +void preTransmission() +{ + digitalWrite(TX_ENABLE_PIN, HIGH); // Enable RS485 transmit + digitalWrite(DE_ENABLE_PIN, HIGH); +} + +void postTransmission() +{ + digitalWrite(TX_ENABLE_PIN, LOW); // Disable RS485 transmit + digitalWrite(DE_ENABLE_PIN, LOW); +} + +void setup() +{ + // Initialize the built-in serial port for debugging + Serial.begin(9600); + + // Initialize SoftwareSerial for Modbus communication + modbusSerial.begin(9600); + + pinMode(DE_ENABLE_PIN, OUTPUT); + digitalWrite(DE_ENABLE_PIN, HIGH); + + + // Set the pin mode for the RS485 control pin + pinMode(TX_ENABLE_PIN, OUTPUT); + digitalWrite(TX_ENABLE_PIN, LOW); + + + // Modbus communication setup + node.begin(1, modbusSerial); // Slave ID = 1, use modbusSerial for RS485 communication + + // Set callbacks to handle RS485 flow control + node.preTransmission(preTransmission); + node.postTransmission(postTransmission); +} + +void loop() +{ + static uint16_t count = 0; + uint8_t result; + uint16_t data[6]; + Serial.print("Loop:"); + Serial.println(count); + // Write the count value to the holding register at address 0x0001 + result = node.writeSingleRegister(0x0001, count); + + + // Check if the write was successful + if (result == node.ku8MBSuccess) + { + Serial.print("Write successful: "); + Serial.println(count); + } + else + { + Serial.print("Write error: "); + Serial.println(result, HEX); + } + + // Increment the count value + count++; + // Delay before the next read cycle + delay(1000); +} diff --git a/firmware/testing/serial1.ino b/firmware/testing/serial1.ino new file mode 100644 index 0000000..bb84a13 --- /dev/null +++ b/firmware/testing/serial1.ino @@ -0,0 +1,57 @@ + +/*-----( Import needed libraries )-----*/ +#include +/*-----( Declare Constants and Pin Numbers )-----*/ +#define SSerialRX 13 //Serial Receive pin +#define SSerialTX 12 //Serial Transmit pin + +#define SSerialTxControl 4 //RS485 Direction control +#define RS485Transmit HIGH +#define RS485Receive LOW + +#define Pin13LED 9 + +/*-----( Declare objects )-----*/ +SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX + +/*-----( Declare Variables )-----*/ +int byteReceived; +int byteSend; + +void setup() /****** SETUP: RUNS ONCE ******/ +{ + // Start the built-in serial port, probably to Serial Monitor + Serial.begin(9600); + Serial.println("Remote connector"); // Can be ignored + + pinMode(Pin13LED, OUTPUT); + pinMode(SSerialTxControl, OUTPUT); + + digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver + + // Start the software serial port, to another device + RS485Serial.begin(9600); // set the data rate +}//--(end setup )--- + + +void loop() /****** LOOP: RUNS CONSTANTLY ******/ +{ + //Copy input data to output + if (RS485Serial.available()) + { + byteSend = RS485Serial.read(); // Read the byte + + digitalWrite(Pin13LED, HIGH); // Show activity + delay(1); + digitalWrite(Pin13LED, LOW); + + digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit + RS485Serial.write(byteSend); // Send the byte back + //delay(10); + digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit + Serial.println("Bounce"); + Serial.println(byteSend); +// delay(100); + }// End If RS485SerialAvailable + +}//--(end main loop )--- \ No newline at end of file diff --git a/firmware/testing/serial2.ino b/firmware/testing/serial2.ino new file mode 100644 index 0000000..f374c16 --- /dev/null +++ b/firmware/testing/serial2.ino @@ -0,0 +1,72 @@ + +/*-----( Import needed libraries )-----*/ +#include +/*-----( Declare Constants and Pin Numbers )-----*/ +#define SSerialRX 13 //Serial Receive pin +#define SSerialTX 12 //Serial Transmit pin + +#define SSerialTxControl 4 //RS485 Direction control + +#define RS485Transmit HIGH +#define RS485Receive LOW + +#define Pin13LED 9 + +/*-----( Declare objects )-----*/ +SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX + +/*-----( Declare Variables )-----*/ +int byteReceived; +int byteSend; + +void setup() /****** SETUP: RUNS ONCE ******/ +{ + // Start the built-in serial port, probably to Serial Monitor + Serial.begin(9600); + Serial.println("Master connector"); + + + pinMode(Pin13LED, OUTPUT); + pinMode(SSerialTxControl, OUTPUT); + + digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver + + // Start the software serial port, to another device + RS485Serial.begin(9600); // set the data rate + +}//--(end setup )--- + + +void loop() /****** LOOP: RUNS CONSTANTLY ******/ +{ + digitalWrite(Pin13LED, HIGH); // Show activity + if (Serial.available()) + { + byteReceived = Serial.read(); + + digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit + RS485Serial.write(byteReceived); // Send byte to Remote Arduino + + digitalWrite(Pin13LED, LOW); // Show activity + //Serial.println("\nRead Local"); + delay(10); + digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit + } + + if (RS485Serial.available()) //Look for data from other Arduino + { + digitalWrite(Pin13LED, HIGH); // Show activity + byteReceived = RS485Serial.read(); // Read received byte + Serial.println("Received "); + Serial.write(byteReceived); // Show on Serial Monitor + + delay(10); + digitalWrite(Pin13LED, LOW); // Show activity + } + +}//--(end main loop )--- + +/*-----( Declare User-written Functions )-----*/ + +//NONE +//*********( THE END )*********** \ No newline at end of file