eg
This commit is contained in:
parent
a8fe198247
commit
364fab188b
1
firmware/libs/simplemodbusng
Submodule
1
firmware/libs/simplemodbusng
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 1079f442a5591a92baa38ddd98668b3ff162bb13
|
99
firmware/testing/modbus_sender.ino
Normal file
99
firmware/testing/modbus_sender.ino
Normal file
@ -0,0 +1,99 @@
|
||||
#include <ModbusMaster.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// 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);
|
||||
}
|
81
firmware/testing/send1.ino
Normal file
81
firmware/testing/send1.ino
Normal file
@ -0,0 +1,81 @@
|
||||
#include <ModbusMaster.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// 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);
|
||||
}
|
81
firmware/testing/send2.ino
Normal file
81
firmware/testing/send2.ino
Normal file
@ -0,0 +1,81 @@
|
||||
#include <ModbusMaster.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// 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);
|
||||
}
|
57
firmware/testing/serial1.ino
Normal file
57
firmware/testing/serial1.ino
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
/*-----( Import needed libraries )-----*/
|
||||
#include <SoftwareSerial.h>
|
||||
/*-----( 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 )---
|
72
firmware/testing/serial2.ino
Normal file
72
firmware/testing/serial2.ino
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
/*-----( Import needed libraries )-----*/
|
||||
#include <SoftwareSerial.h>
|
||||
/*-----( 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 )***********
|
Loading…
Reference in New Issue
Block a user