protocol
This commit is contained in:
1 parent
4e80a72d24
commit
9f3b8cf65a
24 files changed
+619
-3018
No files matched your search
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* common.c
|
||||
*
|
||||
* Created on: Apr 3, 2026
|
||||
* Author: 18896
|
||||
*/
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* common.h
|
||||
*
|
||||
* Created on: Apr 3, 2026
|
||||
* Author: 18896
|
||||
*/
|
||||
|
||||
#ifndef W5500_PROTOCOLS_COMMON_H_
|
||||
#define W5500_PROTOCOLS_COMMON_H_
|
||||
|
||||
#define W5500_COMM_LED_TOGGLE_CNT 50
|
||||
#define W5500_COMM_LED_OFF_TIMEMS 2000
|
||||
|
||||
#endif /* W5500_PROTOCOLS_COMMON_H_ */
|
||||
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* @file finsudpmaster.c
|
||||
* @brief 欧姆龙FINS UDP客户端主站实现
|
||||
*
|
||||
* 轮询机制:
|
||||
* - myRtumSTIdx: 0~19 轮转索引,对应 myRtumST[20]
|
||||
* - **奇数索引**: 读操作,从PLC读取数据存储到 + (idx*20) + i
|
||||
* - **偶数索引**: 写操作,从 + (idx*20) + i 读取数据写入PLC
|
||||
* - 每次主循环调用 update_timeout_fins() 递增超时计数
|
||||
* - 超时计数 > 500 时发送下一帧,接收后重置超时为 490
|
||||
*/
|
||||
|
||||
#include "finsudpmaster.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "rtuMaster.h"
|
||||
#include "modbusSlave.h"
|
||||
#include "main.h"
|
||||
#include "common.h"
|
||||
#include <string.h>
|
||||
|
||||
static uint8_t comm_count;
|
||||
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;
|
||||
|
||||
// 读响应固定头,只有匹配这个头才会被处理
|
||||
// 格式: ICV(2) + RSV(2) + GATEWAY(1) + len(2) + cmd(1) + ...
|
||||
uint8_t readrx[14] = { 0xc0, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x40 };
|
||||
|
||||
// 预定义的请求模板
|
||||
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] = { // 写请求模板 (最大20点 = 18 + 20*2 = 58字节)
|
||||
0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x01, 0x02, 0x82, 0x00, 0x64, 0x00, 0x00, 0x02 };
|
||||
|
||||
// ============================================================================
|
||||
// 公共接口
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 处理接收到的FINS读响应
|
||||
* @param size: 接收数据长度
|
||||
* @param local_node: 本地节点号,填充到响应头比较
|
||||
* @return true: 处理成功,false: 不是有效的读响应
|
||||
* @note 只有读响应需要存储数据,写响应不需要处理
|
||||
*/
|
||||
_Bool handleReadRecv(uint16_t size, uint16_t local_node) {
|
||||
// 1. 设置本地节点号,然后比对前14字节确认是读响应
|
||||
readrx[4] = (uint8_t) local_node;
|
||||
for (int i = 0; i < 14; i++) {
|
||||
if (readrx[i] != comm_buf[i]) {
|
||||
return false; // 不匹配,不是读响应
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t pointNum = get_register(myRtumST[prevReadIdx].size);
|
||||
|
||||
// 5. 解析响应数据,存储到寄存器映射
|
||||
// FINS使用大端序: 高字节在前,低字节在后
|
||||
for (int i = 0; i < pointNum; i++) {
|
||||
uint8_t byte_h = comm_buf[i * 2 + 14];
|
||||
uint8_t byte_l = comm_buf[i * 2 + 15];
|
||||
uint16_t value = ((uint16_t) byte_h << 8) | byte_l;
|
||||
// 存储地址: + (索引*20) + 偏移
|
||||
set_register(master.localStartIdx + (20 * prevReadIdx) + i, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 构建FINS请求帧
|
||||
* @param local_node: 本地节点号
|
||||
* @return 发送长度,0表示不发送(参数非法)
|
||||
* @note 奇数索引 -> 读操作,偶数索引 -> 写操作
|
||||
*/
|
||||
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 pointNum = get_register(myRtumST[myRtumSTIdx].size);
|
||||
|
||||
// ======================
|
||||
// 无效 → 索引+1 → 下一个
|
||||
// ======================
|
||||
if (slave_id == 0 || slave_id >= 127 || pointNum == 0 || pointNum > 20) {
|
||||
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 奇数 = 读
|
||||
// ======================
|
||||
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) pointNum;
|
||||
sendsize = 18;
|
||||
memcpy(comm_buf, send_read, sendsize);
|
||||
prevReadIdx = myRtumSTIdx;
|
||||
}
|
||||
// ======================
|
||||
// 偶数 = 写
|
||||
// ======================
|
||||
else {
|
||||
sendsize = 18 + pointNum * 2;
|
||||
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) pointNum;
|
||||
|
||||
for (int i = 0; i < pointNum; i++) {
|
||||
uint16_t value = get_register(master.localStartIdx + 20 * myRtumSTIdx + i);
|
||||
write_send[18 + i * 2] = (value >> 8) & 0xFF;
|
||||
write_send[19 + i * 2] = value & 0xFF;
|
||||
}
|
||||
memcpy(comm_buf, write_send, sendsize);
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 有效发送完成 → 下一个
|
||||
// ======================
|
||||
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
|
||||
return sendsize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 超时计数,由主循环每次调用递增
|
||||
*/
|
||||
void update_timeout_fins(void) {
|
||||
if (timeout < 0xffff) {
|
||||
timeout++;
|
||||
}
|
||||
if (commled_off_timeout < 0xffff) {
|
||||
commled_off_timeout++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FINS UDP客户端主函数,W5500socket主循环处理
|
||||
* @param sn: W5500 socket编号
|
||||
* @param destip: 目标IP地址
|
||||
* @param destport: 目标端口
|
||||
* @param local_node: 本地节点号
|
||||
* @return 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node) {
|
||||
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;
|
||||
|
||||
switch (getSn_SR(sn)) {
|
||||
case SOCK_UDP:
|
||||
// 接收
|
||||
if ((size = getSn_RX_RSR(sn)) > 0) {
|
||||
if (size > sizeof(comm_buf))
|
||||
size = sizeof(comm_buf);
|
||||
ret = recvfrom(sn, comm_buf, size, destip, (uint16_t*) &destport);
|
||||
if (ret > 0) {
|
||||
commled_off_timeout = 0;
|
||||
comm_count++;
|
||||
handleReadRecv(ret, local_node);
|
||||
timeout = 490;
|
||||
}
|
||||
}
|
||||
|
||||
// 超时触发发送
|
||||
size = 0;
|
||||
if (timeout > 500) {
|
||||
size = handleSend(local_node);
|
||||
if (size == 0) {
|
||||
// 无效通道,快速跳过
|
||||
timeout = 499;
|
||||
} else {
|
||||
// 有效发送,重置超时
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 发送
|
||||
sentsize = 0;
|
||||
while (size != 0 && sentsize < size) {
|
||||
ret = sendto(sn, comm_buf + sentsize, size - sentsize, destip, destport);
|
||||
if (ret < 0)
|
||||
break;
|
||||
sentsize += ret;
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_CLOSED:
|
||||
if (any_port == 0xFFFF)
|
||||
any_port = 50000;
|
||||
socket(sn, Sn_MR_UDP, any_port++, 0x00);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @file finsudpmaster.h
|
||||
* @brief 欧姆龙FINS UDP客户端主站头文件
|
||||
*/
|
||||
#ifndef FINSUDPMASTER_H
|
||||
#define FINSUDPMASTER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// 接口函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 处理接收到的FINS读响应
|
||||
* @param size: 接收数据长度
|
||||
* @param local_node: 本地节点号
|
||||
* @return true: 处理成功,false: 不是有效的读响应
|
||||
*/
|
||||
_Bool handleReadRecv(uint16_t size, uint16_t local_node);
|
||||
|
||||
/**
|
||||
* @brief 构建FINS请求帧
|
||||
* @param local_node: 本地节点号
|
||||
* @return 发送长度,0表示不发送(参数非法)
|
||||
*/
|
||||
uint16_t handleSend(uint16_t local_node);
|
||||
|
||||
/**
|
||||
* @brief 超时计数,由主循环每次调用递增
|
||||
*/
|
||||
void update_timeout_fins(void);
|
||||
|
||||
/**
|
||||
* @brief FINS UDP客户端主函数,W5500socket主循环处理
|
||||
* @param sn: socket编号
|
||||
* @param destip: 目标IP地址
|
||||
* @param destport: 目标端口
|
||||
* @param local_node: 本地节点号
|
||||
* @return 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* @file modbustcpslave.c
|
||||
* @brief Modbus TCP从站服务 + Modbus TCP转RTU转发
|
||||
*/
|
||||
|
||||
#include "modbustcpslave.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "modbusSlave.h"
|
||||
#include "common.h"
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
uint8_t loopback_tcps_modbus_rxbuf[1024];
|
||||
uint8_t loopback_tcps_modbus_txbuf[1024];
|
||||
|
||||
static uint16_t commled_off_timeout = 0;
|
||||
static uint8_t comm_count;
|
||||
|
||||
void update_modbusTCP_timeout() {
|
||||
if (commled_off_timeout < 0xffff) {
|
||||
commled_off_timeout++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Modbus TCP服务主函数,W5500socket主循环处理
|
||||
* @param sn: socket编号
|
||||
* @param port: 监听端口 (通常502)
|
||||
* @return 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) {
|
||||
comm_count++;
|
||||
commled_off_timeout = 0;
|
||||
if (size > DATA_BUF_SIZE) {
|
||||
size = DATA_BUF_SIZE;
|
||||
}
|
||||
ret = recv(sn, loopback_tcps_modbus_rxbuf, size);
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 处理Modbus TCP请求
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @file modbustcpslave.h
|
||||
* @brief Modbus TCP从站服务头文件
|
||||
*/
|
||||
#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
|
||||
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// 接口函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Modbus TCP服务主函数,W5500socket主循环处理
|
||||
* @param sn: socket编号
|
||||
* @param port: 监听端口 (通常502)
|
||||
* @return 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port);
|
||||
void update_modbusTCP_timeout();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* @file s7tcpmaster.c
|
||||
* @brief 西门子S7Comm TCP客户端主站实现
|
||||
*
|
||||
* 轮询机制: 和SLMP/FINS相同,使用myRtumST[20]
|
||||
* - s7_idx: 0~19 轮转索引,对应 myRtumST[20]
|
||||
* - **奇数索引**: 读操作,从PLC读取数据存储到 1000 + (idx*20) + i
|
||||
* - **偶数索引**: 写操作,从 1000 + (idx*20) + i 读取数据写入PLC
|
||||
* - 需要先建立TCP连接,然后完成COTP连接协商和S7Comm参数协商
|
||||
* - 协商完成后进入正常轮询读写
|
||||
*/
|
||||
|
||||
#include "s7tcpmaster.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "rtuMaster.h"
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
|
||||
static uint8_t buf[1024];
|
||||
|
||||
int32_t loopback_tcpc_s7(uint8_t sn,uint8_t *destip, uint16_t destport) {
|
||||
int32_t ret; // return value for SOCK_ERRORs
|
||||
uint16_t size = 0, sentsize = 0;
|
||||
|
||||
// Destination (TCP Server) IP info (will be connected)
|
||||
// >> loopback_tcpc() function parameter
|
||||
// >> Ex)
|
||||
// uint8_t destip[4] = {192, 168, 0, 214};
|
||||
// uint16_t destport = 5000;
|
||||
|
||||
// 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)
|
||||
switch (getSn_SR(sn)) {
|
||||
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_
|
||||
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 ((size = getSn_RX_RSR(sn)) > 0) { // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
|
||||
if (size > 1024) {
|
||||
size = 1024; // DATA_BUF_SIZE means user defined buffer size (array)
|
||||
}
|
||||
ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
|
||||
|
||||
if (ret <= 0) {
|
||||
return ret; // If the received data length <= 0, receive failed and process end
|
||||
}
|
||||
size = (uint16_t) ret;
|
||||
sentsize = 0;
|
||||
|
||||
// Data sentsize control
|
||||
while (size != sentsize) {
|
||||
ret = send(sn, buf + sentsize, size - 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.
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
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: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:
|
||||
close(sn);
|
||||
if ((ret = socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn) {
|
||||
if (any_port == 0xffff) {
|
||||
any_port = 50000;
|
||||
}
|
||||
return ret; // TCP socket open with 'any_port' port number
|
||||
}
|
||||
#ifdef _LOOPBACK_DEBUG_
|
||||
//printf("%d:TCP client loopback start\r\n",sn);
|
||||
//printf("%d:Socket opened\r\n",sn);
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @file s7tcpmaster.h
|
||||
* @brief 西门子S7Comm TCP客户端主站头文件
|
||||
*/
|
||||
#ifndef S7TCPMASTER_H
|
||||
#define S7TCPMASTER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *destip, uint16_t destport);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file slmptcpmaster.h
|
||||
* @brief 三菱SLMP协议TCP客户端主站头文件
|
||||
*/
|
||||
#ifndef SLMPTCPMASTER_H
|
||||
#define SLMPTCPMASTER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// 接口函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief 处理接收到的SLMP响应
|
||||
* @param size: 接收数据长度
|
||||
* @return true: 处理成功,false: 不是有效的读响应
|
||||
*/
|
||||
_Bool slmp_handle_recv(uint16_t size);
|
||||
|
||||
/**
|
||||
* @brief 构建SLMP请求帧
|
||||
* @return 发送长度,0表示不发送(参数非法)
|
||||
*/
|
||||
uint16_t slmp_build_send(void);
|
||||
|
||||
/**
|
||||
* @brief 超时计数,由主循环每次调用递增
|
||||
*/
|
||||
void update_timeout_slmp(void);
|
||||
|
||||
/**
|
||||
* @brief SLMP TCP客户端主函数,W5500socket主循环处理
|
||||
* @param sn: socket编号
|
||||
* @param destip: 目标IP地址
|
||||
* @param destport: 目标端口
|
||||
* @return 0/正表示正常,负表示错误
|
||||
*/
|
||||
int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user