目錄
(1)結構體定義
(2)通訊流程詳解
1.通訊方式
2.通訊結構體
3.通訊過程
①搜尋
②獲取配置
③配置模組
④恢復出廠設定
(3)說明文件連線
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
<--9121回覆的應答包:
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
<--9121的應答包:
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
<--9121應答包
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
<--9121應答包
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
協議文件:https://files.cnblogs.com/files/blogs/808422/NetModCfgDemo_CH9120.zip?t=1712976869&download=true
CH9121流程及資料包解析可參考隨筆:CH9121網口配置協議及說明