Skip to content

Commit f98fb6e

Browse files
committed
first initial
1 parent 2e8b0c7 commit f98fb6e

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# ModbusRTUSlave Arduino Library
1+
# MBModbusRTUSlave Arduino Library
22

33
A simple and flexible Modbus RTU slave library for Arduino.
44

55
## Overview
66

7-
The `ModbusRTUSlave` library allows Arduino devices to function as Modbus RTU slaves over a serial connection. It supports reading and writing registers via Modbus function codes `0x03` (Read Holding Registers) and `0x06` (Write Single Register), with provisions for LED control based on a designated register value. The library is designed to be customizable, with configurable slave address, LED pin, LED register index, and baud rate.
7+
The `MBModbusRTUSlave` library allows Arduino devices to function as Modbus RTU slaves over a serial connection. It supports reading and writing registers via Modbus function codes `0x03` (Read Holding Registers) and `0x06` (Write Single Register), with provisions for LED control based on a designated register value. The library is designed to be customizable, with configurable slave address, LED pin, LED register index, and baud rate.
88

99
### Features
1010
- Supports Modbus RTU protocol (function codes `0x03` and `0x06`).
@@ -29,19 +29,19 @@ The `ModbusRTUSlave` library allows Arduino devices to function as Modbus RTU sl
2929
1. **Download the Repository**:
3030
- Clone this repository or download it as a ZIP file:
3131
```bash
32-
git clone https://github.com/electrocoder/ModbusRTUSlave.git
32+
git clone https://github.com/electrocoder/MBModbusRTUSlave.git
3333
```
3434

3535
2. **Install to Arduino IDE**:
36-
- Move the `ModbusRTUSlave` folder to your Arduino `libraries` directory:
36+
- Move the `MBModbusRTUSlave` folder to your Arduino `libraries` directory:
3737
- Windows: `Documents/Arduino/libraries/`
3838
- macOS/Linux: `~/Documents/Arduino/libraries/`
3939
- Restart the Arduino IDE.
4040

4141
3. **Include in Your Sketch**:
4242
- Add the library to your sketch with:
4343
```cpp
44-
#include <ModbusRTUSlave.h>
44+
#include <MBModbusRTUSlave.h>
4545
```
4646

4747
---
@@ -52,10 +52,10 @@ The `ModbusRTUSlave` library allows Arduino devices to function as Modbus RTU sl
5252
This example initializes a Modbus slave with default settings and controls an LED based on register 5.
5353

