This commit is contained in:
claw committed 2026-03-31 13:27:23 +08:00
commit 53c85e94af
393 files changed
+141844

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
#include "modbusRtuMaster.h"
#include "w5500/modbusSlave.h"
#include "w5500/modbus_crc.h"
#include "usart.h"
#include "gpio.h"
#include "rtuMaster.h"
typedef union {
uint16_t val16;
uint8_t bytes[2];
} Uint16To8;
Uint16To8 cvt;
static uint8_t idx = 0;
static uint16_t slave_id, slave_address, size;
void RTU_Master_Loop(RtuMaster *master, rtumST myRtumST[20]) {
for (int j = 0; j < 20; j++) {
slave_id = get_register(myRtumST[j].slave_id);
slave_address = get_register(myRtumST[j].slave_address);
size = get_register(myRtumST[j].size);
if (slave_id == master->localSlaveId) {
set_register(120, j);
set_register(121, slave_id);
set_register(122, slave_address);
set_register(123, size);
if (j % 2 == 0) {
for (int i = 0; i < size; i++) {
set_register(j * 20 + master->localStartIdx + i, get_register(slave_address + i));
}
} else {
for (int i = 0; i < size; i++) {
set_register(slave_address + i, get_register(j * 20 + master->localStartIdx + i));
}
}
}
}
if (master->timeout >= 1000) {
slave_id = get_register(myRtumST[idx].slave_id);
slave_address = get_register(myRtumST[idx].slave_address);
size = get_register(myRtumST[idx].size);
if (slave_id == 0 || slave_id >= 127 || size > 20 || size == 0 || slave_id != master->localSlaveId) {
master->txsize = 0;
} else {
if (idx % 2 == 0) {
master->txbuf[0] = slave_id;
master->txbuf[1] = 0x03;
cvt.val16 = slave_address;
master->txbuf[2] = cvt.bytes[1];
master->txbuf[3] = cvt.bytes[0];
cvt.val16 = size;
master->txbuf[4] = cvt.bytes[1];
master->txbuf[5] = cvt.bytes[0];
cvt.val16 = crc16(master->txbuf, 6);
master->txbuf[6] = cvt.bytes[0];
master->txbuf[7] = cvt.bytes[1];
master->txsize = 8;
} else {
master->txbuf[0] = slave_id;
master->txbuf[1] = 0x10;
cvt.val16 = slave_address;
master->txbuf[2] = cvt.bytes[1];
master->txbuf[3] = cvt.bytes[0];
cvt.val16 = size;
master->txbuf[4] = cvt.bytes[1];
master->txbuf[5] = cvt.bytes[0];
master->txbuf[6] = size * 2;
for (uint8_t i = 0; i < size; i++) {
cvt.val16 = get_register(idx * 20 + master->localStartIdx + i);
master->txbuf[2 * i + 7] = cvt.bytes[1];
master->txbuf[2 * i + 8] = cvt.bytes[0];
}
cvt.val16 = crc16(master->txbuf, size * 2 + 7);
master->txbuf[size * 2 + 7] = cvt.bytes[0];
master->txbuf[size * 2 + 8] = cvt.bytes[1];
master->txsize = 2 * size + 9;
}
}
if (master->txsize > 0) {
HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET);
HAL_UART_Transmit_DMA(&huart1, master->txbuf, master->txsize);
master->timeout = 1;
} else {
master->timeout = 1000;
}
idx = (idx + 1) % 20;
}
if (master->rxdone) {
cvt.bytes[0] = master->rxbuf[master->rxsize - 2];
cvt.bytes[1] = master->rxbuf[master->rxsize - 1];
if (crc16(master->rxbuf, master->rxsize - 2) != cvt.val16) {
return;
}
if (master->rxbuf[1] == 0x03) {
uint16_t size = master->rxbuf[2] / 2;
size = size >= 20 ? 20 : size;
for (int i = 0; i < size; i++) {
cvt.bytes[0] = master->rxbuf[2 * i + 4];
cvt.bytes[1] = master->rxbuf[2 * i + 3];
if (idx == 0) {
set_register(master->localStartIdx + i + (20 * 19), cvt.val16);
} else {
set_register(master->localStartIdx + i + (20 * (idx - 1)), cvt.val16);
}
}
}
master->rxdone = 0;
master->timeout = 950;
}
}