This commit is contained in:
claw committed 2026-04-03 16:45:01 +08:00
1 parent 4e80a72d24
commit 9f3b8cf65a
24 files changed
+619 -3018

No files matched your search

+211
View File
@@ -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;
}