chore: rewrite to modbus rtu

This commit is contained in:
2024-11-09 00:36:06 +06:00
parent 0a0084f98f
commit 68f47c9d53
14 changed files with 506 additions and 167 deletions

View File

@@ -7,36 +7,42 @@ KiCad hardware design files are in the `hw` directory.
![photo](img/ch32_extension_photo.jpg)
![render](img/ch32_extension.png)
## Communication Protocol
## comm protocol
### Message Format
uses Modbus RTU protocol over RS485
The RS485 communication uses a simple 4-byte message format:
### Config
```
Byte 0: Board Address (0x01-0xFE, 0xFF for broadcast)
Byte 1: Command (0x01 for SET_OUTPUTS)
Byte 2: Data High Byte
Byte 3: Data Low Byte
Baud Rate: 9600
Data Bits: 8
Stop Bits: 1
Parity: None
```
### Protocol Constants
### Modbus
- Function Code: 0x06 (Write Single Register)
- Default Slave Address: 0x01
- Register Map:
- Register 0x0000: Output Control Register (16 bits for controlling 16 relays)
### Outputs
Each bit in the control register corresponds to one output:
- Bit 0 controls Output 1
- Bit 1 controls Output 2
- etc.
### Msg
```c
#define BOARD_ADDRESS 0x01 // Default board address
#define CMD_SET_OUTPUTS 0x01 // Command to set outputs
#define BROADCAST_ADDR 0xFF // Broadcast address
```
### Example Message
To control the outputs, send a 4-byte message:
```rust
let message = [
address, // Board address (0xFF for broadcast)
CMD_SET_OUTPUTS, // Command
(value >> 8) as u8, // Data high byte
value as u8, // Data low byte
];
Byte 0: Slave Address (0x01)
Byte 1: Function Code (0x06)
Byte 2: Register Address High (0x00)
Byte 3: Register Address Low (0x00)
Byte 4: Data High Byte
Byte 5: Data Low Byte
Byte 6: CRC Low Byte
Byte 7: CRC High Byte
```