Skip to content

Commit 742489a

Browse files
committed
Uploaded test suites for minion modbus configuration
1 parent c9ccaed commit 742489a

File tree

7 files changed

+159
-38
lines changed

7 files changed

+159
-38
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <SoftwareSerial.h>
2+
#include "ModbusRTUSlave.h"
3+
4+
#define RX_PIN 4
5+
#define TX_PIN 0
6+
#define DE_PIN 5
7+
#define SLAVE_ID 1
8+
#define BUFFER_SIZE 256
9+
10+
SoftwareSerial swSerial(RX_PIN, TX_PIN);
11+
uint8_t buf[BUFFER_SIZE];
12+
ModbusRTUSlave slave(swSerial, buf, sizeof(buf), DE_PIN);
13+
14+
uint16_t holdingRegister[1];
15+
16+
// Callbacks
17+
int holdingRegisterRead(uint16_t index) {
18+
return holdingRegister[index];
19+
}
20+
21+
bool holdingRegisterWrite(uint16_t index, uint16_t value) {
22+
holdingRegister[index] = value;
23+
return true;
24+
}
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
// Configure the Modbus Slave
30+
slave.configureHoldingRegisters(1, holdingRegisterRead, holdingRegisterWrite);
31+
slave.begin(SLAVE_ID, 9600, SERIAL_8N1); // 9600 baud rate, 8 data bits, No parity, 1 stop bit
32+
}
33+
34+
void loop() {
35+
slave.poll();
36+
37+
if(holdingRegister[0] != 0) {
38+
Serial.print("Value in Holding Register: ");
39+
Serial.println(holdingRegister[0]);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <SoftwareSerial.h>
2+
#include "ModbusRTUSlave.h"
3+
4+
#define RX_PIN 8
5+
#define TX_PIN 2
6+
#define DE_PIN 1
7+
#define SLAVE_ID 1
8+
#define BUFFER_SIZE 256
9+
10+
SoftwareSerial swSerial(RX_PIN, TX_PIN);
11+
uint8_t buf[BUFFER_SIZE];
12+
ModbusRTUSlave slave(swSerial, buf, sizeof(buf), DE_PIN);
13+
14+
uint16_t holdingRegister[1];
15+
16+
// Callbacks
17+
int holdingRegisterRead(uint16_t index) {
18+
return holdingRegister[index];
19+
}
20+
21+
bool holdingRegisterWrite(uint16_t index, uint16_t value) {
22+
holdingRegister[index] = value;
23+
return true;
24+
}
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
// Configure the Modbus Slave
30+
slave.configureHoldingRegisters(1, holdingRegisterRead, holdingRegisterWrite);
31+
slave.begin(SLAVE_ID, 9600, SERIAL_8N1); // 9600 baud rate, 8 data bits, No parity, 1 stop bit
32+
}
33+
34+
void loop() {
35+
slave.poll();
36+
37+
if(holdingRegister[0] != 0) {
38+
Serial.print("Value in Holding Register: ");
39+
Serial.println(holdingRegister[0]);
40+
}
41+
}

minions/NanoTestSuite/nano-test-master/nano-test-master.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <Wire.h>
22
#include <Adafruit_BMP3XX.h>
3-
//#include <ArduinoModbus.h>
3+
#include <ArduinoModbus.h>
44

55
#define BMP_SDA A4 // BMP390 sensor SDA pin
66
#define BMP_SCL A5 // BMP390 sensor SCL pin
@@ -19,9 +19,9 @@ void setup() {
1919

2020
pinMode(PIR_PIN, INPUT);
2121

22-
//if (!ModbusRTUClient.begin(9600)) {
23-
//Serial.println("Failed to start Modbus RTU Client!");
24-
//}
22+
if (!ModbusRTUClient.begin(9600)) {
23+
Serial.println("Failed to start Modbus RTU Client!");
24+
}
2525
}
2626

2727
void loop() {
@@ -49,12 +49,12 @@ void loop() {
4949
Serial.print("Soil Moisture = ");
5050
Serial.println(moistureValue);
5151

52-
// Modbus communication
53-
//uint8_t serverId = 1;
54-
//uint16_t address = 0;
55-
//uint16_t value = 1234; // example data
52+
//Modbus communication
53+
uint8_t serverId = 1;
54+
uint16_t address = 0;
55+
uint16_t value = 1234; // example data
5656

57-
//ModbusRTUClient.holdingRegisterWrite(serverId, address, value);
57+
ModbusRTUClient.holdingRegisterWrite(serverId, address, value);
5858

5959
delay(1000);
6060
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <ArduinoModbus.h>
2+
3+
#define DE_PIN 7 // Driver Enable pin for RS485
4+
5+
void setup() {
6+
Serial.begin(9600);
7+
pinMode(DE_PIN, OUTPUT); // Configure DE pin as output
8+
9+
if (!ModbusRTUClient.begin(9600)) {
10+
Serial.println("Failed to start Modbus RTU Client!");
11+
}
12+
}
13+
14+
void loop() {
15+
//Modbus communication
16+
uint8_t serverId = 1;
17+
uint16_t address = 0;
18+
uint16_t value = 1234; // example data
19+
20+
digitalWrite(DE_PIN, HIGH); // Set RS485 to send mode
21+
if (ModbusRTUClient.holdingRegisterWrite(serverId, address, value) < 0) {
22+
Serial.println("Modbus Write Failed");
23+
}
24+
25+
digitalWrite(DE_PIN, LOW); // Set RS485 to receive mode
26+
long readValue = ModbusRTUClient.holdingRegisterRead(serverId, address);
27+
if (readValue < 0) {
28+
Serial.println("Modbus Read Failed");
29+
} else {
30+
Serial.print("Read Value: ");
31+
Serial.println(readValue);
32+
}
33+
34+
delay(1000);
35+
}

setup_scripts/i2c_rtc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
echo rtc-pcf85363 | sudo tee -a /etc/modules
55

66
#bind rtc to i2c bus
7-
echo rtc-pcf85363 0xA2 | sudo tee /sys/class/i2c-adapter/i2c-2/new_device
7+
echo rtc-pcf85363 0x51 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device

utils/PCF85363A_i2c.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
import smbus2
3+
4+
# Create a new SMBus instance on line 2
5+
bus = smbus2.SMBus(1)
6+
7+
def set_rtcm():
8+
# Device i2c write address
9+
device_address = 0x51
10+
11+
# Register address
12+
register_address = 0x28
13+
14+
# First, we need to read the current value of the register
15+
current_value = bus.read_byte_data(device_address, register_address)
16+
17+
# Then, we need to clear the fourth bit (fifth from the left, little endian starting at 0)
18+
# We can do this by creating a mask where all bits are 1, except for the fourth bit, which is 0.
19+
# Then, we use the bitwise AND operation to clear the fourth bit.
20+
mask = 0b11101111
21+
new_value = current_value & mask
22+
23+
# Write the new value back to the register
24+
bus.write_byte_data(device_address, register_address, new_value)
25+
26+
if __name__ == "__main__":
27+
28+
if sys.argv[1] == "set_rtcm":
29+
print("Setting RTCM on the RTC")
30+
set_rtcm()
31+
32+

utils/i2c_helper.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)