51 lines
1.4 KiB
C
51 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include "loopback.h"
|
|
#include "protocols/modbustcpslave.h"
|
|
#include "protocols/finsudpmaster.h"
|
|
#include "protocols/slmptcpmaster.h"
|
|
#include "rtuMaster.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
|
|
|
|
// 全局共享变量定义
|
|
uint16_t client_wait_timeout = 0;
|
|
uint16_t comm_count = 0;
|
|
uint16_t slmpTimegap = 0;
|
|
uint16_t fins_timeout = 0;
|
|
|
|
// 通用字节序转换工具函数
|
|
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); // 最低字节
|
|
}
|
|
|
|
// 顶层统一入口,根据PLC类型选择协议
|
|
void loopback_comm(uint8_t *ip, uint16_t port, uint8_t node) {
|
|
if (master.plc_type == 0) {
|
|
// 0 -> SLMP TCP (三菱)
|
|
loopback_tcpc_slmp(1, ip, port);
|
|
} else if (master.plc_type == 1) {
|
|
// 1 -> OMR FINS UDP (欧姆龙)
|
|
loopback_udpc_omrfins(1, ip, port, node);
|
|
}
|
|
// 始终启动Modbus TCP服务在端口502
|
|
loopback_tcps_modbus(0, 502);
|
|
}
|
|
|
|
#endif
|