refactor: 分文件重构,新建protocols文件夹,三个协议分文件存放:modbustcpslave/finsudpmaster/slmptcpmaster,结构更清晰

This commit is contained in:
claw committed 2026-04-02 22:35:56 +08:00
1 parent 99fa72eaa0
commit e3519bf1f4
8 files changed
+852 -741

No files matched your search

+189
View File
@@ -0,0 +1,189 @@
/*
* finsudpmaster.c
* 欧姆龙FINS UDP通信主站
* 轮询方式: myRtumSTIdx 0~19轮转, 奇数索引读, 偶数索引写
*/
#include "finsudpmaster.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "rtuMaster.h"
#include "main.h"
#include <string.h>
uint8_t myRtumSTIdx = 0; // 轮询索引 (0~19对应myRtumST[20])
uint8_t omrfins_buf[1024]; // FINS收发缓冲区
// 读响应固定头,只有匹配这个头才会处理
uint8_t readrx[14] = { 0xc0, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x40 };
// 处理FINS读响应
// 返回1: 处理成功,0: 不是有效响应
_Bool handleReadRecv(uint16_t size, uint16_t local_node) {
// 设置本地节点号
readrx[4] = (uint8_t)local_node;
// 对比前14字节,确认是读响应
for (int i = 0; i < 14; i++) {
if (readrx[i] != omrfins_buf[i]) {
return 0;
}
}
// 找到上一次发送对应的索引 (因为发送后索引+1,所以响应对应前一个)
uint16_t prevIdx;
if (myRtumSTIdx == 0) {
prevIdx = 19;
} else {
prevIdx = myRtumSTIdx - 1;
}
// 只有读响应会走到这里,读使用奇数索引,验证一下
if ((prevIdx % 2 == 1) != (memcmp(readrx, readrx, 7) == 0)) {
return 0; // 不匹配,丢弃
}
uint16_t thisSize = get_register(myRtumST[prevIdx].size);
// 大小检查 + 边界保护 (最大20个寄存器)
if (thisSize > 20 || thisSize * 2 + 14 != size) {
return 0;
}
// FINS使用大端序: 高字节在前,低字节在后
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;
// 存储位置: 1400 + (索引*20) + 偏移
set_register(1400 + (20 * prevIdx) + 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 };
// 构建FINS请求帧,返回发送长度
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 == 0 || size > 20) {
sendsize = 0;
} else {
if (myRtumSTIdx % 2 == 1) {
// 奇数索引 -> 读操作
send_read[7] = (uint8_t)local_node;
send_read[13] = (plc_address >> 8) & 0xFF;
send_read[14] = plc_address & 0xFF;
send_read[17] = (uint8_t)size;
sendsize = 18;
// 拷贝到发送缓冲区,带边界检查
for (int i = 0; i < sendsize && i < sizeof(omrfins_buf); i++) {
omrfins_buf[i] = send_read[i];
}
} else {
// 偶数索引 -> 写操作
sendsize = (size * 2) + 18;
// 检查缓冲区边界,防止越界
if (sendsize > sizeof(write_send) || sendsize > sizeof(omrfins_buf)) {
return 0;
}
write_send[7] = (uint8_t)local_node;
write_send[13] = (plc_address >> 8) & 0xFF;
write_send[14] = plc_address & 0xFF;
write_send[17] = (uint8_t)size;
// 填充数据,FINS大端序: 高字节在前
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;
}
// 拷贝到发送缓冲区
for (int i = 0; i < sendsize; i++) {
omrfins_buf[i] = write_send[i];
}
}
}
// 索引轮转
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
return sendsize;
}
uint16_t fins_timeout = 0;
// FINS超时计数,由主循环调用
void update_timeout_fins(void) {
if (fins_timeout < 0xffff) {
fins_timeout++;
}
}
// FINS UDP客户端主函数
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 > sizeof(omrfins_buf)) {
size = sizeof(omrfins_buf);
}
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 (size != 0 && sentsize != size) {
ret = sendto(sn, omrfins_buf + sentsize, size - sentsize, destip, destport);
if (ret < 0) {
return ret;
}
sentsize += ret;
}
break;
case SOCK_CLOSED:
// 端口绕回处理
if (any_port == 0xffff) {
any_port = 50000;
}
if ((ret = socket(sn, Sn_MR_UDP, any_port++, 0x00)) != sn) {
return ret;
}
break;
default:
break;
}
return 1;
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef FINSUDPMASTER_H
#define FINSUDPMASTER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
extern uint8_t myRtumSTIdx;
extern uint8_t omrfins_buf[1024];
extern uint16_t fins_timeout;
extern uint16_t comm_count;
// 处理接收到的FINS读响应
_Bool handleReadRecv(uint16_t size, uint16_t local_node);
// 构建FINS请求帧,返回发送长度
uint16_t handleSend(uint16_t local_node);
// 超时计数,由主循环调用
void update_timeout_fins(void);
// FINS UDP客户端主函数
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node);
#ifdef __cplusplus
}
#endif
#endif
+243
View File
@@ -0,0 +1,243 @@
/*
* modbustcpslave.c
* Modbus TCP从站服务
*/
#include "modbustcpslave.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "modbusSlave.h"
#include "modbus_crc.h"
#include "usart.h"
#include <string.h>
#include "gpio.h"
uint8_t loopback_tcps_modbus_rxbuf[1024];
uint8_t loopback_tcps_modbus_txbuf[1024];
// 将Modbus TCP转RTU (转发到485)
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 (从485转发回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;
// Modbus TCP服务主函数
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) {
if (size > DATA_BUF_SIZE)
size = DATA_BUF_SIZE;
ret = recv(sn, loopback_tcps_modbus_rxbuf, size);
if (ret <= 0)
return ret;
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;
}
}
}
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转发主函数
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;
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);
RE_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;
}
}
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;
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef MODBUSTCPSLAVE_H
#define MODBUSTCPSLAVE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#ifndef DATA_BUF_SIZE
#define DATA_BUF_SIZE 2048
#endif
// RTU接收状态结构体
typedef struct {
uint8_t buf[512];
_Bool done;
uint16_t size;
} RtuRxST;
extern uint8_t loopback_tcps_modbus_rxbuf[1024];
extern uint8_t loopback_tcps_modbus_txbuf[1024];
// Modbus TCP服务主函数
int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port);
// Modbus TCP转RTU转发主函数
int32_t loopback_tcps_modbus_to485(uint8_t sn, uint8_t *buf, uint16_t port, uint16_t *timeout, RtuRxST *rtu_rx);
// 工具函数
uint16_t tcp_to_rtu(uint8_t *tcp_buf, uint8_t *rtu_buf);
uint16_t rtu_to_tcp(uint8_t *rtu_buf, uint8_t *tcp_buf, uint16_t rtu_len, uint8_t *rx_buf);
#ifdef __cplusplus
}
#endif
#endif
+265
View File
@@ -0,0 +1,265 @@
/*
* slmptcpmaster.c
* 三菱SLMP TCP通信主站
* 轮询方式: slmp_idx 0~9轮转, 奇数索引读(readST), 偶数索引写(writeST)
* 和FINS完全相同的架构
*/
#include "slmptcpmaster.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "rtuMaster.h"
#include "main.h"
#include <string.h>
// 24位小端存储结构体 (SLMP使用)
typedef union {
uint8_t bytes[3];
struct {
uint8_t byte0; // 最低字节
uint8_t byte1; // 中间字节
uint8_t byte2; // 最高字节
};
} slmp_uint24_t;
// SLMP帧联合体 - 支持结构化访问和字节访问
typedef union {
struct __attribute__((__packed__)){
uint16_t header; // 固定0x0050
uint16_t target; // 固定0xff00
uint16_t index; // 固定0x03FF
uint8_t subIdx; // 固定0x00
uint16_t requestSize; // 请求数据长度
uint16_t timeout; // 固定0x0004 (4秒超时)
uint16_t cmd; // 命令: 0x0401=读,0x1401=写
uint16_t subCmd; // 固定0x0000
slmp_uint24_t start; // 起始地址(24位小端)
uint8_t dm; // 固定0xA8 (D寄存器)
uint16_t size; // 读写点数
} frameStruct;
uint8_t frameU8[21]; // 帧头最大长度21字节
} SLMPFrame;
// SLMP全局变量
uint8_t slmp_txbuf[1024]; // 发送缓冲区
uint8_t slmp_rxbuf[1024]; // 接收缓冲区
uint16_t slmp_read_resp_header[7] = { 0xD0, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00 };
uint16_t slmp_idx = 0; // 轮询索引 (0~9)
uint16_t slmp_curr_idx; // 当前处理的索引
uint16_t slmp_addr; // 当前读写地址
uint16_t slmp_size; // 当前读写点数
static SLMPFrame slmp_frame = {
.frameStruct.header = 0x0050,
.frameStruct.target = 0xff00,
.frameStruct.index = 0x03FF,
.frameStruct.subIdx = 0x00,
.frameStruct.timeout = 0x0004,
.frameStruct.subCmd = 0x0000,
.frameStruct.dm = 0xA8
};
// 32位转换为SLMP 24位小端
static slmp_uint24_t u32_to_slmp24(uint32_t val) {
slmp_uint24_t r;
r.byte0 = val & 0xFF;
r.byte1 = (val >> 8) & 0xFF;
r.byte2 = (val >> 16) & 0xFF;
return r;
}
// 处理SLMP接收到的响应
// 返回1: 处理成功,0: 不是有效响应
_Bool slmp_handle_recv(uint16_t size) {
// 只有读响应需要处理数据,写响应不需要存储
// 比对读响应头确认
if (memcmp(slmp_rxbuf, slmp_read_resp_header, 7) != 0) {
return 0; // 不是读响应,直接忽略
}
// 找到上一次发送对应的索引 (发送后索引+1,响应对应前一个)
uint16_t prevIdx;
if (slmp_idx == 0) {
prevIdx = 9;
} else {
prevIdx = slmp_idx - 1;
}
// 读使用奇数索引,验证一下
if (prevIdx % 2 != 1) {
return 0; // 不匹配,丢弃
}
uint16_t thisSize = get_register(readST[prevIdx].size);
// 大小检查 + 边界保护 (最大20个寄存器)
if (thisSize > 20 || thisSize * 2 + 11 != size) {
return 0;
}
// SLMP使用小端序: 低字节在前,高字节在后
for (int i = 0; i < thisSize; i++) {
uint8_t byte_l = slmp_rxbuf[i * 2 + 11];
uint8_t byte_h = slmp_rxbuf[i * 2 + 12];
uint16_t value = ((uint16_t)byte_l) | (((uint16_t)byte_h) << 8);
// 存储位置: 1200 + (索引*20) + 偏移
set_register(1200 + (20 * prevIdx) + i, value);
}
return 1;
}
// 构建SLMP请求,返回发送长度
uint16_t slmp_build_send(void) {
uint16_t sendsize = 0;
uint16_t slave_id;
uint16_t plc_address;
uint16_t size;
// 奇数索引 -> 读操作 (和FINS一致,读使用readST)
if (slmp_idx % 2 == 1) {
slave_id = get_register(readST[slmp_idx].slave_id);
plc_address = get_register(readST[slmp_idx].plc_address);
size = get_register(readST[slmp_idx].size);
if (slave_id < 0x01 || slave_id > 0xF7 || size == 0 || size > 20) {
sendsize = 0;
} else if ((plc_address + size) >= 8000) {
sendsize = 0;
} else {
// 构建读请求帧
slmp_frame.frameStruct.cmd = 0x0401; // 读命令
slmp_frame.frameStruct.requestSize = 0x000C; // 请求长度固定12字节
slmp_frame.frameStruct.start = u32_to_slmp24(plc_address);
slmp_frame.frameStruct.size = size;
sendsize = 21; // 帧头长度固定21字节,无额外数据
// 拷贝到发送缓冲区,带边界检查
for (int i = 0; i < sendsize && i < sizeof(slmp_txbuf); i++) {
slmp_txbuf[i] = slmp_frame.frameU8[i];
}
}
} else {
// 偶数索引 -> 写操作 (和FINS一致,写使用writeST)
slave_id = get_register(writeST[slmp_idx].slave_id);
plc_address = get_register(writeST[slmp_idx].plc_address);
size = get_register(writeST[slmp_idx].size);
if (slave_id < 0x01 || slave_id > 0xF7 || size == 0 || size > 20) {
sendsize = 0;
} else if ((plc_address + size) >= 8000) {
sendsize = 0;
} else {
sendsize = 21 + (size * 2); // 帧头 + 数据
// 检查缓冲区边界,防止越界
if (sendsize > sizeof(slmp_txbuf)) {
return 0;
}
// 构建写请求帧
slmp_frame.frameStruct.cmd = 0x1401; // 写命令
slmp_frame.frameStruct.requestSize = 0x000C + (size * 2);
slmp_frame.frameStruct.start = u32_to_slmp24(plc_address);
slmp_frame.frameStruct.size = size;
// 填充数据,SLMP小端序: 低字节在前
for (int i = 0; i < size; i++) {
uint16_t value = get_register(1000 + (20 * slmp_idx) + i);
slmp_txbuf[i * 2 + 21] = value & 0xFF; // 低字节在前
slmp_txbuf[i * 2 + 22] = (value >> 8) & 0xFF; // 高字节在后
}
// 拷贝帧头到发送缓冲区
for (int i = 0; i < 21 && i < sizeof(slmp_txbuf); i++) {
slmp_txbuf[i] = slmp_frame.frameU8[i];
}
}
}
// 索引轮转 (0~9对应10个通道)
slmp_idx = (slmp_idx + 1) % 10;
return sendsize;
}
// SLMP超时计数,由主循环调用 (和FINS一致)
void update_timeout_slmp(void) {
if (slmpTimegap < 0xffff) {
slmpTimegap++;
}
}
// SLMP TCP客户端主函数 (和FINS相同结构)
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) {
int32_t ret;
uint16_t size = 0, sentsize = 0;
static uint16_t any_port = 50000;
switch (getSn_SR(sn)) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) {
setSn_IR(sn, Sn_IR_CON);
}
// 接收响应
if ((size = getSn_RX_RSR(sn)) > 0) {
// 边界保护,不超过缓冲区大小
if (size > sizeof(slmp_rxbuf)) {
size = sizeof(slmp_rxbuf);
}
ret = recv(sn, slmp_rxbuf, size);
if (ret <= 0) {
return ret;
} else {
comm_count++;
slmp_handle_recv(ret);
slmpTimegap = 900; // 重置超时
}
}
// 超时到了,发送下一个请求
size = 0;
if (slmpTimegap > 1000) {
size = slmp_build_send();
if (size == 0) {
slmpTimegap = 1000;
}
}
// 发送请求
sentsize = 0;
while (size != 0 && sentsize != size) {
ret = send(sn, slmp_txbuf + sentsize, size - sentsize);
if (ret < 0) {
close(sn);
return ret;
}
sentsize += ret;
}
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;
}
break;
case SOCK_CLOSED:
close(sn);
// 端口绕回处理 (和FINS一致)
if (any_port == 0xffff) {
any_port = 50000;
}
if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) {
return ret;
}
break;
default:
break;
}
return 1;
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef SLMPTCPMASTER_H
#define SLMPTCPMASTER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
extern uint8_t slmp_txbuf[1024];
extern uint8_t slmp_rxbuf[1024];
extern uint16_t slmp_idx;
extern uint16_t slmp_addr;
extern uint16_t slmp_size;
extern uint16_t slmpTimegap;
extern uint16_t comm_count;
// 处理接收到的SLMP响应
_Bool slmp_handle_recv(uint16_t size);
// 构建SLMP请求,返回发送长度
uint16_t slmp_build_send(void);
// 超时计数,由主循环调用
void update_timeout_slmp(void);
// SLMP TCP客户端主函数
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport);
#ifdef __cplusplus
}
#endif
#endif