first push

This commit is contained in:
claw committed 2026-04-02 17:52:12 +08:00
1 parent 53c85e94af
commit 32f5ef9d3b
398 files changed
+1675 -140886

No files matched your search

+896
View File
@@ -0,0 +1,896 @@
#include <stdio.h>
#include "loopback.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "modbusSlave.h"
#include "modbus_crc.h"
#include "usart.h"
#include <string.h>
#include "slmpMaster.h"
#include "rtuMaster.h"
#include "gpio.h"
#define DBID1_IDX 1000
#define START1_IDX 1001
#define SIZE1_IDX 1002
#define TOMOD_IDX 1003
#define DBID2_IDX 1005
#define START2_IDX 1006
#define SIZE2_IDX 1007
#define FROMMOD_IDX 1008
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
uint8_t loopback_tcps_modbus_rxbuf[1024];
uint8_t loopback_tcps_modbus_txbuf[1024];
uint16_t comm_count = 0;
int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port) {
int32_t ret;
uint16_t size = 0, sentsize = 0;
#ifdef _LOOPBACK_DEBUG_
uint8_t destip[4];
uint16_t destport;
#endif
switch (getSn_SR(sn)) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) {
#ifdef _LOOPBACK_DEBUG_
getSn_DIPR(sn, destip);
destport = getSn_DPORT(sn);
printf("%d:Connected - %d.%d.%d.%d : %d\r\n", sn, destip[0],
destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON);
}
if ((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
{
if (size > DATA_BUF_SIZE)
size = DATA_BUF_SIZE;
ret = recv(sn, loopback_tcps_modbus_rxbuf, size);
if (ret <= 0)
return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
uint16_t rxbuf_size = handleModbusTcp(loopback_tcps_modbus_rxbuf, loopback_tcps_modbus_txbuf);
if (rxbuf_size > 0) {
sentsize = 0;
while (rxbuf_size != sentsize) {
ret = send(sn, loopback_tcps_modbus_txbuf + sentsize, rxbuf_size - sentsize);
if (ret < 0) {
close(sn);
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
}
break;
case SOCK_CLOSE_WAIT:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:CloseWait\r\n",sn);
#endif
if ((ret = disconnect(sn)) != SOCK_OK)
return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT:
#ifdef _LOOPBACK_DEBUG_
printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
#endif
if ((ret = listen(sn)) != SOCK_OK)
return ret;
break;
case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:TCP server loopback start\r\n",sn);
#endif
if ((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn)
return ret;
#ifdef _LOOPBACK_DEBUG_
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
// 将Modbus TCP转RTU
uint16_t tcp_to_rtu(uint8_t *tcp_buf, uint8_t *rtu_buf) {
uint16_t len = ((tcp_buf[4] << 8) | tcp_buf[5]) - 1; // 去掉UnitID前的长度
memcpy(rtu_buf, &tcp_buf[6], len + 1); // 从UnitID开始拷贝
uint16_t crc = crc16(rtu_buf, len + 1);
rtu_buf[len + 1] = crc & 0xFF;
rtu_buf[len + 2] = (crc >> 8) & 0xFF;
return len + 3; // 返回总长度
}
// 将Modbus RTU转TCP
uint16_t rtu_to_tcp(uint8_t *rtu_buf, uint8_t *tcp_buf, uint16_t rtu_len, uint8_t *rx_buf) {
uint16_t crc_calc = crc16(rtu_buf, rtu_len - 2);
uint16_t crc_recv = rtu_buf[rtu_len - 2] | (rtu_buf[rtu_len - 1] << 8);
if (crc_calc != crc_recv) {
return 0; // CRC错误
}
// 还原MBAP头
memcpy(tcp_buf, rx_buf, 6); // Transaction ID, Protocol ID, Length先拷贝
tcp_buf[6] = rtu_buf[0]; // UnitID
memcpy(&tcp_buf[7], &rtu_buf[1], rtu_len - 3); // 功能码+数据
uint16_t pdu_len = rtu_len - 3; // PDU长度
uint16_t mbap_len = pdu_len + 1; // **必须 +1 包含 UnitID**
tcp_buf[4] = (mbap_len >> 8) & 0xFF;
tcp_buf[5] = (mbap_len) & 0xFF;
return 7 + pdu_len; // MBAP(7) + PDU
}
enum {
STATE_IDLE, STATE_WAIT_RTU
} modbus_state = STATE_IDLE;
int32_t loopback_tcps_modbus_to485(uint8_t sn, uint8_t *buf, uint16_t port, uint16_t *timeout, RtuRxST *rtu_rx) {
int32_t ret;
uint16_t size = 0, sentsize = 0;
#ifdef _LOOPBACK_DEBUG_
uint8_t destip[4];
uint16_t destport;
#endif
switch (getSn_SR(sn)) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) {
#ifdef _LOOPBACK_DEBUG_
getSn_DIPR(sn, destip);
destport = getSn_DPORT(sn);
printf("%d:Connected - %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2],
destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON);
}
uint16_t rxbuf_size = 0;
uint8_t sendbuf[1024];
switch (modbus_state) {
case STATE_WAIT_RTU:
if (rtu_rx->done) {
rxbuf_size = rtu_to_tcp(rtu_rx->buf, sendbuf, rtu_rx->size, buf);
}
if (rtu_rx->done || *timeout > 1000) {
rtu_rx->done = 0;
modbus_state = STATE_IDLE;
}
if ((size = getSn_RX_RSR(sn)) > 0) {
ret = recv(sn, buf, size);
if (ret <= 0) {
return ret;
}
}
break;
case STATE_IDLE:
if ((size = getSn_RX_RSR(sn)) > 0) {
if (size > DATA_BUF_SIZE)
size = DATA_BUF_SIZE;
ret = recv(sn, buf, size);
if (ret <= 0)
return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
if (buf[6] == get_slaveid()) {
rxbuf_size = handleModbusTcp(buf, sendbuf);
} else {
uint8_t rtu_tx[256];
uint16_t rtu_tx_len = tcp_to_rtu(buf, rtu_tx);
HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET);
HAL_UART_Transmit_DMA(&huart1, rtu_tx, rtu_tx_len);
*timeout = 0;
rtu_rx->done = 0;
modbus_state = STATE_WAIT_RTU;
}
}
break;
default:
break;
}
if (rxbuf_size > 0) {
sentsize = 0;
while (rxbuf_size != sentsize) {
ret = send(sn, sendbuf + sentsize, rxbuf_size - sentsize);
if (ret < 0) {
close(sn);
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
break;
case SOCK_CLOSE_WAIT:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:CloseWait\r\n",sn);
#endif
if ((ret = disconnect(sn)) != SOCK_OK)
return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT:
#ifdef _LOOPBACK_DEBUG_
printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
#endif
if ((ret = listen(sn)) != SOCK_OK)
return ret;
break;
case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:TCP server loopback start\r\n",sn);
#endif
if ((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn)
return ret;
#ifdef _LOOPBACK_DEBUG_
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
void to_big_endian_16(uint16_t value, uint8_t *dest) {
dest[0] = (uint8_t) (value >> 8); // 高字节
dest[1] = (uint8_t) (value & 0xFF); // 低字节
}
void to_big_endian_24(uint32_t value, uint8_t *dest) {
dest[0] = (uint8_t) (value >> 16); // 最高字节
dest[1] = (uint8_t) (value >> 8); // 次高字节
dest[2] = (uint8_t) (value & 0xFF); // 最低字节
}
uint8_t s7_rx_done = 1;
uint8_t comm_step = 0;
uint8_t read_write_flag = 0;
uint8_t sendbuf[1024];
uint16_t sendbufsize;
uint8_t comm1[] = { 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC0, 0x01, 0x0A, 0xC1, 0x02, 0x01, 0x00, 0xC2, 0x02, 0x03, 0x02 };
uint8_t comm2[] = { 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01,
0x01, 0xE0 };
uint8_t read[] = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02,
0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 };
uint8_t write[] = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02,
0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff };
uint16_t comm1_size = sizeof(comm1);
uint16_t comm2_size = sizeof(comm2);
uint16_t read_size = sizeof(read);
uint16_t write_size = sizeof(write);
typedef union {
uint8_t frame[31];
struct {
uint8_t padding1[23];
uint8_t read_size[2];
uint8_t db_id[2];
uint8_t padding2[1];
uint8_t start_address[3];
};
} s7ReadFrame;
typedef union {
uint8_t frame[1024];
struct {
uint8_t padding1[2];
uint8_t frame_size[2];
uint8_t padding2[11];
uint8_t dataframe_size[2];
uint8_t padding3[6];
uint8_t data_size[2];
uint8_t db_id[2];
uint8_t padding4[1];
uint8_t start_address[3];
uint8_t padding5[2];
uint8_t databyte_size_t8[2];
uint8_t data[800];
};
} s7WriteFrame;
s7ReadFrame readframe = { .frame = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12,
0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 } };
s7WriteFrame writeframe = { .frame = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12,
0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
uint16_t prepare_write_frame(uint8_t *write, uint16_t writebyte_size) {
if (writebyte_size == 0) {
return 0;
}
uint16_t frame_size = 35 + writebyte_size;
to_big_endian_16(frame_size, writeframe.frame_size);
to_big_endian_16(4 + writebyte_size, writeframe.dataframe_size);
to_big_endian_16(writebyte_size, writeframe.data_size);
to_big_endian_16(get_register(DBID2_IDX), writeframe.db_id);
to_big_endian_24(get_register(START2_IDX) * 8, writeframe.start_address);
to_big_endian_16(writebyte_size * 8, writeframe.databyte_size_t8);
memcpy(writeframe.data, write, writebyte_size);
return frame_size;
}
void print_list(uint8_t *buf, uint16_t size) {
for (uint16_t i = 0; i < size; i++) {
printf("0x%x ", buf[i]);
}
printf("\r\n");
}
uint8_t write_to_s7[1024] = { 0 };
int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *recvbuf, uint8_t *destip, uint16_t destport) {
int32_t ret; // return value for SOCK_ERRORs
uint16_t sentsize = 0;
// Port number for TCP client (will be increased)
static uint16_t any_port = 50000;
// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
uint8_t sr_status = getSn_SR(sn);
switch (sr_status) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
{
#ifdef _LOOPBACK_DEBUG_S7_
printf("%d:Connected to - %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Data Transaction Parts; Handle the [data receive and send] process
//////////////////////////////////////////////////////////////////////////////////////////////
if ((s7_rx_done == 1 || client_wait_timeout == 0) && s7Timegap == 0) {
sentsize = 0;
if (comm_step == 0) {
memcpy(sendbuf, comm1, comm1_size);
sendbufsize = comm1_size;
} else if (comm_step == 1) {
memcpy(sendbuf, comm2, comm2_size);
sendbufsize = comm2_size;
} else {
if (read_write_flag == 0) {
read_write_flag = 1;
// S7 > MODBUS
if (get_register(SIZE1_IDX) == 0) {
sendbufsize = 0;
} else {
to_big_endian_16(get_register(DBID1_IDX), readframe.db_id);
to_big_endian_24(get_register(START1_IDX) * 8, readframe.start_address);
to_big_endian_16(get_register(SIZE1_IDX) * 2, readframe.read_size);
memcpy(sendbuf, readframe.frame, read_size);
sendbufsize = read_size;
}
} else {
read_write_flag = 0;
// MODBUS > S7
get_register_range_to_u8(get_register(FROMMOD_IDX), get_register(SIZE2_IDX), write_to_s7);
uint16_t write_size = prepare_write_frame(write_to_s7, (uint16_t) get_register(SIZE2_IDX) * 2);
memcpy(sendbuf, writeframe.frame, write_size);
sendbufsize = write_size;
}
}
if (sendbufsize != 0) {
s7_rx_done = 0;
client_wait_timeout = 2000;
} else {
s7_rx_done = 1;
client_wait_timeout = 50;
}
#ifdef _LOOPBACK_DEBUG_S7_
if (get_register(1900) > 0) {
printf("TX step %d -> ", comm_step);
if (sendbufsize > 0) {
print_list(sendbuf, sendbufsize);
}
}
#endif
while (sendbufsize != sentsize) {
s7Timegap = get_register(1030);
ret = send(sn, sendbuf + sentsize, sendbufsize - sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
if (ret < 0) // Send Error occurred (sent data length < 0)
{
close(sn); // socket close
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
#ifdef _LOOPBACK_DEBUG_S7_
if (get_register(1900) > 0) {
printf("TX send return %ld -> ", ret);
}
#endif
}
}
if (s7_rx_done == 0) {
uint16_t clientrxsize = 0;
if ((clientrxsize = getSn_RX_RSR(sn)) > 0) {
int16_t ret = recv(sn, recvbuf, clientrxsize);
#ifdef _LOOPBACK_DEBUG_S7_
if (get_register(1900) > 0) {
printf("RX step %d -> ", comm_step);
print_list(recvbuf, clientrxsize);
}
#endif
if (ret > 0) {
if (comm_step < 2) {
comm_step++;
} else {
// 处理接收到的数据
if (read_write_flag == 1 && clientrxsize >= 27) {
uint16_t idx_in_modbus = get_register(TOMOD_IDX);
for (uint16_t idx = 25; idx < clientrxsize; idx += 2) {
if (get_register(1004) == 10) {
set_register(idx_in_modbus++, (recvbuf[idx + 1]) | (recvbuf[idx] << 8));
} else {
set_register(idx_in_modbus++, (recvbuf[idx]) | (recvbuf[idx + 1] << 8));
}
}
}
}
s7_rx_done = 1;
} else {
// 处理错误或断开连接
return ret;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
break;
case SOCK_CLOSE_WAIT:
#ifdef _LOOPBACK_DEBUG_S7_
//printf("%d:CloseWait\r\n",sn);
#endif
if ((ret = disconnect(sn)) != SOCK_OK)
return ret;
#ifdef _LOOPBACK_DEBUG_S7_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT:
#ifdef _LOOPBACK_DEBUG_S7_
printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
if ((ret = connect(sn, destip, destport)) != SOCK_OK) {
return ret; // Try to TCP connect to the TCP server (destination)
}
break;
case SOCK_CLOSED:
comm_step = 0;
close(sn);
if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) {
if (any_port == 0xffff)
any_port = 50000;
return ret; // TCP socket open with 'any_port' port number
}
#ifdef _LOOPBACK_DEBUG_S7_
//printf("%d:TCP client loopback start\r\n",sn);
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
int32_t loopback_udps(uint8_t sn, uint8_t *buf, uint16_t port) {
int32_t ret;
uint16_t size, sentsize;
uint8_t destip[4];
uint16_t destport;
switch (getSn_SR(sn)) {
case SOCK_UDP:
if ((size = getSn_RX_RSR(sn)) > 0) {
if (size > DATA_BUF_SIZE)
size = DATA_BUF_SIZE;
ret = recvfrom(sn, buf, size, destip, (uint16_t*) &destport);
if (ret <= 0) {
#ifdef _LOOPBACK_DEBUG_
printf("%d: recvfrom error. %ld\r\n", sn, ret);
#endif
return ret;
}
size = (uint16_t) ret;
sentsize = 0;
while (sentsize != size) {
ret = sendto(sn, buf + sentsize, size - sentsize, destip, destport);
if (ret < 0) {
#ifdef _LOOPBACK_DEBUG_
printf("%d: sendto error. %ld\r\n", sn, ret);
#endif
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
break;
case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
//printf("%d:UDP loopback start\r\n",sn);
#endif
if ((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
#endif
break;
default:
break;
}
return 1;
}
// ******************************* SLMP *******************************
uint8_t slmp_txbuf[1024], slmp_rxbuf[1024];
uint16_t read_address, read_size, write_address, write_size, read_idx_slmp = 0, write_idx_slmp = 0, curr_read_idx_slmp, curr_write_idx_slmp;
_Bool isReadRx(uint16_t clientrxsize) {
return clientrxsize == read_size * 2 + 11 && memcmp(slmp_rxbuf, readRespHeader, 7) == 0;
}
void mapReadRx() {
for (uint8_t i = 0; i < read_size; i++) {
uint16_t thisValue = slmp_rxbuf[i * 2 + 11] | (slmp_rxbuf[i * 2 + 12] << 8);
set_register(curr_read_idx_slmp * 20 + 1200 + i, thisValue);
}
}
_Bool update_read() {
curr_read_idx_slmp = read_idx_slmp;
uint16_t slave_id = get_register(writeST[read_idx_slmp].slave_id);
uint16_t plc_address = get_register(writeST[read_idx_slmp].plc_address);
uint16_t size = get_register(writeST[read_idx_slmp].size);
read_idx_slmp = (read_idx_slmp + 1) % 10;
if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20)
return 0;
read_address = plc_address;
read_size = size;
return 1;
}
_Bool update_write() {
curr_write_idx_slmp = write_idx_slmp;
uint16_t slave_id = get_register(readST[write_idx_slmp].slave_id);
uint16_t plc_address = get_register(readST[write_idx_slmp].plc_address);
uint16_t size = get_register(readST[write_idx_slmp].size);
write_idx_slmp = (write_idx_slmp + 1) % 10;
if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20)
return 0;
write_address = plc_address;
write_size = size;
return 1;
}
uint16_t slmpTimegap = 0;
void update_timeout_slmp() {
if (slmpTimegap < 0xffff) {
slmpTimegap++;
}
}
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) {
int32_t ret = 0;
// Port number for TCP client (will be increased)
static uint16_t any_port = 50000;
// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
uint8_t sr_status = getSn_SR(sn);
switch (sr_status) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
{
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Data Transaction Parts; Handle the [data receive and send] process
//////////////////////////////////////////////////////////////////////////////////////////////
// 处理接受到的报文
if (slmp_master_status == readySend) {
uint16_t clientrxsize = getSn_RX_RSR(sn);
if (clientrxsize > 0) {
ret = recv(sn, slmp_rxbuf, clientrxsize);
if (ret > 0) {
comm_count++;
if (isReadRx(clientrxsize)) {
mapReadRx();
slmp_master_status = slmp_readRxDone;
} else {
slmp_master_status = slmp_writeRxDone;
}
}
}
}
// 准备需要发送的报文
uint16_t sendbufsize = 0;
if (slmpTimegap > 1000 || slmp_master_status != readySend) {
slmpTimegap = 0;
switch (slmp_master_status) {
case slmp_writeRxDone:
if (update_read()) {
slmp_master_status = readySend;
if (read_size <= 20 && (read_address + read_size) < 8000) {
sendbufsize = handleSMLP_ReadTX(read_address, read_size);
memcpy(slmp_txbuf, frame.frameU8, 21);
}
}
break;
case slmp_readRxDone:
if (update_write()) {
slmp_master_status = readySend;
if (write_size <= 20 && (write_address + write_size) < 8000) {
sendbufsize = handleSLMP_WriteTX(write_address, write_size);
memcpy(slmp_txbuf, frame.frameU8, 21);
for (uint8_t i = 0; i < write_size; i++) {
uint16_t val = get_register(curr_write_idx_slmp * 20 + 1000 + i);
slmp_txbuf[i * 2 + 21] = val & 0xFF;
slmp_txbuf[i * 2 + 22] = (val >> 8) & 0xFF;
}
}
}
break;
default:
slmp_master_status = slmp_writeRxDone;
break;
}
}
// 发送该报文
uint16_t sentsize = 0;
while (sendbufsize != sentsize) {
// Data send process (User's buffer -> Destination through H/W Tx socket buffer)
ret = send(sn, slmp_txbuf + sentsize, sendbufsize - sentsize);
if (ret < 0) // Send Error occurred (sent data length < 0)
{
close(sn); // socket close
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
break;
case SOCK_CLOSE_WAIT:
if ((ret = disconnect(sn)) != SOCK_OK)
return ret;
break;
case SOCK_INIT:
if ((ret = connect(sn, destip, destport)) != SOCK_OK) {
return ret; // Try to TCP connect to the TCP server (destination)
}
break;
case SOCK_CLOSED:
close(sn);
if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) {
if (any_port == 0xffff)
any_port = 50000;
return ret; // TCP socket open with 'any_port' port number
}
break;
default:
break;
}
return 1;
}
/////////////////FINS start
uint8_t myRtumSTIdx = 0;
uint8_t omrfins_buf[1024];
uint8_t readrx[14] = { 0xc0, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x40 };
_Bool handleReadRecv(uint16_t size, uint16_t local_node) {
readrx[4] = local_node;
for (int i = 0; i < 14; i++) {
if (readrx[i] != omrfins_buf[i]) {
return 0;
}
}
uint16_t lastMyRtumSTIdx;
if (myRtumSTIdx == 0) {
lastMyRtumSTIdx = 19;
} else {
lastMyRtumSTIdx = myRtumSTIdx - 1;
}
uint16_t thisSize = get_register(myRtumST[lastMyRtumSTIdx].size);
if (thisSize * 2 + 14 != size) {
return 0;
}
for (int i = 0; i < thisSize; i++) {
uint8_t byte_h = omrfins_buf[i * 2 + 14];
uint8_t byte_l = omrfins_buf[i * 2 + 15];
uint16_t value = ((uint16_t) byte_h << 8) | byte_l;
set_register(1400 + (20 * lastMyRtumSTIdx) + i, value);
}
return 1;
}
uint8_t send_read[18] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x01, 0x01, 0x82, 0x00, 0x64, 0x00, 0x00, 0x0a };
uint8_t write_send[58] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x01, 0x02, 0x82, 0x00, 0x64, 0x00, 0x00, 0x02 };
uint16_t handleSend(uint16_t local_node) {
uint16_t sendsize = 0;
uint16_t slave_id = get_register(myRtumST[myRtumSTIdx].slave_id);
uint16_t plc_address = get_register(myRtumST[myRtumSTIdx].plc_address);
uint16_t size = get_register(myRtumST[myRtumSTIdx].size);
if (slave_id == 0 || slave_id >= 127 || size > 20 || size == 0) {
sendsize = 0;
} else {
if (myRtumSTIdx % 2 == 1) {
send_read[7] = local_node;
send_read[13] = (plc_address >> 8) & 0xFF;
send_read[14] = plc_address & 0xFF;
send_read[17] = size;
sendsize = 18;
for (int i = 0; i < sendsize; i++) {
omrfins_buf[i] = send_read[i];
}
} else {
write_send[7] = local_node;
write_send[13] = (plc_address >> 8) & 0xFF;
write_send[14] = plc_address & 0xFF;
write_send[17] = size;
for (int i = 0; i < size; i++) {
uint16_t value = get_register(1400 + (20 * myRtumSTIdx) + i);
write_send[i * 2 + 18] = (value >> 8) & 0xFF;
write_send[i * 2 + 19] = value & 0xFF;
}
sendsize = (size * 2) + 18;
for (int i = 0; i < sendsize; i++) {
omrfins_buf[i] = write_send[i];
}
}
}
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
return sendsize;
}
uint16_t fins_timeout = 0;
void update_timeout_fins() {
if (fins_timeout < 0xffff) {
fins_timeout++;
}
}
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node) {
int32_t ret;
uint16_t size = 0, sentsize = 0;
static uint16_t any_port = 50000;
switch (getSn_SR(sn)) {
case SOCK_UDP:
if ((size = getSn_RX_RSR(sn)) > 0) {
if (size > DATA_BUF_SIZE) {
size = DATA_BUF_SIZE;
}
ret = recvfrom(sn, omrfins_buf, size, destip, (uint16_t*) &destport);
if (ret <= 0) {
return ret;
} else {
comm_count++;
handleReadRecv(ret, local_node);
fins_timeout = 490;
}
}
size = 0;
if (fins_timeout > 500) {
size = handleSend(local_node);
if (size == 0) {
fins_timeout = 1000;
}
}
sentsize = 0;
while (sentsize != size) {
ret = sendto(sn, omrfins_buf + sentsize, size - sentsize, destip, destport);
if (ret < 0) {
return ret;
}
sentsize += ret;
}
break;
case SOCK_CLOSED:
if ((ret = socket(sn, Sn_MR_UDP, any_port, 0x00)) != sn) {
return ret;
}
break;
default:
break;
}
return 1;
}
//////////////FINS end
void loopback_comm(uint8_t *ip, uint16_t port, uint8_t node) {
if (master.plc_type == 0) {
loopback_tcpc_slmp(1, ip, port);
} else if (master.plc_type == 1) {
loopback_udpc_omrfins(1, ip, port, node);
}
loopback_tcps_modbus(0, 502);
}
#endif
+62
View File
@@ -0,0 +1,62 @@
#ifndef _LOOPBACK_H_
#define _LOOPBACK_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* Loopback test debug message printout enable */
//#define _LOOPBACK_DEBUG_
//#define _LOOPBACK_DEBUG_S7_
/* DATA_BUF_SIZE define for Loopback example */
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#endif
/************************/
/* Select LOOPBACK_MODE */
/************************/
//#define _LOOPBACK_DEBUG_SLMP_
#define LOOPBACK_MAIN_NOBLOCK 0
#define LOOPBACK_MODE LOOPBACK_MAIN_NOBLOCK
extern uint16_t client_wait_timeout;
extern uint16_t s7Timegap;
extern uint16_t comm_count;
extern _Bool comm_flag;
extern uint16_t slmpTimegap;
extern uint16_t fins_timeout;
void update_timeout_slmp();
typedef struct {
uint8_t buf[512];
_Bool done;
uint16_t size;
} RtuRxST;
/* TCP server Loopback test example */
int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port);
int32_t loopback_tcps_modbus_to485(uint8_t sn, uint8_t *buf, uint16_t port, uint16_t *timeout, RtuRxST *rtu_rx);
/* TCP client Loopback test example */
int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *recvbuf, uint8_t *destip, uint16_t destport);
/* UDP Loopback test example */
int32_t loopback_udps(uint8_t sn, uint8_t *buf, uint16_t port);
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport);
void update_timeout_fins();
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node);
void loopback_comm(uint8_t *ip, uint16_t port, uint8_t node);
#ifdef __cplusplus
}
#endif
#endif
+396
View File
@@ -0,0 +1,396 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "iwdg.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "w5500/w5500.h"
#include "w5500/w5500_port.h"
#include "w5500/loopback.h"
#include "w5500/webpages.h"
#include "w5500/httpUtil.h"
#include "w5500/slmpMaster.h"
#include "w5500/modbus_crc.h"
#include "w5500/modbusSlave.h"
#include "w5500/rtuMaster.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t local_ip[4], slmp_ip[4];
uint16_t slmp_port;
uint16_t out_word1 = 0;
uint16_t speed = 0;
uint16_t speed_idx = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//////////////////////////////////////////////////////////// ↓定时器(TIME6=1MS)↓ /////////////////////////////////////////////
uint16_t beatcnt = 0;
_Bool beatval;
uint16_t runbeat, rstbtn_timeout;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim6) {
update_timeout_slmp();
update_timeout_fins();
if (runbeat < 1000) { //控制运行指示灯间隔1秒闪烁
runbeat++;
} else {
runbeat = 0;
HAL_GPIO_TogglePin(run_GPIO_Port, run_Pin);
set_register(109, HAL_GPIO_ReadPin(run_GPIO_Port, run_Pin) ? 1 : 0); //109运行心跳位,间隔1秒闪烁
}
if (HAL_GPIO_ReadPin(rstbtn_GPIO_Port, rstbtn_Pin) == GPIO_PIN_RESET) { //系统参数初始化等待延时,如果按下3秒系统设定数据复位
if (rstbtn_timeout < 0xffff) {
rstbtn_timeout++;
} else {
rstbtn_timeout = 0;
}
}
}
}
//////////////////////////////////////////////////////////// ↑定时器↑ /////////////////////////////////////////////
/////////////////////////////////////
uint16_t flash_reset[100];
void reset_button() {
if (rstbtn_timeout > 3000) {
memset(flash_reset, 0xFF, 200);
Write_To_Flash_uint16(EDIT_FLASH_ADDRESS, flash_reset, 100);
rstbtn_timeout = 0;
set_register(120, get_register(120) + 1);
}
}
////////////////////////////////////////////////////////////↓将110地址输出给继电器↓/////////////////////////////////////////////
int getBit(int num, int n) {
return (num & (1 << n)) >> n;
}
void map_out(uint16_t startAddress) {
out_word1 = get_register(startAddress);
(getBit(out_word1, 0)) ? HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 1)) ? HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 2)) ? HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 3)) ? HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 4)) ? HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 5)) ? HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 6)) ? HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 7)) ? HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 8)) ? HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 9)) ? HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 10)) ? HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 11)) ? HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 12)) ? HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 13)) ? HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 14)) ? HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_RESET);
(getBit(out_word1, 15)) ? HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_RESET);
}
////////////////////////////////////////////////////////////↑将110地址输出给继电器上/////////////////////////////////////////////
void Read_From_Flash(uint32_t startAddress, uint16_t *buffer, uint32_t numElements) {
for (uint32_t i = 0; i < numElements; i++) {
buffer[i] = *(__IO uint16_t*) (startAddress + (i * 2));
}
}
void load_data() {
// 初始化保存数据
uint16_t readDataBuffer[FLASH_SAVE_SIZE];
Read_From_Flash(EDIT_FLASH_ADDRESS, readDataBuffer, FLASH_SAVE_SIZE);
set_register_range(MODBUS_SAVE_START, FLASH_SAVE_SIZE, readDataBuffer);
//加载参数
slmp_port = get_register(85);
slmp_ip[0] = get_register(81);
slmp_ip[1] = get_register(82);
slmp_ip[2] = get_register(83);
slmp_ip[3] = get_register(84);
if (slmp_ip[0] == 0xff || slmp_ip[1] == 0xff || slmp_ip[2] == 0xff || slmp_ip[3] == 0xff) {
slmp_ip[0] = 192;
slmp_ip[1] = 168;
slmp_ip[2] = 1;
slmp_ip[3] = 9;
set_register(81, 192);
set_register(82, 168);
set_register(83, 1);
set_register(84, 9);
}
if (slmp_port == 0xffff) {
set_register(85, 1025);
}
local_ip[0] = get_register(86);
local_ip[1] = get_register(87);
local_ip[2] = get_register(88);
local_ip[3] = get_register(89);
if (local_ip[0] == 0xff || local_ip[1] == 0xff || local_ip[2] == 0xff || local_ip[3] == 0xff) {
local_ip[0] = 192;
local_ip[1] = 168;
local_ip[2] = 1;
local_ip[3] = 13;
set_register(86, 192);
set_register(87, 168);
set_register(88, 1);
set_register(89, 13);
}
if (get_register(90) < 1 || get_register(90) > 0xff) {
set_register(90, 120);
}
for (uint16_t i = 0; i < 80; i++) {
if (get_register(i) == 0xffff) {
set_register(i, 0);
}
}
set_slaveid(get_register(90));
if (get_register(80) == 0xffff) {
set_register(80, 0);
}
uint32_t BaudRate = 0;
switch (get_register(80)) {
case 0:
BaudRate = 9600;
break;
case 1:
BaudRate = 14400;
break;
case 2:
BaudRate = 19200;
break;
case 3:
BaudRate = 38400;
break;
case 4:
BaudRate = 56000;
break;
case 5:
BaudRate = 57600;
break;
case 6:
BaudRate = 115200;
break;
default:
BaudRate = 9600;
break;
}
MX_USART1_UART_Init(BaudRate);
if (get_register(91) == 0xffff) {
set_register(91, 0);
}
master.localSlaveId = get_register(90);
master.isMaster = get_register(91) ? 1 : 0;
master.plc_type = get_register(92);
}
void commled() {
if (comm_count > 100) {
HAL_GPIO_TogglePin(comm_GPIO_Port, comm_Pin);
comm_count = 0;
}
if (getSn_SR(0) == SOCK_CLOSED && getSn_SR(1) == SOCK_CLOSED) {
HAL_GPIO_WritePin(comm_GPIO_Port, comm_Pin, GPIO_PIN_SET);
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void) {
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_IWDG_Init();
// MX_USART1_UART_Init();
MX_SPI1_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim6);
load_data();
w5500_init(local_ip);
httpServer_init();
reg_httpServer_cbfunc(NVIC_SystemReset, NULL);
reg_httpServer_webContent((uint8_t*) "index.html", (uint8_t*) rtu_master);
reg_httpServer_webContent((uint8_t*) "ajax.js", (uint8_t*) w5x00web_ajax_js);
initRtuMaster();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_IWDG_Refresh(&hiwdg);
map_out(110);
reset_button();
commled();
httpServer_run_main();
// 网口通信
loopback_comm(slmp_ip, slmp_port, local_ip[3]);
// 串口通信
RTU_Master_Loop();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void) {
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
+940
View File
@@ -0,0 +1,940 @@
#include "modbusSlave.h"
#include "modbus_crc.h"
#include "string.h"
#include "stdio.h"
uint16_t tcp_ledcnt = 0;
uint16_t u232_ledcnt = 0;
uint16_t u485_ledcnt = 0;
static uint8_t SLAVE_ID = 7;
static uint16_t Holding_Registers_Database[3000] = { 0 };
void set_slaveid(uint8_t val) {
SLAVE_ID = val;
}
uint8_t get_slaveid() {
return SLAVE_ID;
}
void get_register_range(uint16_t start, uint16_t size, uint16_t *buf) {
for (uint16_t i = 0; i < size; i++) {
buf[i] = Holding_Registers_Database[start + i];
}
}
void get_register_range_to_u8(uint16_t start, uint16_t count, uint8_t *buf) {
for (int i = 0; i < count; ++i) {
// 获取当前 uint16_t 值
uint16_t current = Holding_Registers_Database[start + i];
// 高字节放在低地址
if (get_register(1009) == 10) {
buf[2 * i] = (uint8_t) (current >> 8); // 高字节
buf[2 * i + 1] = (uint8_t) (current); // 低字节
} else {
buf[2 * i] = (uint8_t) (current); // 低字节
buf[2 * i + 1] = (uint8_t) (current >> 8); // 高字节
}
}
}
void set_register_range(uint16_t start, uint16_t size, uint16_t *buf) {
for (uint16_t i = 0; i < size; i++) {
Holding_Registers_Database[start + i] = buf[i];
}
}
void set_register_range_u8(uint16_t start, uint16_t size, uint8_t *buf) {
for (uint16_t i = 0; i < size; i++) {
Holding_Registers_Database[start + i] = buf[i];
}
}
void set_register(uint16_t index, uint16_t value) {
Holding_Registers_Database[index] = value;
}
void set_register_u32(uint16_t index, uint32_t value) {
uint16_t high = (value >> 16) & 0xFFFF;
uint16_t low = value & 0xFFFF;
Holding_Registers_Database[index] = low;
Holding_Registers_Database[index + 1] = high;
}
uint32_t get_register_u32(uint16_t index) {
uint32_t low = Holding_Registers_Database[index];
uint32_t high = Holding_Registers_Database[index + 1];
return (high << 16) | low;
}
void set_register_float(uint16_t index, float value) {
FloatToUint16 converter;
converter.value = value;
Holding_Registers_Database[index] = converter.u16[0];
Holding_Registers_Database[index + 1] = converter.u16[1];
}
uint16_t get_register(uint16_t index) {
return Holding_Registers_Database[index];
}
float get_register_float(uint16_t index) {
FloatToUint16 converter;
converter.u16[0] = Holding_Registers_Database[index];
converter.u16[1] = Holding_Registers_Database[index + 1];
return converter.value;
}
// *************************** MODBUS TCP start************************
uint16_t readHoldingRegs_tcp(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]);
uint16_t numRegs = ((rx_buf[10] << 8) | rx_buf[11]);
uint16_t frame_size = numRegs * 2 + 3;
tx_buf[0] = rx_buf[0];
tx_buf[1] = rx_buf[1];
tx_buf[2] = rx_buf[2];
tx_buf[3] = rx_buf[3];
tx_buf[4] = (frame_size >> 8) & 0xFF;
tx_buf[5] = frame_size & 0xFF;
tx_buf[6] = rx_buf[6];
tx_buf[7] = rx_buf[7];
tx_buf[8] = numRegs * 2;
int index = 9;
for (int i = 0; i < numRegs; i++) {
tx_buf[index++] = (Holding_Registers_Database[startAddr] >> 8) & 0xFF;
tx_buf[index++] = (Holding_Registers_Database[startAddr]) & 0xFF;
startAddr++;
}
return numRegs * 2 + 9;
}
uint16_t writeMultiRegs_tcp(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]);
uint8_t write_size = rx_buf[12] / 2;
uint8_t write_value_idx = 13;
while (write_size > 0) {
uint8_t left = rx_buf[write_value_idx++];
uint8_t right = rx_buf[write_value_idx++];
uint16_t this_value = (left << 8) | right;
Holding_Registers_Database[startAddr++] = this_value;
write_size--;
}
memcpy(tx_buf, rx_buf, 12);
tx_buf[4] = 0x00;
tx_buf[5] = 6;
return 12;
}
uint16_t writeSingleReg_tcp(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[8] << 8) | rx_buf[9]);
uint8_t left = rx_buf[10];
uint8_t right = rx_buf[11];
uint16_t this_value = (left << 8) | right;
Holding_Registers_Database[startAddr] = this_value;
memcpy(tx_buf, rx_buf, 12);
tx_buf[4] = 0x00;
tx_buf[5] = 0x06;
return 12;
}
uint16_t handleModbusTcp(uint8_t *rx_buf, uint8_t *tx_buf) {
switch (rx_buf[7]) {
case 0x03:
return readHoldingRegs_tcp(rx_buf, tx_buf);
break;
case 0x06:
return writeSingleReg_tcp(rx_buf, tx_buf);
break;
case 0x10:
return writeMultiRegs_tcp(rx_buf, tx_buf);
break;
default:
break;
}
return 0;
}
// *************************** MODBUS TCP end************************
uint8_t readHoldingRegs_rtu(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address
uint16_t numRegs = ((rx_buf[4] << 8) | rx_buf[5]); // number to registers master has requested
tx_buf[0] = SLAVE_ID; // slave ID
tx_buf[1] = rx_buf[1]; // function code
tx_buf[2] = numRegs * 2; // Byte count
int endIdx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
for (int i = 0; i < numRegs; i++) // Load the actual data into MDTxData buffer
{
tx_buf[endIdx++] = (Holding_Registers_Database[startAddr] >> 8) & 0xFF; // extract the higher byte
tx_buf[endIdx++] = (Holding_Registers_Database[startAddr]) & 0xFF; // extract the lower byte
startAddr++; // increment the register address
}
return endIdx;
}
uint8_t writeSingleReg_rtu(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address
uint16_t this_value = (rx_buf[4] << 8) | rx_buf[5];
Holding_Registers_Database[startAddr] = this_value;
memcpy(tx_buf, rx_buf, 6);
return 6;
}
uint8_t writeMultiRegs_rtu(uint8_t *rx_buf, uint8_t *tx_buf) {
uint16_t startAddr = ((rx_buf[2] << 8) | rx_buf[3]); // start Register Address
uint16_t numRegs = ((rx_buf[4] << 8) | rx_buf[5]); // number to registers master has requested
int endIdx = 7; // we need to keep track of index in MDRxData
for (int i = 0; i < numRegs; i++) {
uint16_t highByte = rx_buf[endIdx++] << 8;
uint16_t lowByte = rx_buf[endIdx++];
Holding_Registers_Database[startAddr++] = highByte | lowByte;
}
memcpy(tx_buf, rx_buf, 6);
return 6;
}
uint8_t handleModbusRtu(uint8_t *rx_buf, uint8_t *tx_buf) {
// if(huart == &huart3){
// u232_ledcnt =0;
// }else if(huart == &huart4){
// u485_ledcnt =0;
// }
switch (rx_buf[1]) {
case 0x03:
return readHoldingRegs_rtu(rx_buf, tx_buf);
break;
case 0x06:
return writeSingleReg_rtu(rx_buf, tx_buf);
break;
case 0x10:
return writeMultiRegs_rtu(rx_buf, tx_buf);
break;
default:
break;
}
return 0;
}
///////////////////////////////////////////↓485从站功能↓///////////////////////////////////////
void handleModbusRtu_NEW(uint8_t *rx_buf, uint8_t *tx_buf, uint8_t rxSize, UART_HandleTypeDef *huart) {
uint8_t txSize;
uint16_t crc = crc16(rx_buf, rxSize - 2);
if (crc != ((rx_buf[rxSize - 2] << 8) | rx_buf[rxSize - 1])) {
txSize = 0;
}
if (rx_buf[0] == SLAVE_ID) {
tcp_ledcnt = 0;
switch (rx_buf[1]) {
case 0x03:
txSize = readHoldingRegs_rtu(rx_buf, tx_buf);
break;
case 0x06:
txSize = writeSingleReg_rtu(rx_buf, tx_buf);
break;
case 0x10:
txSize = writeMultiRegs_rtu(rx_buf, tx_buf);
break;
default:
break;
}
}
if (txSize > 0) {
HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET);
crc = crc16(tx_buf, txSize);
tx_buf[txSize] = crc & 0xFF;
tx_buf[txSize + 1] = (crc >> 8) & 0xFF;
HAL_UART_Transmit_DMA(huart, tx_buf, txSize + 2);
} else {
HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_RESET);
HAL_UARTEx_ReceiveToIdle_DMA(huart, rx_buf, 1024);
}
}
///////////////////////////////////////////↑485从站功能↑///////////////////////////////////////
//extern uint8_t MDRxData[256];
//extern uint8_t MDTxData[256];
//extern UART_HandleTypeDef huart1;
//void modbusException (uint8_t exceptioncode)
//{
// //| SLAVE_ID | FUNCTION_CODE | Exception code | CRC |
// //| 1 BYTE | 1 BYTE | 1 BYTE | 2 BYTES |
//
// MDTxData[0] = MDRxData[0]; // slave ID
// MDTxData[1] = MDRxData[1]|0x80; // adding 1 to the MSB of the function code
// MDTxData[2] = exceptioncode; // Load the Exception code
// sendData(MDTxData, 3); // send Data... CRC will be calculated in the function
//}
//
//void check_exception(uint16_t numRegs, uint16_t endAddr){
// if ((numRegs<1)||(numRegs>125))
// {
// modbusException (ILLEGAL_DATA_VALUE);
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1;
// if (endAddr>999)
// {
// modbusException(ILLEGAL_DATA_ADDRESS);
// return 0;
// }
//}
//void sendData (uint8_t *data, int size)
//{
// // we will calculate the CRC in this function itself
// uint16_t crc = crc16(data, size);
// data[size] = crc&0xFF;
// data[size+1] = (crc>>8)&0xFF;
// HAL_UART_Transmit_DMA(&huart6, data, size+2);
//}
//
//void test_freq(void)
//{
// if(test_idx>9998){
// test_idx = 0;
// }
// Holding_Registers_Database[0] = test_idx;
// test_idx++;
//}
//
//void test_spi(uint16_t data){
// Holding_Registers_Database[0] = data;
//}
//
//void set_holdR_data(uint16_t* data, uint8_t size)
//{
// memcpy(Holding_Registers_Database, data, size);
//
//}
//
//void set_holdR_value(uint16_t idx, uint16_t value){
//
// Holding_Registers_Database[idx] = value;
//}
//
//uint16_t get_holdR_value(uint16_t idx){
// return Holding_Registers_Database[idx];
//}
//
//
//
//float get_hold_float(uint8_t idx){
// union FloatToBytes converter;
// uint16_t left = Holding_Registers_Database[idx];
// uint16_t right = Holding_Registers_Database[idx+1];
// converter.byteValues[0] = right;
// converter.byteValues[1] = right<<8;
// converter.byteValues[2] = left;
// converter.byteValues[3] = left<<8;
// return converter.floatValue;
//}
//
//void set_hold_float(uint8_t idx, float value){
// union FloatToBytes converter;
// converter.floatValue = value;
// uint16_t highValue = (converter.byteValues[1] << 8) | converter.byteValues[0];
// uint16_t lowValue = (converter.byteValues[3] << 8) | converter.byteValues[2];
// Holding_Registers_Database[idx] = highValue;
// Holding_Registers_Database[idx+1] = lowValue;
//}
//
//void setup_defalut_values(float* default_values){
// for(uint8_t i=0; i<7;i++){
// union FloatToBytes converter;
// converter.floatValue = default_values[i];
// uint16_t highValue = (converter.byteValues[1] << 8) | converter.byteValues[0];
// uint16_t lowValue = (converter.byteValues[3] << 8) | converter.byteValues[2];
// Holding_Registers_Database[i*2] = highValue;
// Holding_Registers_Database[i*2+1] = lowValue;
// }
//
//}
//
//void setup_default_uint16(uint16_t* default_values,uint8_t size){
// for(uint8_t i=0; i<size;i++){
// Holding_Registers_Database[i] = default_values[i];
// }
//}
//
//
//void get_ips(uint8_t* data){
// // local ip/port + remote ip/port
// for(uint8_t i=0; i<10;i++){
// data[i] = Holding_Registers_Database[10+i];
// }
//}
//
//
//void getHoldingRegistersData(uint16_t* data){
// for (int i = 0; i < 10; i++) {
// data[i] = Holding_Registers_Database[i];
// }
//}
//
//uint8_t readHoldingRegs (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested
// if ((numRegs<1)||(numRegs>125)) // maximum no. of Registers as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1; // end Register
// if (endAddr>1000)
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = numRegs*2; // Byte count
// int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
//
// for (int i=0; i<numRegs; i++) // Load the actual data into MDTxData buffer
// {
// MDTxData[indx++] = (Holding_Registers_Database[startAddr]>>8)&0xFF; // extract the higher byte
// MDTxData[indx++] = (Holding_Registers_Database[startAddr])&0xFF; // extract the lower byte
// startAddr++; // increment the register address
// }
//
// sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself
//
// return 1; // success
//}
//
//uint8_t readHoldingRegs_with_targetHuart (UART_HandleTypeDef *targetHuart)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested
// if ((numRegs<1)||(numRegs>125)) // maximum no. of Registers as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1; // end Register
// if (endAddr>1000)
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = numRegs*2; // Byte count
// int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
//
// for (int i=0; i<numRegs; i++) // Load the actual data into MDTxData buffer
// {
// MDTxData[indx++] = (Holding_Registers_Database[startAddr]>>8)&0xFF; // extract the higher byte
// MDTxData[indx++] = (Holding_Registers_Database[startAddr])&0xFF; // extract the lower byte
// startAddr++; // increment the register address
// }
//
// sendData_with_target_huart(MDTxData, indx, targetHuart); // send data... CRC will be calculated in the function itself
//
// return 1; // success
//}
//
//
//uint8_t readInputRegs (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested
// if ((numRegs<1)||(numRegs>125)) // maximum no. of Registers as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1; // end Register
// if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// // Prepare MDTxData buffer
//
// //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC |
// //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = numRegs*2; // Byte count
// int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
//
// for (int i=0; i<numRegs; i++) // Load the actual data into MDTxData buffer
// {
// MDTxData[indx++] = (Input_Registers_Database[startAddr]>>8)&0xFF; // extract the higher byte
// MDTxData[indx++] = (Input_Registers_Database[startAddr])&0xFF; // extract the lower byte
// startAddr++; // increment the register address
// }
//
// sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//uint8_t writeHoldingRegs (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested
// if ((numRegs<1)||(numRegs>123)) // maximum no. of Registers as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1; // end Register
// if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* start saving 16 bit data
// * Data starts from MDRxData[7] and we need to combine 2 bytes together
// * 16 bit Data = firstByte<<8|secondByte
// */
// int indx = 7; // we need to keep track of index in MDRxData
// for (int i=0; i<numRegs; i++)
// {
// uint16_t highByte = MDRxData[indx++] << 8;
// uint16_t lowByte = MDRxData[indx++];
// Holding_Registers_Database[startAddr++] = highByte | lowByte;
//// Holding_Registers_Database[startAddr++] = (MDRxData[indx++]<<8)|MDRxData[indx++];
// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | num of Regs | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // num of Regs HIGH Byte
// MDTxData[5] = MDRxData[5]; // num of Regs LOW Byte
//
// sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself
//
// return 1; // success
//}
//
//uint8_t writeHoldingRegs_with_targetHuart (UART_HandleTypeDef *targetHuart)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numRegs = ((MDRxData[4]<<8)|MDRxData[5]); // number to registers master has requested
// if ((numRegs<1)||(numRegs>123)) // maximum no. of Registers as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numRegs-1; // end Register
// if (endAddr>49) // end Register can not be more than 49 as we only have record of 50 Registers in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* start saving 16 bit data
// * Data starts from MDRxData[7] and we need to combine 2 bytes together
// * 16 bit Data = firstByte<<8|secondByte
// */
// int indx = 7; // we need to keep track of index in MDRxData
// for (int i=0; i<numRegs; i++)
// {
// uint16_t highByte = MDRxData[indx++] << 8;
// uint16_t lowByte = MDRxData[indx++];
// Holding_Registers_Database[startAddr++] = highByte | lowByte;
//// Holding_Registers_Database[startAddr++] = (MDRxData[indx++]<<8)|MDRxData[indx++];
// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | num of Regs | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // num of Regs HIGH Byte
// MDTxData[5] = MDRxData[5]; // num of Regs LOW Byte
//
// sendData_with_target_huart(MDTxData, 6, targetHuart); // send data... CRC will be calculated in the function itself
//
// return 1; // success
//}
//
//uint8_t writeSingleReg (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// if (startAddr>999) // The Register Address can not be more than 49 as we only have record of 50 Registers in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* Save the 16 bit data
// * Data is the combination of 2 bytes, MDRxData[4] and MDRxData[5]
// */
// uint16_t this_value = (MDRxData[4]<<8)|MDRxData[5];
// Holding_Registers_Database[startAddr] = this_value;
//
//
//// if(startAddr==0){
//// htim2.Instance->CCR1 = this_value; // 25-125 = 0-180
//// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // Reg Data HIGH Byte
// MDTxData[5] = MDRxData[5]; // Reg Data LOW Byte
//
// sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//
//uint8_t writeSingleReg_with_targetHuart (UART_HandleTypeDef *targetHuart)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// if (startAddr>999) // The Register Address can not be more than 49 as we only have record of 50 Registers in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* Save the 16 bit data
// * Data is the combination of 2 bytes, MDRxData[4] and MDRxData[5]
// */
// uint16_t this_value = (MDRxData[4]<<8)|MDRxData[5];
// Holding_Registers_Database[startAddr] = this_value;
//
//
//// if(startAddr==0){
//// htim2.Instance->CCR1 = this_value; // 25-125 = 0-180
//// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // Reg Data HIGH Byte
// MDTxData[5] = MDRxData[5]; // Reg Data LOW Byte
//
// sendData_with_target_huart(MDTxData, 6, targetHuart); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//
//uint8_t readCoils (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Coil Address
//
// uint16_t numCoils = ((MDRxData[4]<<8)|MDRxData[5]); // number to coils master has requested
// if ((numCoils<1)||(numCoils>2000)) // maximum no. of coils as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numCoils-1; // Last coils address
// if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
//
// //reset MDTxData buffer
// memset (MDTxData, '\0', 256);
//
// // Prepare MDTxData buffer
//
// //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC |
// //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = (numCoils/8) + ((numCoils%8)>0 ? 1:0); // Byte count
// int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
//
// /* The approach is simple. We will read 1 bit at a time and store them in the Txdata buffer.
// * First find the offset in the first byte we read from, for eg- if the start coil is 13,
// * we will read from database[1] with an offset of 5. This bit will be stored in the MDTxData[0] at 0th position.
// * Then we will keep shifting the database[1] to the right and read the bits.
// * Once the bitposition has crossed the value 7, we will increment the startbyte
// * When the indxposition exceeds 7, we increment the indx variable, so to copy into the next byte of the MDTxData
// * This keeps going until the number of coils required have been copied
// */
// int startByte = startAddr/8; // which byte we have to start extracting the data from
// uint16_t bitPosition = startAddr%8; // The shift position in the first byte
// int indxPosition = 0; // The shift position in the current indx of the MDTxData buffer
//
// // Load the actual data into MDTxData buffer
// for (int i=0; i<numCoils; i++)
// {
// MDTxData[indx] |= ((Coils_Database[startByte] >> bitPosition) &0x01) << indxPosition;
// indxPosition++; bitPosition++;
// if (indxPosition>7) // if the indxposition exceeds 7, we have to copy the data into the next byte position
// {
// indxPosition = 0;
// indx++;
// }
// if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte
// {
// bitPosition=0;
// startByte++;
// }
// }
//
// if (numCoils%8 != 0)indx++; // increment the indx variable, only if the numcoils is not a multiple of 8
// sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//
//uint8_t readInputs (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Register Address
//
// uint16_t numCoils = ((MDRxData[4]<<8)|MDRxData[5]); // number to coils master has requested
// if ((numCoils<1)||(numCoils>2000)) // maximum no. of coils as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numCoils-1; // Last coils address
// if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
//
// //reset MDTxData buffer
// memset (MDTxData, '\0', 256);
//
// // Prepare MDTxData buffer
//
// //| SLAVE_ID | FUNCTION_CODE | BYTE COUNT | DATA | CRC |
// //| 1 BYTE | 1 BYTE | 1 BYTE | N*2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = (numCoils/8) + ((numCoils%8)>0 ? 1:0); // Byte count
// int indx = 3; // we need to keep track of how many bytes has been stored in MDTxData Buffer
//
// /* The approach is simple. We will read 1 bit at a time and store them in the Txdata buffer.
// * First find the offset in the first byte we read from, for eg- if the start coil is 13,
// * we will read from database[1] with an offset of 5. This bit will be stored in the MDTxData[0] at 0th position.
// * Then we will keep shifting the database[1] to the right and read the bits.
// * Once the bitposition has crossed the value 7, we will increment the startbyte
// * When the indxposition exceeds 7, we increment the indx variable, so to copy into the next byte of the MDTxData
// * This keeps going until the number of coils required have been copied
// */
// int startByte = startAddr/8; // which byte we have to start extracting the data from
// uint16_t bitPosition = startAddr%8; // The shift position in the first byte
// int indxPosition = 0; // The shift position in the current indx of the MDTxData buffer
//
// // Load the actual data into MDTxData buffer
// for (int i=0; i<numCoils; i++)
// {
// MDTxData[indx] |= ((Inputs_Database[startByte] >> bitPosition) &0x01) << indxPosition;
// indxPosition++; bitPosition++;
// if (indxPosition>7) // if the indxposition exceeds 7, we have to copy the data into the next byte position
// {
// indxPosition = 0;
// indx++;
// }
// if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte
// {
// bitPosition=0;
// startByte++;
// }
// }
//
// if (numCoils%8 != 0)indx++; // increment the indx variable, only if the numcoils is not a multiple of 8
// sendData(MDTxData, indx); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//
//uint8_t writeSingleCoil (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Coil Address
//
// if (startAddr>199) // The Coil Address can not be more than 199 as we only have record of 200 Coils in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* Calculation for the bit in the database, where the modification will be done */
// int startByte = startAddr/8; // which byte we have to start writing the data into
// uint16_t bitPosition = startAddr%8; // The shift position in the first byte
//
//
// /* The next 2 bytes in the MDRxData determines the state of the coil
// * A value of FF 00 hex requests the coil to be ON.
// * A value of 00 00 requests it to be OFF.
// * All other values are illegal and will not affect the coil.
// */
//
// if ((MDRxData[4] == 0xFF) && (MDRxData[5] == 0x00))
// {
// Coils_Database[startByte] |= 1<<bitPosition; // Replace that bit with 1
// }
//
// else if ((MDRxData[4] == 0x00) && (MDRxData[5] == 0x00))
// {
// Coils_Database[startByte] &= ~(1<<bitPosition); // Replace that bit with 0
// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // Coil Data HIGH Byte
// MDTxData[5] = MDRxData[5]; // Coil Data LOW Byte
//
// sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//uint8_t writeMultiCoils (void)
//{
// uint16_t startAddr = ((MDRxData[2]<<8)|MDRxData[3]); // start Coil Address
//
// uint16_t numCoils = ((MDRxData[4]<<8)|MDRxData[5]); // number to coils master has requested
// if ((numCoils<1)||(numCoils>1968)) // maximum no. of coils as per the PDF
// {
// modbusException (ILLEGAL_DATA_VALUE); // send an exception
// return 0;
// }
//
// uint16_t endAddr = startAddr+numCoils-1; // Last coils address
// if (endAddr>199) // end coil can not be more than 199 as we only have record of 200 (0-199) coils in total
// {
// modbusException(ILLEGAL_DATA_ADDRESS); // send an exception
// return 0;
// }
//
// /* Calculation for the bit in the database, where the modification will be done */
// int startByte = startAddr/8; // which byte we have to start writing the data into
// uint16_t bitPosition = startAddr%8; // The shift position in the first byte
// int indxPosition = 0; // The shift position in the current indx of the MDRxData buffer
//
// int indx = 7; // we need to keep track of index in MDRxData
//
// /* The approach is simple. We will read 1 bit (starting from the very first bit in the MDRxData Buffer)
// * at a time and store them in the Database.
// * First find the offset in the first byte we write into, for eg- if the start coil is 13,
// * we will Write into database[1] with an offset of 5. This bit is read from the MDRxData[indx] at 0th indxposition.
// * Then we will keep shifting the MDRxData[indx] to the right and read the bits.
// * Once the bitposition has crossed the value 7, we will increment the startbyte and start modifying the next byte in the database
// * When the indxposition exceeds 7, we increment the indx variable, so to copy from the next byte of the MDRxData
// * This keeps going until the number of coils required have been modified
// */
//
// // Modify the bits as per the Byte received
// for (int i=0; i<numCoils; i++)
// {
// if (((MDRxData[indx]>>indxPosition)&0x01) == 1)
// {
// Coils_Database[startByte] |= 1<<bitPosition; // replace that bit with 1
// }
// else
// {
// Coils_Database[startByte] &= ~(1<<bitPosition); // replace that bit with 0
// }
//
// bitPosition++; indxPosition++;
//
// if (indxPosition>7) // if the indxposition exceeds 7, we have to copy the data into the next byte position
// {
// indxPosition = 0;
// indx++;
// }
// if (bitPosition>7) // if the bitposition exceeds 7, we have to increment the startbyte
// {
// bitPosition=0;
// startByte++;
// }
// }
//
// // Prepare Response
//
// //| SLAVE_ID | FUNCTION_CODE | Start Addr | Data | CRC |
// //| 1 BYTE | 1 BYTE | 2 BYTE | 2 BYTES | 2 BYTES |
//
// MDTxData[0] = SLAVE_ID; // slave ID
// MDTxData[1] = MDRxData[1]; // function code
// MDTxData[2] = MDRxData[2]; // Start Addr HIGH Byte
// MDTxData[3] = MDRxData[3]; // Start Addr LOW Byte
// MDTxData[4] = MDRxData[4]; // num of coils HIGH Byte
// MDTxData[5] = MDRxData[5]; // num of coils LOW Byte
//
// sendData(MDTxData, 6); // send data... CRC will be calculated in the function itself
// return 1; // success
//}
//
//
//
//
//uint8_t modbus0x06_tcp (uint8_t* rx_buf)
//{
// uint16_t startAddr = ((rx_buf[8]<<8)|rx_buf[9]);
// uint8_t left = rx_buf[10];
// uint8_t right = rx_buf[11];
// uint16_t this_value = (left<<8)|right;
// Holding_Registers_Database[startAddr] = this_value;
// return 1;
//}
+38
View File
@@ -0,0 +1,38 @@
/*
* modbusSlave.h
*
* Created on: Oct 27, 2022
* Author: controllerstech.com
*/
#ifndef INC_MODBUSSLAVE_H_
#define INC_MODBUSSLAVE_H_
#include "main.h"
typedef union {
float value;
uint16_t u16[2];
} FloatToUint16;
uint32_t get_register_u32(uint16_t index);
void set_slaveid(uint8_t val);
uint8_t get_slaveid();
void set_register_range(uint16_t start, uint16_t size, uint16_t *buf);
void set_register_range_u8(uint16_t start, uint16_t size, uint8_t *buf);
void set_register_u32(uint16_t index, uint32_t value);
void set_register(uint16_t index, uint16_t value);
void set_register_float(uint16_t index, float value);
uint16_t get_register(uint16_t index);
void get_register_range(uint16_t start, uint16_t size, uint16_t *buf);
void get_register_range_to_u8(uint16_t start, uint16_t count, uint8_t *buf);
float get_register_float(uint16_t index);
uint8_t handleModbusRtu(uint8_t *rx_buf, uint8_t *tx_buf);
void handleModbusRtu_NEW(uint8_t *rx_buf, uint8_t *tx_buf, uint8_t rxSize, UART_HandleTypeDef *huart);
uint16_t handleModbusTcp(uint8_t* rx_buf, uint8_t* tx_buf);
uint16_t readHoldingRegs_tcp (uint8_t* rx_buf, uint8_t* tx_buf);
uint16_t writeSingleReg_tcp (uint8_t* rx_buf, uint8_t* tx_buf);
uint16_t writeMultiRegs_tcp (uint8_t* rx_buf, uint8_t* tx_buf);
#endif
+170
View File
@@ -0,0 +1,170 @@
#include "w5500/modbusSlave.h"
#include "w5500/modbus_crc.h"
#include "usart.h"
#include "gpio.h"
#include "rtuMaster.h"
rtumST readST[10] = { { .slave_id = 0, .slave_address = 1, .plc_address = 2, .size = 3 }, { .slave_id = 4, .slave_address = 5, .plc_address = 6, .size = 7 }, {
.slave_id = 8, .slave_address = 9, .plc_address = 10, .size = 11 }, { .slave_id = 12, .slave_address = 13, .plc_address = 14, .size = 15 }, {
.slave_id = 16, .slave_address = 17, .plc_address = 18, .size = 19 }, { .slave_id = 20, .slave_address = 21, .plc_address = 22, .size = 23 }, {
.slave_id = 24, .slave_address = 25, .plc_address = 26, .size = 27 }, { .slave_id = 28, .slave_address = 29, .plc_address = 30, .size = 31 }, {
.slave_id = 32, .slave_address = 33, .plc_address = 34, .size = 35 }, { .slave_id = 36, .slave_address = 37, .plc_address = 38, .size = 39 } };
rtumST writeST[10] = { { .slave_id = 40, .slave_address = 41, .plc_address = 42, .size = 43 }, { .slave_id = 44, .slave_address = 45, .plc_address = 46, .size =
47 }, { .slave_id = 48, .slave_address = 49, .plc_address = 50, .size = 51 }, { .slave_id = 52, .slave_address = 53, .plc_address = 54, .size = 55 }, {
.slave_id = 56, .slave_address = 57, .plc_address = 58, .size = 59 }, { .slave_id = 60, .slave_address = 61, .plc_address = 62, .size = 63 }, {
.slave_id = 64, .slave_address = 65, .plc_address = 66, .size = 67 }, { .slave_id = 68, .slave_address = 69, .plc_address = 70, .size = 71 }, {
.slave_id = 72, .slave_address = 73, .plc_address = 74, .size = 75 }, { .slave_id = 76, .slave_address = 77, .plc_address = 78, .size = 79 } };
rtumST myRtumST[20];
RtuMaster master = { .localStartIdx = 1000 };
void initRtuMaster() {
for (int i = 0; i < 10; i++) {
myRtumST[i * 2] = readST[i];
myRtumST[i * 2 + 1] = writeST[i];
}
if (master.isMaster) {
RE_RESET;
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, master.rxbuf, 255);
}
HAL_GPIO_WritePin(comm485_GPIO_Port, comm485_Pin, GPIO_PIN_SET);
}
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() {
if (master.isMaster) {
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;
}
} else {
if (master.rxdone) {
HAL_GPIO_TogglePin(comm485_GPIO_Port, comm485_Pin);
handleModbusRtu_NEW(master.rxbuf, master.rxbuf, master.rxsize, &huart1);
master.rxdone = 0;
}
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart == &huart1) {
RE_RESET;
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, master.rxbuf, 255);
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
if (huart == &huart1) {
master.rxdone = 1;
master.rxsize = Size;
}
}
+41
View File
@@ -0,0 +1,41 @@
/*
* rtuMaster.h
*
* Created on: Mar 26, 2026
* Author: 18896
*/
#ifndef RTUMASTER_H_
#define RTUMASTER_H_
#define RE_SET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET)
#define RE_RESET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_RESET)
typedef struct {
uint16_t slave_id;
uint16_t slave_address;
uint16_t plc_address;
uint16_t size;
} rtumST;
typedef struct {
uint8_t plc_type;
_Bool isMaster;
_Bool rxdone;
uint16_t rxsize, txsize;
uint16_t timeout;
uint8_t rxbuf[255];
uint8_t txbuf[255];
uint16_t localSlaveId;
uint16_t localStartIdx;
} RtuMaster;
extern rtumST readST[10];
extern rtumST writeST[10];
extern rtumST myRtumST[20];
extern RtuMaster master;
void initRtuMaster();
void RTU_Master_Loop();
#endif /* RTUMASTER_H_ */
+37
View File
@@ -0,0 +1,37 @@
/*
* smlMaster.c
*
* Created on: Apr 15, 2025
* Author: 18896
*/
#include "main.h"
#include "slmpMaster.h"
SLMPSTATUS slmp_master_status = slmp_writeRxDone;
uint8_t readRespHeader[7] = { 0xD0, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00 };
SMLPFrame frame = { .frameStruct.header = 0x0050, .frameStruct.taget = 0xff00, .frameStruct.index = 0x03FF, .frameStruct.subIdx = 0x00, .frameStruct.timeout = 0x0004, .frameStruct.subCmd = 0x0000, .frameStruct.DM = 0xA8 };
uint24_t u32_to_u24(uint32_t val) {
uint24_t r;
r.byte0 = val & 0xFF;
r.byte1 = (val >> 8) & 0xFF;
r.byte2 = (val >> 16) & 0xFF;
return r;
}
uint16_t handleSMLP_ReadTX(uint32_t start, uint16_t size) {
frame.frameStruct.cmd = 0x0401;
frame.frameStruct.requestSize = 0x000c;
frame.frameStruct.start = u32_to_u24(start);
frame.frameStruct.size = size;
return 21;
}
uint16_t handleSLMP_WriteTX(uint32_t start, uint16_t size) {
frame.frameStruct.cmd = 0x1401;
frame.frameStruct.requestSize = 0x000C + (size * 2);
frame.frameStruct.start = u32_to_u24(start);
frame.frameStruct.size = size;
return size * 2 + 21;
}
+53
View File
@@ -0,0 +1,53 @@
/*
* smlpMaster.h
*
* Created on: Apr 15, 2025
* Author: 18896
*/
#ifndef SLMPMASTER_H_
#define SLMPMASTER_H_
#define SLMP_HEAD_SIZE 9
typedef enum {
slmp_read, slmp_readRxDone, slmp_write, slmp_writeRxDone,readySend
} SLMPSTATUS;
typedef struct {
uint8_t byte0;
uint8_t byte1;
uint8_t byte2;
} uint24_t;
uint24_t u32_to_u24(uint32_t val);
typedef union {
struct __attribute__((__packed__)){
uint16_t header;
uint16_t taget;
uint16_t index;
uint8_t subIdx;
uint16_t requestSize;
uint16_t timeout;
uint16_t cmd;
uint16_t subCmd;
uint24_t start;
uint8_t DM;
uint16_t size;
} frameStruct;
uint8_t frameU8[21];
} SMLPFrame;
extern SLMPSTATUS slmp_master_status;
extern uint8_t readRespHeader[7];
extern SMLPFrame frame;
uint16_t handleSMLP_ReadTX(uint32_t start, uint16_t size);
uint16_t handleSLMP_WriteTX(uint32_t start, uint16_t size);
#endif /* SLMPMASTER_H_ */