HJ212上报
概述
演示HJ212上报。
代码
var server = "xx.xx.xx.xx";
var port = 3344;
var cn = "2011";
var pw = "123456";
var mn = "40142001000003";
var report_period = 30 * 60 * 1000; //发送周期
function timeString()
{
var t = Box.getTime();
return Coder.num(t.year, "%04d") + Coder.num(t.month, "%02d") + Coder.num(t.day, "%02d") + Coder.num(t.hour, "%02d") + Coder.num(t.minute, "%02d") + Coder.num(t.second, "%02d");
}
//上报数据
function report(rc, td)
{
var rc = 100.01;
var td = 68.01;
var cp="DataTime=" + timeString() + ";w01101-Rtd=" + rc + ",w01101-Flag=N;w01102-Rtd=" + td + ",w01102-Flag=N";
var data = "QN=" + timeString() + "000;"+"ST=32;CN=" + cn + ";PW=" + pw + ";MN=" + mn + ";Flag=5;CP=&&" + cp + "&&";
var crc=Coder.hex(Coder.crc16(data, "ansi"), "2B");
print(crc);
Net.send(0, Packet.HJ212.packet(data));
Timer.start(2, 20000);
}
//处理服务器返回的数据
function hj212CB(data)
{
print("receive", data);
Timer.stop(2);
}
var parser = Packet.HJ212.Parser.create(hj212CB);
function netCB(m)
{
switch(m.event)
{
case Net.READY:
Net.syncTime();
Net.tcp(0, server, port);
break;
case Net.CONN_OK:
Timer.start(1, 1000);
break;
case Net.CONN_FAIL:
break;
case Net.CONN_CLOSE:
break;
case Net.RECV:
print(m.data);
parser.parse(m.data);//解析服务器下发数据包
break;
}
}
function timerCB(id)
{
switch(id)
{
case 1: //连接网络
if(Net.isConnected(0))
report();
Timer.start(1, report_period);
break;
case 2: //请求超时
print("Timeout");
}
}
//初始化硬件,程序从这里开始跑
GPIO.set(GPIO.POWER, 1);
Net.init();
while(true)
{
var m = Event.get();
switch(m.msg)
{
case Event.NET_EVENT:
netCB(m); //网络事件处理
break;
case Event.TIMER_OUT:
timerCB(m.id); //定时器处理
break;
case Event.UART_RECV:
break;
case Event.PIO_CHANGE:
break;
}
}
下载代码(右键另存为):hj212.js