CH9120网口配置协商及说明
目录
CH9120上位机搜索配置协议
1 #define MAX_DEVICE_CNT 2000 //最大搜索设备数 2 #define NET_MODULE_DATA_LENGTH 255 //与模块通信时数据区的最大长度 3 #define MODULE_DEFAULT_CONFIG_PORT 50000 //默认的配置端口 4 #define LOCAL_PORT 60000 //默认本地配置端口 5 6 //通信命令码 7 #define NET_MODULE_CMD_SET 0X01 //配置网络中的模块 8 #define NET_MODULE_CMD_GET 0X02 //获取某个模块的配置 9 #define NET_MODULE_CMD_RESET 0x03 //复位模块 10 #define NET_MODULE_CMD_SEARCH 0X04 //搜索网络中的模块 11 12 //应答命令码 13 #define NET_MODULE_ACK_SET 0X81 //回应配置命令码 14 #define NET_MODULE_ACK_GET 0X82 //回应获取命令码 15 #define NET_MODULE_ACK_RESEST 0X83 //回应复位命令码 16 #define NET_MODULE_ACK_SEARCH 0X84 //回应所搜命令码 17 18 #define CH9120_CFG_FLAG "CH9120_CFG_FLAG" //用来标识通信_new 19 typedef struct _DEVICEHW_CONFIG 20 { 21 UCHAR bDevType; /* 设备类型*/ 22 UCHAR bAuxDevType; /* 设备子类型 */ 23 UCHAR bIndex; /* 设备序号 */ 24 UCHAR bDevHardwareVer; /* 设备硬件版本号 */ 25 UCHAR bDevSoftwareVer; /* 设备软件版本号 */ 26 UCHAR szModulename[21]; /* 模块名*/ 27 UCHAR bDevMAC[ 6 ]; /* 模块网络MAC地址 */ 28 UCHAR bDevIP[ 4 ]; /* 模块IP地址*/ 29 UCHAR bDevGWIP[ 4 ]; /* 模块网关IP */ 30 UCHAR bDevIPMask[ 4 ]; /* 模块子网掩码 */ 31 UCHAR bDhcpEnable; /* DHCP 使能,是否启用DHCP,1:启用,0:不启用*/ 32 USHORT wWebPort; /* WEB网页地址 */ 33 UCHAR szUsername[8]; /* 用户名同模块名*/ 34 UCHAR bPassWordEn; /*密码使能 1:使能 0: 禁用*/ 35 UCHAR szPassWord[8]; /* 密码*/ 36 UCHAR bUpdateFlag; /* 固件升级标志,1:升级 0:不升级*/ 37 UCHAR bComcfgEn; /*串口协商进入配置模式使能,1:使能 0:不使能 */ 38 UCHAR breserved[8]; /* 保留*/ 39 }DeviceHWConfigS,*pDeviceHWConfigS; 40 41 typedef struct _DEVICEPORT_CONFIG 42 { 43 UCHAR bIndex; /* 端口序号 */ 44 UCHAR bPortEn; /* 端口启用标志 1:启用后 ;0:不启用 */ 45 UCHAR bNetMode; /* 网络工作模式: 0: TCP SERVER;1: TCP CLENT; 2: UDP SERVER 3:UDP CLIENT; */ 46 UCHAR bRandSportFlag; /* TCP 客户端模式下随即本地端口号,1:随机 0: 不随机*/ 47 USHORT wNetPort; /* 网络通讯端口号 */ 48 UCHAR bDesIP[ 4 ]; /* 目的IP地址 */ 49 USHORT wDesPort; /* 工作于TCP Server模式时,允许外部连接的端口号 */ 50 ULONG dBaudRate; /* 串口波特率: 300---921600bps */ 51 UCHAR bDataSize; /* 串口数据位: 5---8位 */ 52 UCHAR bStopBits; /* 串口停止位: 1表示1个停止位; 2表示2个停止位 */ 53 UCHAR bParity; /* 串口校验位: 0表示奇校验; 1表示偶校验; 2表示标志位(MARK,置1); 3表示空白位(SPACE,清0);4为无校验 */ 54 UCHAR bPHYChangeHandle; /* PHY断开,Socket动作,1:关闭Socket 2、不动作*/ 55 ULONG dRxPktlength; /* 串口RX数据打包长度,最大1024 */ 56 ULONG dRxPktTimeout; /* 串口RX数据打包转发的最大等待时间,单位为: 10ms,0则表示关闭超时功能 */ 57 UCHAR bReConnectCnt; /* 工作于TCP CLIENT时,连接TCP SERVER的最大重试次数*/ 58 UCHAR bResetCtrl; /* 串口复位操作: 0表示不清空串口数据缓冲区; 1表示连接时清空串口数据缓冲区 */ 59 UCHAR bDNSFlag; /* 域名功能启用标志,1:启用 2:不启用*/ 60 UCHAR szDomainname[20]; /* 域名*/ 61 UCHAR bDNSHostIP[4]; /* DNS 主机*/ 62 USHORT wDNSHostPort; /* DNS 端口*/ 63 UCHAR breserved[8]; /* 保留*/ 64 }DevicePortConfigS,*pDevicePortConfigS;
1.通讯方式
CH9120 通过 UDP 广播方式进行网络配置功能。
CH9120以60000端口向50000端口下发广播数据,模块以50000端口向60000端口回传数据
1 //网络通信结构体 2 typedef struct NET_COMM { 3 unsigned char flag[16]; //通信标识,因为都是用广播方式进行通信的,所以这里加一个固定值 4 unsigned char cmd; //命令头 5 unsigned char id[6]; //目标模块mac地址 6 unsigned char cfg_mac[6]; //配置软件端的MAC 7 unsigned char len; //数据区长度 8 unsigned char data[NET_MODULE_DATA_LENGTH]; //数据区缓冲区 9 }net_comm,*pnet_comm; 10 11 //模块的配置结构 12 #pragma pack(1) 13 //模块MAC\IP结构体 14 typedef struct _Mod_MacIP{ 15 unsigned char mod_ip[4]; 16 unsigned char mod_mac[6]; 17 unsigned char mod_name[21]; 18 unsigned char mod_ver; 19 }Mod_MacIP,pMod_MacIP; 20 21 typedef struct _NET_DEVICE_CONFIG 22 { 23 DeviceHWConfigS HWCfg; /*从硬件处获取的配置信息*/ 24 DevicePortConfigS PortCfg[2]; /*网络设备所包含的子设备的配置信息*/ 25 }NetDeviceConfigS,*pNetDeviceConfigS;
CH9120上位机配置功能主要包括:①搜索,②获取配置,③配置网络参数,④恢复出厂设置。
下面结合通讯过程中的数据包解析设置流程。(wireshark抓包可以抓取到数据包)
-->PC下发的搜索包:
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00 //固定16字节通信标识
04 //通信命令码,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 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
<--9120回复的应答包:
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00
84 //回应所搜命令码
38 3b 26 60 00 1f //模块mac地址
00 00 00 00 00 00
0b //数据长度(IP+设备名总长)
c0 a8 03 c8 //模块IP
43 48 39 31 32 30 00 //模块设备名(最大21字节)
0f //模块固件版本号
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 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 30 5f 43 46 47 5f 46 4c 41 47 00
02 //获取某个模块的配置
38 3b 26 60 00 1f //模块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
<--9120的应答包:
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00
82 //回应获取命令码
38 3b 26 60 00 1f //模块mac
00 00 00 00 00 00
cc //数据总长(从下一个数据为开始,不包括最后51固定字节)
21 /* 设备类型 */
21 /* 设备子类型 */
01 /* 设备序号 */
02 /* 设备硬件版本号 */
03 /* 设备软件版本号 */
43 48 39 31 32 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 /* 模块名,21字节*/
38 3b 26 60 00 1f /* 模块网络MAC地址 */
c0 a8 01 c8 /* 模块IP地址*/
c0 a8 01 01 /* 模块网关IP */
ff ff ff 00 /* 模块子网掩码 */
00 /* DHCP 使能,是否启用DHCP,1:启用,0:不启用*/
50 00 /* WEB网页地址 */
61 64 6d 69 6e 00 00 00 /* 用户名同模块名*/ (恢复出厂设置后默认为该数据,正常设置时为00即可)
00 /*密码使能 1:使能 0: 禁用*/
08 08 08 08 08 08 08 08 /* 密码*/ (恢复出厂设置后默认为该数据,正常设置时为00即可)
ff /* 固件升级标志,1:升级 0:不升级*/
00 /*串口协商进入配置模式使能,1:使能 0:不使能 */
00 00 00 00 00 00 00 00 /* 保留*/
/*************************************************端口0设置*************************************************/
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 /* 目的端口 */···························固定
80 25 00 00 /* 串口波特率: 300---921600bps */
08 /* 串口数据位 */
01 /* 串口停止位 */
04 /* 串口校验位 */
01 /* PHY断开,Socket动作,1:关闭Socket 2、不动作*/
00 02 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 /* 保留*/
/*************************************************端口1设置************************************************/(CH9120只有一个端口,默认用端口1)
01 /* 端口序号 */
01 /* 端口启用标志 1:启用 ;0:不启用 */
01 /* 网络工作模式: 0: TCP SERVER;1: TCP CLENT; 2: UDP SERVER 3:UDP CLIENT; */
01 /* TCP 客户端模式下随即本地端口号,1:随机 0: 不随机*/
d0 07 /* 源端口号 */
c0 a8 01 64 /* 目的IP地址 */
e8 03 /* 目的端口 */
80 25 00 00 /* 串口波特率: 300---921600bps */
08 /* 串口数据位 */
01 /* 串口停止位 */
04 /* 串口校验位 */
01 /* PHY断开,Socket动作,1:关闭Socket 2、不动作*/
00 02 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 /* 保留*/
55 aa 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
00 00 00
-->PC机下发的恢复出厂请求包:
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00
01 //配置网络中的模块
38 3b 26 60 00 1f
00 e0 4c 36 3c ec
cc
21
21
01
02
03
43 48 39 31 32 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
38 3b 26 60 00 1f
c0 a8 01 c8
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
00
02
01
b8 0b
c0 a8 01 64
d0 07
80 25 00 00
08
01
04
01
00 02 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 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
01
01
00
88 13
c0 a8 01 64
88 13
80 25 00 00
08
01
04
01
00 02 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00
<--9120应答包
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00
81 //回应配置命令码
38 3b 26 60 00 1f
00 e0 4c 36 3c ec
00
21
21
01
02
03
43 48 39 31 32 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
38 3b 26 60 00 1f
c0 a8 01 c8
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
00
02
01
b8 0b
c0 a8 01 64
d0 07
80 25 00 00
08
01
04
01
00 02 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 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
01
01
00
88 13
c0 a8 01 64
88 13
80 25 00 00
08
01
04
01
00 02 00 00
00 00 00 00
00
00
00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 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 30 5f 43 46 47 5f 46 4c 41 47 00
03 //复位模块
38 3b 26 60 00 1f //模块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
<--9120应答包
43 48 39 31 32 30 5f 43 46 47 5f 46 4c 41 47 00
83 //回应复位命令码
38 3b 26 60 00 1f //模块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
CH9120配置工具协议文档:https://files.cnblogs.com/files/blogs/808422/NetModCfgDemo_CH9120.zip?t=1712976869&download=true
如果需要移植CH9121的配置工具,可参考此链接:CH9121网口配置协议及说明