//定义串口名称
var comname = "COM1";
//定义解析单元
var units=[
unit("起始符", "BYTE[]", 2, [0xfe, 0xfe]),
unit("地址", "BYTE", 1),
unit("功能", "BYTE", 1),
unit("数据长度", "WORD", 2),
unit("数据", "BYTE[]", getlength),
unit("校验码", "BYTE[]", 2),
]
function getlength(bytes) {
var l = bytes.substr(bytes.length - 2, 2);
return bytes.obj(l, "WORD");
}
//定义收取数据函数
function received(data) {
console.log(bytes.hex(data));
}
//com.close(comname);
//检查串口,没打开就打开
if (!com.isopen(comname)) {
var para = {};
para.baudRate = 115200;
para.parity = "None";
para.dataBits = 8;
para.stopBits = 1;
com.open(comname, para,units, received);
}
//定义发送的数据
var bytes = hex.bytes("fe fe 03 02 00 02 01 02");
//crc16
var bs = crc16(bytes)
bytes = bytes.add(bs);
//串口发送数据
com.send(comname, bytes);
//关闭串口
//com.close(comname);