Changes for the Arduino Mega setups
This commit is contained in:
199
testing/other/modbus-sd-01.ino
Normal file
199
testing/other/modbus-sd-01.ino
Normal file
@@ -0,0 +1,199 @@
|
||||
#include <Wire.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
#include <NeoSWSerial.h>
|
||||
#include <ModbusMaster.h>
|
||||
#include "PM8000_Modbus_Map_ad.h"
|
||||
|
||||
//#include <SD.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include <SdFat.h>
|
||||
#define SD_CS_PIN 10 // Chip Select for SD Card
|
||||
//RS485 pins
|
||||
#define DE_RE_PIN 4
|
||||
#define RX_PIN 8 // SoftwareSerial RX pin
|
||||
#define TX_PIN 7 // SoftwareSerial TX pin
|
||||
#define SLAVE_ID 101
|
||||
#define SERIAL_BAUDRATE 9600
|
||||
|
||||
// Try to select the best SD card configuration.
|
||||
#define SPI_CLOCK SD_SCK_MHZ(50)
|
||||
#if HAS_SDIO_CLASS
|
||||
#define SD_CONFIG SdioConfig(FIFO_SDIO)
|
||||
#elif ENABLE_DEDICATED_SPI
|
||||
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
|
||||
#else // HAS_SDIO_CLASS
|
||||
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
|
||||
#endif // HAS_SDIO_CLASS
|
||||
|
||||
RTC_DS3231 rtc; // Create an RTC object
|
||||
SdFat32 sd;
|
||||
//SdExFat sd;
|
||||
File dataFile;
|
||||
NeoSWSerial modbusSerial(RX_PIN, TX_PIN); // Create a software serial instance
|
||||
ModbusMaster node;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(SERIAL_BAUDRATE); // For debugging
|
||||
Serial.println(F("Startup \n"));
|
||||
|
||||
// Initialize RTC
|
||||
if (!rtc.begin())
|
||||
{
|
||||
Serial.println(F("Couldn't find RTC\n"));
|
||||
}
|
||||
|
||||
if (rtc.lostPower())
|
||||
{
|
||||
Serial.println(F("RTC lost power, let's set the time!\n"));
|
||||
// Comment out the following line once the time is set to avoid resetting on every start
|
||||
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
}
|
||||
|
||||
Serial.print(F("Time: "));
|
||||
Serial.print(rtc.now().timestamp());
|
||||
Serial.println(F("\n"));
|
||||
|
||||
// Initialize SD card
|
||||
Serial.println(F("SD card initializing..."));
|
||||
pinMode(SD_CS_PIN, OUTPUT);
|
||||
|
||||
// if (!SD.begin(SPI_HALF_SPEED, SD_CS_PIN ))
|
||||
// {
|
||||
// Serial.println(F("SD card initialization failed!\n"));
|
||||
// return;
|
||||
// }
|
||||
// Initialize the SD.
|
||||
if (!sd.begin(SD_CONFIG)) {
|
||||
sd.initErrorHalt(&Serial);
|
||||
return;
|
||||
}
|
||||
Serial.println(F("SD card initialized.\n"));
|
||||
|
||||
Serial.println(F("Initialize RS485 module / Modbus \n"));
|
||||
|
||||
|
||||
pinMode(DE_RE_PIN, OUTPUT);
|
||||
digitalWrite(DE_RE_PIN, LOW); // Set to LOW for receiving mode initially
|
||||
modbusSerial.begin(SERIAL_BAUDRATE);
|
||||
|
||||
node.begin(SLAVE_ID, modbusSerial);
|
||||
node.preTransmission(preTransmission);
|
||||
node.postTransmission(postTransmission);
|
||||
}
|
||||
|
||||
void preTransmission()
|
||||
{
|
||||
digitalWrite(DE_RE_PIN, HIGH); // Enable RS485 transmit
|
||||
}
|
||||
|
||||
void postTransmission()
|
||||
{
|
||||
digitalWrite(DE_RE_PIN, LOW); // Disable RS485 transmit
|
||||
}
|
||||
|
||||
|
||||
void writeFile(char *str)
|
||||
{
|
||||
if (!dataFile)
|
||||
{
|
||||
Serial.println(F("Error opening file"));
|
||||
return;
|
||||
}
|
||||
DateTime now = rtc.now();
|
||||
// Log the current date and time
|
||||
dataFile.print(now.year(), DEC);
|
||||
dataFile.print('-');
|
||||
dataFile.print(now.month(), DEC);
|
||||
dataFile.print('-');
|
||||
dataFile.print(now.day(), DEC);
|
||||
dataFile.print(" ");
|
||||
dataFile.print(now.hour(), DEC);
|
||||
dataFile.print(':');
|
||||
dataFile.print(now.minute(), DEC);
|
||||
dataFile.print(':');
|
||||
dataFile.print(now.second(), DEC);
|
||||
dataFile.print(F(","));
|
||||
dataFile.print(str);
|
||||
|
||||
dataFile.println();
|
||||
}
|
||||
|
||||
String getFilename()
|
||||
{
|
||||
DateTime now = rtc.now();
|
||||
String mb = F("");
|
||||
mb += now.year();
|
||||
mb += now.month();
|
||||
mb += now.day();
|
||||
mb += F(".csv");
|
||||
|
||||
return mb;
|
||||
}
|
||||
unsigned long lastRefreshTime = 0;
|
||||
void loop()
|
||||
{
|
||||
delay(100);
|
||||
|
||||
|
||||
if(millis() - lastRefreshTime >= 1000)
|
||||
{
|
||||
lastRefreshTime += 1000;
|
||||
|
||||
Serial.print(F("\nTime: "));
|
||||
Serial.print(rtc.now().timestamp());
|
||||
// Serial.print("\nHeep:");
|
||||
// Serial.print(ESP.getFreeHeap());
|
||||
Serial.print("\n");
|
||||
|
||||
// Open file for writing
|
||||
String filename = getFilename();
|
||||
dataFile.open(filename.c_str(), FILE_WRITE);
|
||||
Serial.print(F("Open Card "));
|
||||
Serial.print(filename.c_str());
|
||||
Serial.print("\n");
|
||||
|
||||
String mb = "modbus,";
|
||||
writeFile(mb.c_str());
|
||||
|
||||
if (dataFile)
|
||||
{
|
||||
dataFile.close(); // Close the file
|
||||
Serial.print(F("Data written to SD card: "));
|
||||
Serial.print(filename.c_str());
|
||||
Serial.print(F("\n"));
|
||||
}
|
||||
|
||||
Serial.print(F("\n\n"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
||||
Serial.print(F("\naddress: "));
|
||||
Serial.print(registers[i].regaddr);
|
||||
Serial.print(F("\ntype: "));
|
||||
Serial.print(registers[i].regtype);
|
||||
Serial.print(F("\n "));
|
||||
if (registers[i].regaddr > 0) {
|
||||
uint8_t result = node.readHoldingRegisters(registers[i].regaddr , 2);
|
||||
|
||||
if (result == node.ku8MBSuccess)
|
||||
{
|
||||
Serial.print(F("Modbus Read successful:"));
|
||||
|
||||
} else {
|
||||
Serial.print(F("Modbus Read error: "));
|
||||
Serial.println(result, HEX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// // Check if the read was successful
|
||||
|
||||
delay(100);
|
||||
}
|
||||
99
testing/other/modbus_sender.ino
Normal file
99
testing/other/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
testing/other/send1.ino
Normal file
81
testing/other/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
testing/other/send2.ino
Normal file
81
testing/other/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
testing/other/serial1.ino
Normal file
57
testing/other/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
testing/other/serial2.ino
Normal file
72
testing/other/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 )***********
|
||||
Reference in New Issue
Block a user