5454
```cpp
55-
#include <ModbusRTUSlave.h>
55+
#include <MBModbusRTUSlave.h>
5656
5757
long modbusBaudRate = 9600; // Customizable baud rate
58-
ModbusRTUSlave modbus(0x01, 13, 5); // Slave address: 0x01, LED pin: 13, LED register: 5
58+
MBModbusRTUSlave modbus(0x01, 13, 5); // Slave address: 0x01, LED pin: 13, LED register: 5
5959
6060
void setup() {
6161
modbus.begin(modbusBaudRate);

examples/ModbusRTUSlaveExample/ModbusRTUSlaveExample.ino renamed to examples/ModbusRTUSlaveExample/MBModbusRTUSlaveExample.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ModbusRTUSlaveExample.ino - Example Sketch for ModbusRTUSlave Library
2+
* MBModbusRTUSlaveExample.ino - Example Sketch for MBModbusRTUSlave Library
33
*
44
* Description: This sketch demonstrates the usage of the ModbusSlave library to configure
55
* an Arduino as a Modbus RTU slave. It controls an LED based on a specific
@@ -15,7 +15,7 @@
1515
* - LED connected to pin 13 (onboard LED can be used)
1616
*
1717
* Dependencies:
18-
* - ModbusRTUSlave library (version 1.0.0 or higher)
18+
* - MBModbusRTUSlave library (version 1.0.0 or higher)
1919
*
2020
* Usage:
2121
* - Upload this sketch to your Arduino.
@@ -24,9 +24,9 @@
2424
* - Write 0 to register 5: "01 06 00 05 00 00 [CRC]" (turns LED OFF)
2525
*/
2626

27-
#include "ModbusRTUSlave.h"
27+
#include "MBModbusRTUSlave.h"
2828

29-
ModbusRTUSlave modbus(0x01, 13, 0, 10); // Slave address: 0x01, LED pin: 13, LED register: 0, Register count: 10
29+
MBModbusRTUSlave modbus(0x01, 13, 0, 10); // Slave address: 0x01, LED pin: 13, LED register: 0, Register count: 10
3030
long modbusBaudRate = 9600; // If you want it can be another value like 115200, 19200
3131

3232
void setup() {

examples/ModbusRTUSlaveExample/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ModbusRTUSlave Arduino Library
1+
# MBModbusRTUSlave Arduino Library
22

33
A simple and flexible Modbus RTU slave library for Arduino.
44

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ModbusSlave KEYWORD1
1+
MBModbusRTUSlave KEYWORD1
22
begin KEYWORD2
33
update KEYWORD2

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name=ModbusRTUSlave
1+
name=MBModbusRTUSlave
22
version=1.0.0
33
author=S.Mersin (electrocoder) <[email protected]>
44
maintainer=S.Mersin (electrocoder) <[email protected]>
55
sentence=Modbus RTU protocol (function codes `0x03` and `0x06`). This library is a slave implementation of the Modbus RTU protocol.
6-
paragraph=The `ModbusRTUSlave` library allows Arduino devices to function as Modbus RTU slaves over a serial connection. It supports reading and writing registers via Modbus function codes `0x03` (Read Holding Registers) and `0x06` (Write Single Register), with provisions for LED control based on a designated register value. The library is designed to be customizable, with configurable slave address, LED pin, LED register index, and baud rate.
6+
paragraph=The `MBModbusRTUSlave` library allows Arduino devices to function as Modbus RTU slaves over a serial connection. It supports reading and writing registers via Modbus function codes `0x03` (Read Holding Registers) and `0x06` (Write Single Register), with provisions for LED control based on a designated register value. The library is designed to be customizable, with configurable slave address, LED pin, LED register index, and baud rate.
77
category=Communication
8-
url=https://github.com/electrocoder/ModbusRTUSlave
8+
url=https://github.com/electrocoder/MBModbusRTUSlave
99
architectures=avr,esp8266,esp32
1010
license=MIT

src/ModbusRTUSlave.cpp renamed to src/MBModbusRTUSlave.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* ModbusRTUSlave.cpp - Implementation of the Modbus RTU Slave Library for Arduino
2+
* MBModbusRTUSlave.cpp - Implementation of the Modbus RTU Slave Library for Arduino
33
*
4-
* Description: This file implements the methods of the ModbusRTUSlave class.
4+
* Description: This file implements the methods of the MBModbusRTUSlave class.
55
* It supports Modbus RTU protocol for register reading (0x03) and writing (0x06).
66
* Provides LED control and flexible baud rate adjustments.
77
* Author: S.Mersin (electrocoder) <[email protected]> (Assisted by Grok)
@@ -13,9 +13,9 @@
1313
* All registers except the LED register are updated with random values.
1414
*/
1515

16-
#include "ModbusRTUSlave.h"
16+
#include "MBModbusRTUSlave.h"
1717

18-
ModbusRTUSlave::ModbusRTUSlave(uint8_t slaveAddress, uint8_t ledPin, uint8_t ledRegisterIndex, uint16_t registerCount) {
18+
MBModbusRTUSlave::MBModbusRTUSlave(uint8_t slaveAddress, uint8_t ledPin, uint8_t ledRegisterIndex, uint16_t registerCount) {
1919
_slaveAddress = slaveAddress;
2020
_ledPin = ledPin;
2121
_ledRegisterIndex = ledRegisterIndex;
@@ -34,7 +34,7 @@ ModbusRTUSlave::ModbusRTUSlave(uint8_t slaveAddress, uint8_t ledPin, uint8_t led
3434
}
3535
}
3636

37-
void ModbusRTUSlave::begin(long baudRate) {
37+
void MBModbusRTUSlave::begin(long baudRate) {
3838
_baudRate = baudRate;
3939
pinMode(_ledPin, OUTPUT);
4040
digitalWrite(_ledPin, LOW);
@@ -43,7 +43,7 @@ void ModbusRTUSlave::begin(long baudRate) {
4343
;
4444
}
4545

46-
void ModbusRTUSlave::update() {
46+
void MBModbusRTUSlave::update() {
4747
// We assign random values to all registers except the LED register
4848
for (int i = 0; i < _registerCount; i++) {
4949
if (i != _ledRegisterIndex)
@@ -71,7 +71,7 @@ void ModbusRTUSlave::update() {
7171
digitalWrite(_ledPin, modbusRegisters[_ledRegisterIndex] == 1 ? HIGH : LOW);
7272
}
7373

74-
void ModbusRTUSlave::processReadHoldingRegisters() {
74+
void MBModbusRTUSlave::processReadHoldingRegisters() {
7575
uint16_t startAddress = (requestBuffer[2] << 8) | requestBuffer[3];
7676
uint16_t registerCount = (requestBuffer[4] << 8) | requestBuffer[5];
7777

@@ -92,7 +92,7 @@ void ModbusRTUSlave::processReadHoldingRegisters() {
9292
Serial.write(responseBuffer, 5 + registerCount * 2);
9393
}
9494

95-
void ModbusRTUSlave::processWriteSingleRegister() {
95+
void MBModbusRTUSlave::processWriteSingleRegister() {
9696
uint16_t registerAddress = (requestBuffer[2] << 8) | requestBuffer[3];
9797
uint16_t registerValue = (requestBuffer[4] << 8) | requestBuffer[5];
9898

@@ -109,7 +109,7 @@ void ModbusRTUSlave::processWriteSingleRegister() {
109109
Serial.write(responseBuffer, 8);
110110
}
111111

112-
uint16_t ModbusRTUSlave::calculateCRC(uint8_t *buffer, uint8_t length) {
112+
uint16_t MBModbusRTUSlave::calculateCRC(uint8_t *buffer, uint8_t length) {
113113
uint16_t crc = 0xFFFF;
114114
for (uint8_t pos = 0; pos < length; pos++) {
115115
crc ^= (uint16_t)buffer[pos];
@@ -125,29 +125,29 @@ uint16_t ModbusRTUSlave::calculateCRC(uint8_t *buffer, uint8_t length) {
125125
return crc;
126126
}
127127

128-
bool ModbusRTUSlave::checkCRC(uint8_t *buffer, uint8_t length) {
128+
bool MBModbusRTUSlave::checkCRC(uint8_t *buffer, uint8_t length) {
129129
uint16_t receivedCRC = (buffer[length + 1] << 8) | buffer[length];
130130
uint16_t calculatedCRC = calculateCRC(buffer, length);
131131
return (receivedCRC == calculatedCRC);
132132
}
133133

134134
// Setter methods
135-
void ModbusRTUSlave::setSlaveAddress(uint8_t slaveAddress) {
135+
void MBModbusRTUSlave::setSlaveAddress(uint8_t slaveAddress) {
136136
_slaveAddress = slaveAddress;
137137
}
138138

139-
void ModbusRTUSlave::setLedPin(uint8_t ledPin) {
139+
void MBModbusRTUSlave::setLedPin(uint8_t ledPin) {
140140
_ledPin = ledPin;
141141
pinMode(_ledPin, OUTPUT);
142142
}
143143

144-
void ModbusRTUSlave::setLedRegisterIndex(uint8_t ledRegisterIndex) {
144+
void MBModbusRTUSlave::setLedRegisterIndex(uint8_t ledRegisterIndex) {
145145
if (ledRegisterIndex < _registerCount) {
146146
_ledRegisterIndex = ledRegisterIndex;
147147
}
148148
}
149149

150-
void ModbusRTUSlave::setBaudRate(long baudRate) {
150+
void MBModbusRTUSlave::setBaudRate(long baudRate) {
151151
_baudRate = baudRate;
152152
Serial.begin(_baudRate, SERIAL_8N1);
153153
}

src/ModbusRTUSlave.h renamed to src/MBModbusRTUSlave.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ModbusRTUSlave.h - Modbus RTU Slave Library for Arduino
2+
* MBModbusRTUSlave.h - Modbus RTU Slave Library for Arduino
33
*
44
* Description: This library configures Arduino devices as Modbus RTU slaves.
55
* It supports LED control, register reading/writing, and flexible baud rate settings.
@@ -8,18 +8,18 @@
88
* Version: 1.0.0
99
* License: MIT License
1010
*
11-
* Usage: The ModbusRTUSlave class is initialized with a slave address, LED pin, and LED register index.
11+
* Usage: The MBModbusRTUSlave class is initialized with a slave address, LED pin, and LED register index.
1212
* Baud rate can be set via the begin() method using a variable.
1313
*/
1414

15-
#ifndef ModbusRTUSlave_h
16-
#define ModbusRTUSlave_h
15+
#ifndef MBModbusRTUSlave_h
16+
#define MBModbusRTUSlave_h
1717

1818
#include <Arduino.h>
1919

20-
class ModbusRTUSlave {
20+
class MBModbusRTUSlave {
2121
public:
22-
ModbusRTUSlave(uint8_t slaveAddress = 0x01, uint8_t ledPin = 13, uint8_t ledRegisterIndex = 0, uint16_t registerCount = 10); // Constructor
22+
MBModbusRTUSlave(uint8_t slaveAddress = 0x01, uint8_t ledPin = 13, uint8_t ledRegisterIndex = 0, uint16_t registerCount = 10); // Constructor
2323
void begin(long baudRate = 9600); // Setup function
2424
void update(); // Loop function
2525

0 commit comments

Comments
 (0)