NBIOT实现UDP协议的发送和接收(包含软件升级)

源码下载:

nbiot_module程序(java netbean)

UdpServer程序(C# vs2010)  

 

前提条件:开NB卡的时候需要绑定私有ip(电信运营商设置),链路有效连接为30秒,超时了服务器不能给NB模块主发送数据。如果需要服务器主动给NB模块发送指令,需要走电信平台。

UDP网络通信协议规范》

通信采用GB2312编码格式

数据包格式:

标示符=长度#设备编号#任务id#包类型#包内容#

备注

长度: 4位不足补0,从标示符#到#之间的长度大小

1. 包内容为空

HTEMP=0026#1533022506#Meter-001#01##

2. 包内容含有多个字段

HTEMP=0043#1533022506#Meter-001#01#get-version@nb001#

3. 包内容含有多个字段(字段间用@分割,字段内的数组用[{},{}])

HTEMP=0261#WaterMeter-001#1520557005#03#buildid=44@edmid=37@meter=1228@denddate=2018-02-05 17:36:22@[{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}]#

.心跳包 
HTEMP=0026#Meter-001#1533022506#01##
HTEMP=0028#Meter-001#1533022506#01#ok#

.时间同步     
HTEMP=0026#Meter-001#1533022506#02##
HTEMP=0045#Meter-001#1533022506#02#2018-08-16 09:50:58#

.数据上传 
HTEMP=0256#Meter-001#1520557005#03#buildid=44@edmid=37@meter=1228@denddate=2018-02-05 17:36:22@[{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}+{132,0.0000}]#
HTEMP=0048#Meter-001#1520557005#03#error@服务器返回失败!#
HTEMP=0028#Meter-001#1520557005#03#ok#

.版本查询     
HTEMP=0044#Meter-0001#1533022507#04#get-version@nb001#
HTEMP=0028#Meter-0001#1533022507#04#2#

.软件更新 
HTEMP=0042#Meter-0001#1533022508#05#get-count@nb001#
HTEMP=0029#Meter-0001#1533022508#05#16#

.软件下载 
HTEMP=0044#Meter-0001#1533022508#06#do-update@nb001@1#
HTEMP=0495#Meter-0001#1533022508#06#504B03041400000008007A640F4D6BEBFDBA000E00004D2E000007000000636F6D732E7079D51A6B73D356F6BB7FC55D7932926AC7D80EA1901DB10D8901CF244ED636FB189AF1C8D275A246B6BC924C603B9D816E29852DD09D6E976D0B944E4BB7B3DB02EDD22729FD3328099FF80B7BEE4B962C3BA4E36DBBAB99C4D23DCF7BCEB9F79C7BA4349A7C6612198E6975566750CF6F350E929194D5EE3AAE8FBC339EB8B59DD55540128FBED5C6E2BEED98CD9ED7F0D713033913B7AC0EF690EE21C3F393707647C0EC6E1446C3F57B7D2CF23412D337BA114C780A27835D4BB7C553CFB56DAB59CCB2DF#

.版本更新 
HTEMP=0046#Meter-0001#1533022509#07#update-version@56@2#
HTEMP=0029#Meter-0001#1533022509#07#ok#

 

 

 

 

package com.module.ui;

import com.module.serial.BC95SerialPort;

import com.module.serial.CCommondFunc;

import com.module.serial.CTxtHelper;

public class Program {

static int port = 9096;

static BC95SerialPort serialport = new BC95SerialPort();

public static void main(String[] args) {

String com = "COM3";// /dev/ttyAMA0

boolean ret = serialport.init(com, "117.25.162.156", "8292");

if (ret)

{

System.out.println("串口打开成功:" + com);

//ret = serialport.ReStartModule();

if (ret)

{

Heart();

Time();

Upload();

Update();

}

}

else {

System.out.println("串口打开失败:" + com);

}

}

static void Heart() {

String protocol = "HTEMP=0026#Meter-001#1533022506#01##";

String outPut = serialport.PassData1(protocol);

CTxtHelper.AppendLog("===========================");

CTxtHelper.AppendLog("[input]:" + protocol);

CTxtHelper.AppendLog("[output]:" + outPut);

CTxtHelper.AppendLog("===========================");

}

static void Time() {

String protocol = "HTEMP=0213#Meter-8888#1533022507#03#buildid=44@edmid=37@meter=1604@senddate=2018-07-31 15:35:02@[{32,233.1}+{33,217.5}+{34,229.1}+{35,0.0}+{36,0.0}+{37,0.0}+{40,0.0}+{41,0.0}+{38,0.0}+{39,50.01}+{56,0.0}+{54,0.0}+{55,0.0}]#";

String outPut = serialport.PassData1(protocol);

CTxtHelper.AppendLog("===========================");

CTxtHelper.AppendLog("[input]:" + protocol);

CTxtHelper.AppendLog("[output]:" + outPut);

CTxtHelper.AppendLog("===========================");

}

static void Upload() {

String protocol = "HTEMP=0213#Meter-8888#1533022507#03#buildid=44@edmid=37@meter=1604@senddate=2018-07-31 15:35:02@[{32,233.1}+{33,217.5}+{34,229.1}+{35,0.0}+{36,0.0}+{37,0.0}+{40,0.0}+{41,0.0}+{38,0.0}+{39,50.01}+{56,0.0}+{54,0.0}+{55,0.0}]#";

String outPut = serialport.PassData1(protocol);

CTxtHelper.AppendLog("===========================");

CTxtHelper.AppendLog("[input]:" + protocol);

CTxtHelper.AppendLog("[output]:" + outPut);

CTxtHelper.AppendLog("===========================");

}

static void Update() {

String back = ""; String data = "";

String protocol = "HTEMP=0044#Meter-0001#1533022507#04#get-version@nb001#";

String outPut = serialport.PassData1(protocol);

CTxtHelper.AppendLog("===========================");

CTxtHelper.AppendLog("[input]:" + protocol);

CTxtHelper.AppendLog("[output]:" + outPut);

CTxtHelper.AppendLog("===========================");

protocol = "HTEMP=0042#Meter-0001#1533022508#05#get-count@nb001#";

back = serialport.PassData1(protocol);

CTxtHelper.AppendLog("===========================");

CTxtHelper.AppendLog("[input]:" + protocol);

CTxtHelper.AppendLog("[output]:" + back);

CTxtHelper.AppendLog("===========================");

if (!"".endsWith(back))

{

data = back.split("#")[4];

int count = Integer.parseInt(data);

String[] buffer = new String[count];

String[] source = new String[count];

CTxtHelper.AppendLog("Count:" + String.valueOf(count));

for (int i = 0; i < count; i++)

{

buffer[i] = "HTEMP=[XX]#Meter-0001#1533022508#06#do-update@nb001@" + String.valueOf(i + 1) + "#";

}

int tryIndex = 0; boolean downresult = true;

while (tryIndex++ < 10)

{

downresult = true;

for (int i = 0; i < count; i++)

{

protocol = buffer[i];

if (!"".endsWith(protocol))

{

downresult = false;

back = serialport.PassData1(protocol);

if (!"".endsWith(back))

{

buffer[i] = "";

data = back.split("#")[4];

source[i] = data;

}

else

{

CTxtHelper.AppendLog("Fail:" + String.valueOf(i + 1));

}

}

}

if (downresult) break;

}

if (downresult)

{

StringBuilder strFileData = new StringBuilder();

for (int i = 0; i < count; i++)

{

strFileData.append(source[i]);

}

byte[] arrFileData = CCommondFunc.hexStringToBytes(strFileData.toString());

CCommondFunc.writeToFile(arrFileData, "D:\\NBDown\\config.zip");

}

}

}

}

run:
串口打开成功:COM3
20190222 14:21:54 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:21:55 send data=AT+NSOST=0,117.25.162.156,8292,36,4854454D503D30303236234D657465722D30303123313533333032323530362330312323

20190222 14:21:57 send data=AT+NSORF=0,38


20190222 14:21:58 recv:HTEMP=0028#Meter-001#1533022506#01#ok#
20190222 14:21:58 send data=AT+NSOCL=0

20190222 14:21:58 ===========================
20190222 14:21:58 [input]:HTEMP=0026#Meter-001#1533022506#01##
20190222 14:21:58 [output]:HTEMP=0028#Meter-001#1533022506#01#ok#
20190222 14:21:58 ===========================
20190222 14:21:58 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:21:59 send data=AT+NSOST=0,117.25.162.156,8292,223,4854454D503D30323133234D657465722D383838382331353333303232353037233033236275696C6469643D34344065646D69643D3337406D657465723D313630344073656E64646174653D323031382D30372D33312031353A33353A3032405B7B33322C3233332E317D2B7B33332C3231372E357D2B7B33342C3232392E317D2B7B33352C302E307D2B7B33362C302E307D2B7B33372C302E307D2B7B34302C302E307D2B7B34312C302E307D2B7B33382C302E307D2B7B33392C35302E30317D2B7B35362C302E307D2B7B35342C302E307D2B7B35352C302E307D5D23

20190222 14:22:01 send data=AT+NSORF=0,39


20190222 14:22:02 recv:HTEMP=0029#Meter-8888#1533022507#03#ok#
20190222 14:22:02 send data=AT+NSOCL=0

20190222 14:22:02 ===========================
20190222 14:22:02 [input]:HTEMP=0213#Meter-8888#1533022507#03#buildid=44@edmid=37@meter=1604@senddate=2018-07-31 15:35:02@[{32,233.1}+{33,217.5}+{34,229.1}+{35,0.0}+{36,0.0}+{37,0.0}+{40,0.0}+{41,0.0}+{38,0.0}+{39,50.01}+{56,0.0}+{54,0.0}+{55,0.0}]#
20190222 14:22:02 [output]:HTEMP=0029#Meter-8888#1533022507#03#ok#
20190222 14:22:02 ===========================
20190222 14:22:02 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:03 send data=AT+NSOST=0,117.25.162.156,8292,223,4854454D503D30323133234D657465722D383838382331353333303232353037233033236275696C6469643D34344065646D69643D3337406D657465723D313630344073656E64646174653D323031382D30372D33312031353A33353A3032405B7B33322C3233332E317D2B7B33332C3231372E357D2B7B33342C3232392E317D2B7B33352C302E307D2B7B33362C302E307D2B7B33372C302E307D2B7B34302C302E307D2B7B34312C302E307D2B7B33382C302E307D2B7B33392C35302E30317D2B7B35362C302E307D2B7B35342C302E307D2B7B35352C302E307D5D23

20190222 14:22:05 send data=AT+NSORF=0,39


20190222 14:22:06 recv:HTEMP=0029#Meter-8888#1533022507#03#ok#
20190222 14:22:06 send data=AT+NSOCL=0

20190222 14:22:06 ===========================
20190222 14:22:06 [input]:HTEMP=0213#Meter-8888#1533022507#03#buildid=44@edmid=37@meter=1604@senddate=2018-07-31 15:35:02@[{32,233.1}+{33,217.5}+{34,229.1}+{35,0.0}+{36,0.0}+{37,0.0}+{40,0.0}+{41,0.0}+{38,0.0}+{39,50.01}+{56,0.0}+{54,0.0}+{55,0.0}]#
20190222 14:22:06 [output]:HTEMP=0029#Meter-8888#1533022507#03#ok#
20190222 14:22:06 ===========================
20190222 14:22:06 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:07 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D303030312331353333303232353037233034236765742D76657273696F6E406E6230303123

20190222 14:22:08 send data=AT+NSORF=0,38


20190222 14:22:09 recv:HTEMP=0028#Meter-0001#1533022507#04#3#
20190222 14:22:09 send data=AT+NSOCL=0

20190222 14:22:09 ===========================
20190222 14:22:09 [input]:HTEMP=0044#Meter-0001#1533022507#04#get-version@nb001#
20190222 14:22:09 [output]:HTEMP=0028#Meter-0001#1533022507#04#3#
20190222 14:22:09 ===========================
20190222 14:22:09 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:10 send data=AT+NSOST=0,117.25.162.156,8292,52,4854454D503D30303432234D657465722D303030312331353333303232353038233035236765742D636F756E74406E6230303123

20190222 14:22:11 send data=AT+NSORF=0,38


20190222 14:22:12 recv:HTEMP=0028#Meter-0001#1533022508#05#5#
20190222 14:22:12 send data=AT+NSOCL=0

20190222 14:22:12 ===========================
20190222 14:22:12 [input]:HTEMP=0042#Meter-0001#1533022508#05#get-count@nb001#
20190222 14:22:12 [output]:HTEMP=0028#Meter-0001#1533022508#05#5#
20190222 14:22:12 ===========================
20190222 14:22:12 Count:5
20190222 14:22:12 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:13 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D30303031233135333330323235303823303623646F2D757064617465406E62303031403123

20190222 14:22:15 send data=AT+NSORF=0,505


20190222 14:22:16 recv:HTEMP=0495#Meter-0001#1533022508#06#504B03041400000008007A880E4D604C094741030000F83F00000A000000436F6E6669672E696E69EDDABF4B1C411407F05EF07F58B0CCBACC9B796F7E2C1C5C93229080C4F2B8C264AF388C0AE7A592D4214148FA5456E9D3247085FF4C8C76F917B2A76E4EE520F13B06C34B1461389D553FF7DC37EFEB7D9F1D535C3761DD5141BE165393148FC7FBD3D16E3178B45153B215F9D87E54AEDCD89B4CEB488986AB2BF3F71B3BC5154FEB6240142A4F95E58A92A9AD786386C5F39DA6E78A666BBAD57B329A8E26EBB17D5B2371CE58EB85D78C5B7BF672FCA219373DE6FEA8D969172EF477E65FDC23#
20190222 14:22:16 send data=AT+NSOCL=0

20190222 14:22:17 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:17 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D30303031233135333330323235303823303623646F2D757064617465406E62303031403223

20190222 14:22:19 send data=AT+NSORF=0,505


20190222 14:22:21 recv:HTEMP=0495#Meter-0001#1533022508#06#6FB8BF3FDA6DDA0B8C7AD62CBEB1D44E6A63FB8303674BEB5C45AF1E1C3857DAF68790F9924B6BD3C5A3529ACACC17BE5B84CB059B6E41DDA762B748A5B4ABF905A4DB27DC2D2E2E395C8621C5E66F60EC6D2FDB9B6048510649B5312864BB17AF48AF0F923320230C19F441461C9208868CEA20C966400A0C99F4414A0624DC6CBCD1079970480B371B4FEA206D46B3B10186B4FA20339A8DB330A45307E95C06A48721591FA4C721D9C090FA261BCE986C9861C82B938D16C88CE30FE3C71F7D930D671C7FF0F4C7EB9B6C24A3D9087EFCD137D94840216D6DE0593B689B6CCE317048B8D904D20729#
20190222 14:22:21 send data=AT+NSOCL=0

20190222 14:22:21 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:22 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D30303031233135333330323235303823303623646F2D757064617465406E62303031403323

20190222 14:22:24 send data=AT+NSORF=0,505


20190222 14:22:25 recv:HTEMP=0495#Meter-0001#1533022508#06#1990708C16AC3EC88843123CD904A70E128FD1DABDF06413581FA4CF80BCF6A71D2BF67510474B14E730978A9E0C7C88F4E78AD4299A2A768AB2508CF7A1188BC1C3C9646F322C4E3F7C39797FF875363B797B346E4E3E7F3A9DBDFBF6E6E3D9D1E1D9F1EBF2E5B8B942524EB7F6B7DB471632CBAFBDF92BE5A54F8F8507CF20EAEADC1ABCCEAF43FECB756ECD9FAB736BD03ABFC551ED469D7B7D75CE1975CE489D734E9DB3B9A268168A5C854E31DC87A2A0B518FE13FE94B8835B052FBF55843B797A6E75AB08FA6E1519C388C37386A80ED2650C230EFE9F7E48FA203372060787DA515FF2E5120E#
20190222 14:22:25 send data=AT+NSOCL=0

20190222 14:22:26 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:26 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D30303031233135333330323235303823303623646F2D757064617465406E62303031403423

20190222 14:22:28 send data=AT+NSORF=0,505


20190222 14:22:30 recv:HTEMP=0495#Meter-0001#1533022508#06#C970A81DF5255FEC3220E16613F5255F9C116A0BDC6CA2BEE44B32426D8127A5A82FF9928C662370A81DF5452B029F235DFBFBC090C0ECFE57439E63E09070A81D833E488F43129C9AC6A80E924C0624DE6C923E48CE8084CF91C9E883CC6836163E47265207692903120E2D92D5072919907068919C3EC884433A38B448AC0E127FD977BB176F36A20F32E090F8CBBE93BEC986339A0DFEB2EFA46FB2E18C66C3706891144C36AB2B3F00504B01021F001400000008007A880E4D604C094741030000F83F00000A0024000000000000002000000000000000436F6E6669672E696E690A002000000000#
20190222 14:22:30 send data=AT+NSOCL=0

20190222 14:22:30 send data=AT+NSOCR=DGRAM,17,9358,1

20190222 14:22:31 send data=AT+NSOST=0,117.25.162.156,8292,54,4854454D503D30303434234D657465722D30303031233135333330323235303823303623646F2D757064617465406E62303031403523

20190222 14:22:32 send data=AT+NSORF=0,139


20190222 14:22:33 recv:HTEMP=0129#Meter-0001#1533022508#06#00010018006AF5BFB8AD33D401A3CC6F80AB28D401A9253DCC7833D401504B050600000000010001005C000000690300000000#
20190222 14:22:33 send data=AT+NSOCL=0

BUILD SUCCESSFUL (total time: 40 seconds)
 

完成升级包下载

 

 

posted @ 2019-07-25 11:10  CHHC  阅读(5733)  评论(0编辑  收藏  举报