protocol
This commit is contained in:
1 parent
4e80a72d24
commit
9f3b8cf65a
24 files changed
+619
-3018
No files matched your search
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* @file slmptcpmaster.c
|
||||
* @brief 三菱SLMP协议TCP客户端主站实现
|
||||
*
|
||||
* 轮询机制 (和欧姆龙FINS完全一致):
|
||||
* - myRtumSTIdx: 0~9 轮转索引,对应 readST[10] / writeST[10]
|
||||
* - **奇数索引**: 读操作,从PLC读取数据存储到 + (idx*20) + i
|
||||
* - **偶数索引**: 写操作,从 + (idx*20) + i 读取数据写入PLC
|
||||
* - 每次主循环调用 update_timeout_slmp() 递增超时计数
|
||||
* - 超时计数 > 1000 时发送下一帧,接收后重置超时为 900
|
||||
*/
|
||||
|
||||
#include "slmptcpmaster.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "rtuMaster.h"
|
||||
#include "modbusSlave.h"
|
||||
#include "main.h"
|
||||
#include "common.h"
|
||||
#include <string.h>
|
||||
|
||||
// ============================================================================
|
||||
// 结构体定义
|
||||
// ============================================================================
|
||||
|
||||
// 24位地址,SLMP使用小端序存储 (低字节在前)
|
||||
typedef union {
|
||||
uint8_t bytes[3]; // 按字节访问
|
||||
struct {
|
||||
uint8_t byte0; // 最低字节 (bit 0~7)
|
||||
uint8_t byte1; // 中间字节 (bit 8~15)
|
||||
uint8_t byte2; // 最高字节 (bit 16~23)
|
||||
};
|
||||
} 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;
|
||||
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
uint16_t slmp_read_resp_header[7] = { 0xD0, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00 };
|
||||
|
||||
static uint8_t myRtumSTIdx = 0; // 轮询索引 (0~19)
|
||||
static uint8_t prevReadIdx = 0;
|
||||
static uint8_t comm_buf[1024]; // FINS收发缓冲区 (最大1024字节)
|
||||
static uint16_t timeout = 0; // 超时计数,由主循环递增
|
||||
static uint16_t commled_off_timeout = 0;
|
||||
static uint8_t comm_count;
|
||||
|
||||
// SLMP帧静态初始化,固定值填充
|
||||
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 };
|
||||
|
||||
// ============================================================================
|
||||
// 内部工具函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 32位地址转SLMP 24位小端格式
|
||||
* @param val: 32位地址
|
||||
* @return slmp_uint24_t: 转换后的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;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 公共接口
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 处理接收到的SLMP响应
|
||||
* @param size: 接收数据长度
|
||||
* @return true: 处理成功,false: 不是有效的读响应
|
||||
* @note 只有读响应需要存储数据,写响应不需要处理
|
||||
*/
|
||||
_Bool handleReadRecvSLMP(uint16_t size) {
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
if (comm_buf[i] != slmp_read_resp_header[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 4. 获取参数,检查长度合法性
|
||||
uint16_t pointNum = get_register(myRtumST[prevReadIdx].size);
|
||||
if (size != pointNum * 2 + 11) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 5. 解析响应数据,存储到寄存器映射
|
||||
// SLMP数据使用小端序: 低字节在前,高字节在后
|
||||
for (int i = 0; i < pointNum; i++) {
|
||||
uint8_t byte_l = comm_buf[i * 2 + 11];
|
||||
uint8_t byte_h = comm_buf[i * 2 + 12];
|
||||
uint16_t value = ((uint16_t) byte_l) | (((uint16_t) byte_h) << 8);
|
||||
// 存储地址: + (索引*20) + 偏移 (和FINS一致)
|
||||
set_register(master.localStartIdx + (20 * prevReadIdx) + i, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 构建SLMP请求帧
|
||||
* @return 发送长度,0表示不发送(参数非法)
|
||||
* @note 和FINS完全一致: 使用myRtumST[20],奇数索引读,偶数索引写
|
||||
* 读: 存储到 + (idx*20) + i
|
||||
* 写: 从 + (idx*20) + i 读取
|
||||
*/
|
||||
uint16_t handleSendSLMP(void) {
|
||||
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 pointNum = get_register(myRtumST[myRtumSTIdx].size);
|
||||
|
||||
// 参数合法性检查
|
||||
if (slave_id < 0x01 || slave_id > 0xF7 || pointNum == 0 || pointNum > 20 || (plc_address + pointNum) >= 8000) {
|
||||
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ========== 奇数索引: 读操作 ==========
|
||||
if (myRtumSTIdx % 2 == 1) {
|
||||
// 构建读请求帧
|
||||
slmp_frame.frameStruct.cmd = 0x0401; // 读命令码
|
||||
slmp_frame.frameStruct.requestSize = 0x000C; // 请求长度固定12字节
|
||||
slmp_frame.frameStruct.start = u32_to_slmp24(plc_address);
|
||||
slmp_frame.frameStruct.size = pointNum;
|
||||
sendsize = 21; // 读请求只有帧头,固定21字节
|
||||
|
||||
// 拷贝帧头到发送缓冲区,带边界检查
|
||||
for (int i = 0; i < sendsize && i < sizeof(comm_buf); i++) {
|
||||
comm_buf[i] = slmp_frame.frameU8[i];
|
||||
}
|
||||
|
||||
prevReadIdx = myRtumSTIdx;
|
||||
}
|
||||
// ========== 偶数索引: 写操作 ==========
|
||||
else {
|
||||
sendsize = 21 + (pointNum * 2); // 帧头21字节 + 数据2字节*点数
|
||||
if (sendsize > sizeof(comm_buf)) {
|
||||
return 0; // 超出缓冲区,安全保护
|
||||
}
|
||||
|
||||
// 构建写请求帧
|
||||
slmp_frame.frameStruct.cmd = 0x1401; // 写命令码
|
||||
slmp_frame.frameStruct.requestSize = 0x000C + (pointNum * 2);
|
||||
slmp_frame.frameStruct.start = u32_to_slmp24(plc_address);
|
||||
slmp_frame.frameStruct.size = pointNum;
|
||||
|
||||
// 填充数据: 从寄存器映射读取,SLMP小端序,低字节在前
|
||||
for (int i = 0; i < pointNum; i++) {
|
||||
uint16_t value = get_register(master.localStartIdx + (20 * myRtumSTIdx) + i);
|
||||
comm_buf[i * 2 + 21] = value & 0xFF; // 低字节在前
|
||||
comm_buf[i * 2 + 22] = (value >> 8) & 0xFF; // 高字节在后
|
||||
}
|
||||
|
||||
// 拷贝帧头到发送缓冲区
|
||||
for (int i = 0; i < 21 && i < sizeof(comm_buf); i++) {
|
||||
comm_buf[i] = slmp_frame.frameU8[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 索引轮转,下次处理下一个通道 (和FINS完全一样)
|
||||
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
|
||||
return sendsize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 超时计数,由主循环每次调用递增
|
||||
* @note 和FINS完全一致
|
||||
*/
|
||||
void update_timeout_slmp(void) {
|
||||
if (timeout < 0xffff) {
|
||||
timeout++;
|
||||
}
|
||||
if (commled_off_timeout < 0xffff) {
|
||||
commled_off_timeout++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SLMP TCP客户端主函数,由顶层loopback_comm调用
|
||||
* @param sn: W5500 socket编号
|
||||
* @param destip: 目标IP地址
|
||||
* @param destport: 目标端口
|
||||
* @return int32_t: 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) {
|
||||
if (comm_count > W5500_COMM_LED_TOGGLE_CNT) {
|
||||
HAL_GPIO_TogglePin(comm_GPIO_Port, comm_Pin);
|
||||
comm_count = 0;
|
||||
}
|
||||
if (commled_off_timeout > W5500_COMM_LED_OFF_TIMEMS) {
|
||||
HAL_GPIO_WritePin(comm_GPIO_Port, comm_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
int32_t ret;
|
||||
uint16_t size = 0, sentsize = 0;
|
||||
static uint16_t any_port = 50000; // 本地端口,自动递增
|
||||
|
||||
uint8_t sr_status = getSn_SR(sn);
|
||||
switch (sr_status) {
|
||||
// ========== 连接已建立: 收发数据 ==========
|
||||
case SOCK_ESTABLISHED:
|
||||
if (getSn_IR(sn) & Sn_IR_CON) {
|
||||
setSn_IR(sn, Sn_IR_CON); // 清连接中断标志
|
||||
}
|
||||
|
||||
// 1. 接收响应
|
||||
if ((size = getSn_RX_RSR(sn)) > 0) {
|
||||
// 边界保护,不超过缓冲区
|
||||
if (size > sizeof(comm_buf)) {
|
||||
size = sizeof(comm_buf);
|
||||
}
|
||||
ret = recv(sn, comm_buf, size);
|
||||
if (ret > 0) {
|
||||
commled_off_timeout = 0;
|
||||
comm_count++;
|
||||
handleReadRecvSLMP(size);
|
||||
timeout = 490;
|
||||
}
|
||||
}
|
||||
|
||||
// 超时触发发送
|
||||
size = 0;
|
||||
if (timeout > 500) {
|
||||
size = handleSendSLMP();
|
||||
if (size == 0) {
|
||||
// 无效通道,快速跳过
|
||||
timeout = 499;
|
||||
} else {
|
||||
// 有效发送,重置超时
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 发送请求(分片发送,直到发完)
|
||||
sentsize = 0;
|
||||
while (size != 0 && sentsize != size) {
|
||||
ret = send(sn, comm_buf + 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;
|
||||
|
||||
// ========== socket未打开: 新建socket ==========
|
||||
case SOCK_CLOSED:
|
||||
close(sn);
|
||||
// 端口绕回,超过最大值后重置
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user