CH9121搜素配置协议
1 // 定义了与网络CH9121通信的基本数据结构,和配置结构,参数等 2 3 #ifndef __MODULECONFIG_H__ 4 #define __MODULECONFIG_H__ 5 6 //变量类型定义 7 #define UCHAR unsigned char 8 #define USHORT unsigned short 9 #define ULONG unsigned long 10 11 #define NET_MODULE_DATA_LENGTH 255 //与CH9121通信时数据区的最大长度 12 13 #define MODULE_CFG_LEN_NEW ( 74 + 65*2 ) 14 15 //通信命令码 16 #define NET_MODULE_CMD_SET 0X01 //配置网络中的CH9121 17 #define NET_MODULE_CMD_GET 0X02 //获取某个CH9121的配置 18 #define NET_MODULE_CMD_RESET 0X03 //获取某个CH9121的配置 19 #define NET_MODULE_CMD_SEARCH 0X04 //搜索网络中的CH9121 20 21 #define NET_MODULE_CMD_SET_BAUD 0X09 //配置网络中的模块 22 23 //应答命令码 24 #define NET_MODULE_ACK_SET 0X81 //回应配置命令码 25 #define NET_MODULE_ACK_GET 0X82 //回应获取命令码 26 #define NET_MODULE_ACK_RESET 0X83 //获取某个CH9121的配置 27 #define NET_MODULE_ACK_SEARCH 0X84 //回应所搜命令码 28 29 #define NET_MODULE_ACK_SET_BAUD 0X89 //配置网络中的模块 30 31 //校验和错误时的应答命令码 32 #define NET_MODULE_NAK_SEARCH 0xC0 //搜索校验错 33 #define NET_MODULE_NAK_SET 0XC1 //配置校验错 34 #define NET_MODULE_NAK_GET 0XC2 //获取校验错 35 36 #define CH9121_CFG_FLAG "CH9121_CFG_FLAG" //用来标识通信 37 38 39 //网络通信结构体 40 typedef struct NET_COMM { 41 unsigned char flag[16]; //通信标识,因为都是用广播方式进行通信的,所以这里加一个固定值 42 unsigned char cmd; //命令头 43 unsigned char id[6]; //CH9121MAC地址 44 unsigned char pcid[6]; //PC的MAC地址 45 unsigned char len; //数据区长度 46 unsigned char data[NET_MODULE_DATA_LENGTH]; //数据区缓冲区 47 }net_comm,*pnet_comm; 48 49 //CH9121标识 50 #define NET_MODULE_TYPE_TCP_S 0X00 //CH9121作为TCP SERVER 51 #define NET_MODULE_TYPE_TCP_C 0X01 //CH9121作为TCP CLIENT 52 #define NET_MODULE_TYPE_UDP_S 0X02 //CH9121作为UDP SERVER 53 #define NET_MODULE_TYPE_UDP_C 0X03 //CH9121作为UDP CLIENT 54 55 //校验位标识 56 #define NET_MODULE_VERIFY_NULL 0X04 //无校验 57 #define NET_MODULE_VERIFY_ODD 0X00 //奇校验 58 #define NET_MODULE_VERIFY_EVEN 0X01 //偶校验 59 #define NET_MODULE_VERIFY_MARK 0X02 //mark校验 60 #define NET_MODULE_VERIFY_SPACE 0X03 //space校验 61 62 63 /* 设备硬件信息结构体定义 */ 64 #define CH9121_DEV_TYPE 0x30 65 #define CH9121_DEV_AUX_TYPE 0x01 66 #define CH9121_DEV_HARDWARE_VER 0x02 67 #define CH9121_DEV_SOFTWARE_VER 0x02 68 69 /* 子设备配置信息结构体定义 */ 70 71 #define CH9121_DevType 0x21 72 #define CH9121_AuxDevType 0x21 73 #define CH9121_DevbIndex 0x01 74 #define CH9121_DevHardwareVer 0x02 75 #define CH9121_SoftwareVer 0x02 76 #define CH9121_VER (((CH9121_DevHardwareVer<<4) & 0xF0) | (CH9121_SoftwareVer & 0x0f)) 77 78 79 80 __packed typedef struct _DEVICEHW_CONFIG 81 { 82 UCHAR bDevType; /* 设备类型,只读 */ 83 UCHAR bAuxDevType; /* 设备子类型, 只读*/ 84 UCHAR bIndex; /* 设备序号, 只读*/ 85 UCHAR bDevHardwareVer; /* 设备硬件版本号,只读 */ 86 UCHAR bDevSoftwareVer; /* 设备软件版本号,只读 */ 87 UCHAR szModulename[21]; /* 用户名同CH9121名*/ 88 UCHAR bDevMAC[ 6 ]; /* CH9121网络MAC地址 */ 89 UCHAR bDevIP[ 4 ]; /* CH9121IP地址*/ 90 UCHAR bDevGWIP[ 4 ]; /* CH9121网关IP */ 91 UCHAR bDevIPMask[ 4 ]; /* CH9121子网掩码 */ 92 UCHAR bDhcpEnable; /* DHCP 使能,是否启用DHCP,1:启用,0:不启用*/ 93 USHORT breserved1; /* 预留暂未启用 */ 94 UCHAR breserved2[8]; /* 预留暂未启用*/ 95 UCHAR breserved3; /* 预留暂未启用*/ 96 UCHAR breserved4[8]; /* 预留暂未启用*/ 97 UCHAR breserved5; /* 预留暂未启用*/ 98 UCHAR bComcfgEn; /* 串口协商配置标志 1:启用 0:禁用*/ 99 UCHAR breserved6[8]; /* 预留暂未启用*/ 100 }DeviceHWConfigS,*pDeviceHWConfigS; 101 102 __packed typedef struct _DEVICEPORT_CONFIG 103 { 104 UCHAR bIndex; /* 子设备序号,只读 */ 105 UCHAR bPortEn; /* 端口启用标志 1:启用 ;0:不启用 */ 106 UCHAR bNetMode; /* 网络工作模式: 0: TCP SERVER;1: TCP CLENT; 2: UDP SERVER 3:UDP CLIENT; */ 107 UCHAR bRandSportFlag; /* TCP 客户端模式下随机本地端口号,1:随机 0: 不随机*/ 108 USHORT wNetPort; /* 本地端口号 */ 109 UCHAR bDesIP[ 4 ]; /* 目的IP地址 */ 110 USHORT wDesPort; /* 目的端口号 */ 111 ULONG dBaudRate; /* 串口波特率: 300---921600bps */ 112 UCHAR bDataSize; /* 串口数据位: 5---8位 */ 113 UCHAR bStopBits; /* 串口停止位: 0表示1个停止位; 1表示1.5个停止位; 2表示2个停止位 */ 114 UCHAR bParity; /* 串口校验位: 4表示无校验,0表示奇校验; 1表示偶校验; 2表示标志位(MARK,置1); 3表示空白位(SPACE,清0); */ 115 UCHAR bPHYChangeHandle; /* PHY断开,Socket动作,1:关闭Socket 0:不动作*/ 116 ULONG dRxPktlength; /* 串口RX数据打包长度,最大1024 */ 117 ULONG dRxPktTimeout; /* 串口RX数据打包转发的最大等待时间,单位为: 10ms,0则表示关闭超时功能 */ 118 UCHAR bResv; /* 预留未启用*/ 119 UCHAR bResetCtrl; /* 串口复位操作: 0表示不清空串口数据缓冲区; 1表示连接时清空串口数据缓冲区 */ 120 UCHAR bDNSFlag; /* 域名功能启用标志,1:启用 0:不启用*/ 121 UCHAR szDomainname[20]; /* TCP客户端模式下,目的地址,域名*/ 122 UCHAR breserved[14]; /* 保留*/ 123 }DevicePortConfigS,*pDevicePortConfigS; 124 125 126 __packed typedef struct _NET_DEVICE_CONFIG 127 { 128 DeviceHWConfigS HWCfg; /*网络参数结构体*/ 129 DevicePortConfigS PortCfg[2]; /*透传通道参数*/ 130 }NetDeviceConfigS,*pNetDeviceConfigS; 131 132 133 134 #endif
1.通讯方式
CH9121 通过 UDP 广播方式进行网络配置功能。
CH9121 广播本地接收端口:50000,上位机广播本地接收端口:60000。
2.通讯结构体
1 CH9121 和上位机通讯数据包格式为: 2 typedef struct NET_COMM { 3 unsigned char flag[16]; //通信标识,"CH9121_CFG_FLAG" 4 unsigned char cmd; //命令头 5 unsigned char id[6]; //CH9121MAC 地址 6 unsigned char pcid[6]; //PC 的 MAC 地址 7 unsigned char len; //数据区长度 8 unsigned char data[NET_MODULE_DATA_LENGTH]; //数据区缓冲区 9 }net_comm,*pnet_comm;
10 其中在配置和获取配置阶段 data[NET_MODULE_DATA_LENGTH]结构如下: 11 __packed typedef struct _NET_DEVICE_CONFIG 12 { 13 DeviceHWConfigS HWCfg; /*网络参数结构体*/ 14 DevicePortConfigS PortCfg[2]; /*透传通道参数*/ 15 }NetDeviceConfigS,*pNetDeviceConfigS; 16 其中 PortCfg[1]为默认开启的透传通道,并且支持手册全部功能,建议使用单通道的时候默认使用此透传通道。 18 PortCfg[0]为辅助端口,仅支持基本透传功能。
CH9121上位机配置功能主要包括:①搜索,②获取配置,③配置网络参数,④恢复出厂设置。
下面结合通讯过程中的数据包解析设置流程。(wireshark抓包可以抓取到数据包)
-->PC下发的搜索包:
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //16字节固定通信标识
04 //1字节命令头
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
<--9121回复的应答包:
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //16字节固定通信标识
84 //1字节命令头
38 3b 26 44 de a5 //6字节9121自身MAC地址
00 00 00 00 00 00
0c
c0 a8 01 02 //6字节9121自身IP地址
43 48 39 31 32 31 //设备名
2000
29 //固件版本
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
-->PC下发的获取配置请求包
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //16字节固定头
02 //1字节命令头
38 3b 26 44 de a5 //6字节9121自身MAC地址
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
<--9121的应答包:
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 // 16字节固定头
82 //1字节命令头
38 3b 26 44 de a5 //6字节9121自身MAC地址
00 00 00 00 00 00
cc
21 //设备类型
21 //设备子类型
01 //设备序号
02 //设备硬件版本号
06 //设备软件版本号
43 48 39 31 32 31 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 //设备名
38 3b 26 44 de a5 //9121_mac地址
c0 a8 01 02 //9121_ip地址
c0 a8 01 01 //9121_网关
ff ff ff 00 //9121_掩码
00 //DHCP使能,是否启用DHCP,1为启用,0为关闭
50 00 //WEB网页地址
00 00 00 00 00 00 00 00 //用户名同模块名
00 //密码使能 1:使能 0: 禁用
00 00 00 00 00 00 00 00 //密码*/
ff //固件升级标志,1:升级 0:不升级
00 //串口协商进入配置模式使能,1:使能 0:不使能
00 00 00 00 00 00 00 00 //保留
00 //端口序号
00 //端口启用标志 1:启用后 ;0:不启用
02 //网络工作模式: 0: TCP SERVER;1: TCP CLENT; 2: UDP SERVER 3:UDP CLIENT;
01 //TCP 客户端模式下随即本地端口号,1:随机 0: 不随机
b8 0b //网络通讯端口号
c0 a8 01 64 //目的IP地址
d0 07 //工作于TCP Server模式时,允许外部连接的端口号
80 25 00 00 //串口波特率: 300---921600bps
08 //串口数据位: 5---8位
01 //串口停止位: 1表示1个停止位; 2表示2个停止位
04 //串口校验位: 0表示奇校验; 1表示偶校验; 2表示标志位(MARK,置1); 3表示空白位(SPACE,清0);
01 //PHY断开,Socket动作,1:关闭Socket 2、不动作
00 04 00 00 //串口RX数据打包长度,最大1024
00 00 00 00 //串口RX数据打包转发的最大等待时间,单位为: 10ms,0则表示关闭超时功能
00 //工作于TCP CLIENT时,连接TCP SERVER的最大重试次数*/
00 //串口复位操作: 0表示不清空串口数据缓冲区; 1表示连接时清空串口数据缓冲区
00 //域名功能启用标志,1:启用 2:不启用
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 //域名
00 00 00 00 //DNS 主机
00 00 //DNS 端口
00 00 00 00 00 00 00 00 //保留
01 /**************端口2的相关信息参考上面端口1******************/
01
00
01
64 09
c0 a8 01 64
e8 03
00 e1 00 00
08
01
04
01
00 04 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 //最后 51个00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
-->PC下发的获取配置请求包
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //固定头
01 //命令头
38 3b 26 44 de a5 //9121自身的MAC地址
00 e0 4c 36 35 5a //对端的MAC地址
cc //数据区长度
21 /**************下面数据具体含义和上面类似,参考上面******************/
21
01
02
06
43 48 39 31 32 31 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00
38 3b 26 44 de a5
c0 a8 01 1e
c0 a8 01 01
ff ff ff 00
00
50 00
00 00 00 00 00 00 00 00
00
00 00 00 00 00 00 00 00
ff
00
00 00 00 00 00 00 00 00
00 //端口1
01
02
01
b8 0b
c0 a8 01 64
d0 07
80 25 00 00
08
01
04
01
00 04 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
01 //端口2
01
01
01
d0 07
c0 a8 01 6e
e8 03
00 c2 01 00
08
01
04
01
00 04 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
<--9121回复的应答包
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //固定头
81 //命令头
38 3b 26 44 de a5 //9121自身的MAC地址
00 e0 4c 36 35 5a //对端的MAC地址
00 /**************下面数据具体含义和上面类似,参考上面******************/
21
21
01
02
06
43 48 39 31 32 31 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00
38 3b 26 44 de a5
c0 a8 01 1e
c0 a8 01 01
ff ff ff 00
00
50 00
00 00 00 00 00 00 00 00
00
00 00 00 00 00 00 00 00
ff
00
00 00 00 00 00 00 00 00
00 //端口1信息
01
02
01
b8 0b
c0 a8 01 64
d0 07
80 25 00 00
08
01
04
01
00 04 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
01 //端口2
01
01
01
d0 07
c0 a8 01 6e
e8 03
00 c2 01 00
08
01
04
01
00 04 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
-->PC机下发的恢复出厂请求包:
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //固定头
03 //命令头
38 3b 26 44 de a5 //9121自身的MAC地址
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
<--9121应答包
43 48 39 31 32 31 5f 43 46 47 5f 46 4c 41 47 00 //固定头
83 //命令头
38 3b 26 44 de a5 //9121自身的MAC地址
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
上位机搜索软件及协议demo:https://files.cnblogs.com/files/blogs/808422/%E6%90%9C%E7%B4%A2%E8%BD%AF%E4%BB%B6DEMO.zip?t=1704937203&download=true
标签:协议,00,0000,MODULE,网口,CH9121,NET,define From: https://www.cnblogs.com/wchwchlq/p/17954976