修改:设备模版支持获取多个命令的报文

This commit is contained in:
969753572 2020-08-07 18:06:01 +08:00
parent e4fb2e2b77
commit 823995b1fb
13 changed files with 145 additions and 92 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -254,13 +254,15 @@ func (d *CollectInterfaceTemplate) ModifyCollectInterface(pollPeriod, offlinePer
func (d *CollectInterfaceTemplate) NewDeviceNode(dName string,dType string, dAddr string) {
node := &DeviceNodeTemplate{}
node.Name = dName
node.Type = dType
node.Addr = dAddr
node.Index = len(d.DeviceNodeMap)
node.Name = dName
node.Addr = dAddr
node.Type = dType
node.LastCommRTC = "1970-01-01 00:00:00"
node.CommTotalCnt = 0
node.CommSuccessCnt = 0
node.CommStatus = "onLine"
node.CurCommFailCnt = 0
node.CommStatus = "offLine"
node.VariableMap = make([]api.VariableTemplate, 0)
variables := node.NewVariables()
node.VariableMap = append(node.VariableMap, variables...)
@ -271,18 +273,19 @@ func (d *CollectInterfaceTemplate) NewDeviceNode(dName string,dType string, dAdd
func (d *CollectInterfaceTemplate) AddDeviceNode(dName string,dType string, dAddr string) (bool, string) {
node := &DeviceNodeTemplate{}
node.Name = dName
node.Type = dType
node.Addr = dAddr
node.Index = len(d.DeviceNodeMap)
node.Name = dName
node.Addr = dAddr
node.Type = dType
node.LastCommRTC = "1970-01-01 00:00:00"
node.CommTotalCnt = 0
node.CommSuccessCnt = 0
node.CommStatus = "onLine"
node.CurCommFailCnt = 0
node.CommStatus = "offLine"
node.VariableMap = make([]api.VariableTemplate, 0)
variables := node.NewVariables()
node.VariableMap = append(node.VariableMap, variables...)
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
d.DeviceNodeCnt++
return true, "add success"

View File

@ -48,6 +48,7 @@ func (c *CommunicationManageTemplate)CommunicationManageDel() {
{
log.Println("emergency chan")
log.Printf("funName %s\n", cmd.FunName)
/*
var status bool = false
for _, c := range CollectInterfaceMap {
if c.CollInterfaceName == cmd.CollInterfaceName {
@ -121,6 +122,7 @@ func (c *CommunicationManageTemplate)CommunicationManageDel() {
}
}
c.EmergencyAckChan <- status
*/
}
default:
{
@ -134,67 +136,89 @@ func (c *CommunicationManageTemplate)CommunicationManageDel() {
for k,v := range c.DeviceNodeMap{
if v.Addr == cmd.DeviceAddr {
log.Printf("index is %d\n", k)
//--------------组包---------------------------
txBuf := make([]byte,0)
txBuf = append(txBuf,v.GenerateGetRealVariables(v.Addr)...)
log.Printf("tx buf is %+v\n", txBuf)
//---------------发送-------------------------
for _,v := range CommunicationSerialMap{
if v.Name == c.CommInterfaceName{
v.WriteData(txBuf)
step := 0
for{
//--------------组包---------------------------
txBuf := make([]byte,0)
log.Printf("step %d\n",step)
buf,ok := v.GenerateGetRealVariables(v.Addr,step)
if ok == false{
log.Printf("getVariables false\n")
goto LoopCommonStep
}
}
v.CommTotalCnt++
//---------------等待接收----------------------
//阻塞读
rxBuf := make([]byte, 256)
rxTotalBuf := make([]byte, 0)
rxBufCnt := 0
rxTotalBufCnt := 0
//timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Timeout)
timer := time.NewTimer(time.Duration(1000) * time.Millisecond)
for {
select {
//是否正确收到数据包
case <-v.AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt):
{
log.Println("rx ok")
//通信帧延时
//interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
//time.Sleep(time.Duration(interval)*time.Millisecond)
v.CommSuccessCnt++
goto LoopCommon
step++
txBuf = append(txBuf,buf...)
log.Printf("tx buf is %X\n", txBuf)
//---------------发送-------------------------
for _,v := range CommunicationSerialMap{
if v.Name == c.CommInterfaceName{
v.WriteData(txBuf)
}
//是否接收超时
case <-timer.C:
{
log.Println("rx timeout")
//通信帧延时
//interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
//time.Sleep(time.Duration(interval)*time.Millisecond)
goto LoopCommon
}
//继续接收数据
default:
{
//rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf)
for _,v := range CommunicationSerialMap{
if v.Name == c.CommInterfaceName{
rxBufCnt = v.ReadData(rxBuf)
}
v.CommTotalCnt++
//---------------等待接收----------------------
//阻塞读
rxBuf := make([]byte, 256)
rxTotalBuf := make([]byte, 0)
rxBufCnt := 0
rxTotalBufCnt := 0
//timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Timeout)
timer := time.NewTimer(time.Duration(1500) * time.Millisecond)
for {
select {
//是否正确收到数据包
case <-v.AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt):
{
log.Println("rx ok")
//通信帧延时
//interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
//time.Sleep(time.Duration(interval)*time.Millisecond)
v.CommSuccessCnt++
v.CommStatus = "onLine"
v.LastCommRTC = time.Now().Format("2006-01-02 15:04:05")
rxTotalBufCnt = 0
rxTotalBuf = rxTotalBuf[0:0]
goto LoopCommon
}
//是否接收超时
case <-timer.C:
{
log.Println("rx timeout")
//通信帧延时
//interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
//time.Sleep(time.Duration(interval)*time.Millisecond)
v.CurCommFailCnt++
if v.CurCommFailCnt >= c.OfflinePeriod{
v.CurCommFailCnt = 0
v.CommStatus = "offLine"
}
rxTotalBufCnt = 0
rxTotalBuf = rxTotalBuf[0:0]
goto LoopCommon
}
//继续接收数据
default:
{
//rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf)
for _,v := range CommunicationSerialMap{
if v.Name == c.CommInterfaceName{
rxBufCnt = v.ReadData(rxBuf)
}
}
if rxBufCnt > 0 {
rxTotalBufCnt += rxBufCnt
//追加接收的数据到接收缓冲区
rxTotalBuf = append(rxTotalBuf, rxBuf[:rxBufCnt]...)
//清除本地接收数据
rxBufCnt = 0
log.Printf("rxbuf %X\n", rxTotalBuf)
}
}
if rxBufCnt > 0 {
rxTotalBufCnt += rxBufCnt
//追加接收的数据到接收缓冲区
rxTotalBuf = append(rxTotalBuf, rxBuf[:rxBufCnt]...)
//清除本地接收数据
rxBufCnt = 0
log.Printf("rxbuf %X\n", rxTotalBuf)
}
}
}
LoopCommon:
}
LoopCommon:
LoopCommonStep:
}
}
}

View File

@ -15,6 +15,7 @@ type DeviceNodeTemplate struct {
LastCommRTC string `json:"LastCommRTC"` //最后一次通信时间戳
CommTotalCnt int `json:"CommTotalCnt"` //通信总次数
CommSuccessCnt int `json:"CommSuccessCnt"` //通信成功次数
CurCommFailCnt int `json:"-"` //当前通信失败次数
CommStatus string `json:"CommStatus"` //通信状态
VariableMap []api.VariableTemplate `json:"-"` //变量列表
}
@ -31,16 +32,16 @@ func (d *DeviceNodeTemplate) NewVariables() []api.VariableTemplate {
return nil
}
func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string) []byte {
func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string,step int) ([]byte,bool) {
for k,v := range DeviceNodeTypeMap.DeviceNodeType {
if d.Type == v.TemplateType {
generateGetRealVariablesFun, _ := DeviceTypePluginMap[k].Lookup("GenerateGetRealVariables")
nBytes := generateGetRealVariablesFun.(func(string) []byte)(sAddr)
return nBytes
nBytes,ok := generateGetRealVariablesFun.(func(string,int) ([]byte,bool))(sAddr,step)
return nBytes,ok
}
}
return nil
return nil,false
}
func (d *DeviceNodeTemplate) AnalysisRx(sAddr string,variables []api.VariableTemplate,rxBuf []byte,rxBufCnt int) chan bool{

BIN
goAdapter

Binary file not shown.

BIN
plugin/.DS_Store vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
{"templateName":"风机盘管控制器","templateType":"wdt200","templateMessage":""}

Binary file not shown.

View File

@ -1 +1 @@
{"CollectInterfaceParam":[{"CollInterfaceName":"1","CommInterfaceName":"com1","PollPeriod":60,"OfflinePeriod":3,"DeviceNodeCnt":2}],"DeviceNodeNameMap":["1","2"],"DeviceNodeAddrMap":["1","2"],"DeviceNodeTypeMap":["td200","wdt200"]}
{"CollectInterfaceParam":[{"CollInterfaceName":"1","CommInterfaceName":"com1","PollPeriod":60,"OfflinePeriod":3,"DeviceNodeCnt":1}],"DeviceNodeNameMap":["1"],"DeviceNodeAddrMap":["1"],"DeviceNodeTypeMap":["td200"]}

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/binary"
"fmt"
"goAdapter/api"
"log"
"time"
@ -66,6 +67,10 @@ const (
CmdAck_WriteChanAndPanID =0xE0
)
const (
TotalStep = 2
)
func AddVariable(vindex int,vname string,vlable string,vtype string) api.VariableTemplate{
variable := api.VariableTemplate{}
@ -84,7 +89,7 @@ func NewVariables() []api.VariableTemplate{
VariableMap = append(VariableMap, AddVariable(0,"Chan","信道","string"))
VariableMap = append(VariableMap, AddVariable(1,"PanID","系统ID","string"))
VariableMap = append(VariableMap, AddVariable(2,"SoftVer","软件版本","string"))
VariableMap = append(VariableMap, AddVariable(4,"RTC","时钟","string"))
VariableMap = append(VariableMap, AddVariable(3,"RTC","时钟","string"))
return VariableMap
}
@ -102,22 +107,35 @@ func TD200GetCRC(buf []byte,bufLen int) byte{
}
/**
生成读变量的数据包
生成读变量的数据包
*/
func GenerateGetRealVariables(sAddr string) []byte{
func GenerateGetRealVariables(sAddr string,step int) ([]byte,bool){
requestAdu := make([]byte,0)
requestAdu = append(requestAdu,0xFE)
requestAdu = append(requestAdu,0xA5)
requestAdu = append(requestAdu,TYPE_CMD)
requestAdu = append(requestAdu,0x01)
requestAdu = append(requestAdu,CmdRequest_ReadChanAndPanID)
crc := TD200GetCRC(requestAdu[2:],3)
requestAdu = append(requestAdu,crc)
if step == TotalStep{
return requestAdu,false
}
if step == 0{
requestAdu = append(requestAdu,0xFE)
requestAdu = append(requestAdu,0xA5)
requestAdu = append(requestAdu,TYPE_CMD)
requestAdu = append(requestAdu,0x01)
requestAdu = append(requestAdu,CmdRequest_ReadChanAndPanID)
crc := TD200GetCRC(requestAdu[2:],3)
requestAdu = append(requestAdu,crc)
}else if step == 1{
requestAdu = append(requestAdu,0xFE)
requestAdu = append(requestAdu,0xA5)
requestAdu = append(requestAdu,TYPE_CMD)
requestAdu = append(requestAdu,0x01)
requestAdu = append(requestAdu,CmdRequest_ReadRTC)
crc := TD200GetCRC(requestAdu[2:],3)
requestAdu = append(requestAdu,crc)
}
return requestAdu
return requestAdu,true
}
func AnalysisRx(sAddr string,variables []api.VariableTemplate,rxBuf []byte,rxBufCnt int) chan bool{
@ -162,20 +180,28 @@ func AnalysisRx(sAddr string,variables []api.VariableTemplate,rxBuf []byte,rxBuf
if frameType != TYPE_CMD{
return status
}
if framePayload[0] != CmdAck_ReadChanAndPanID{
return status
if framePayload[0] == CmdAck_ReadChanAndPanID{
timeNowStr := time.Now().Format("2006-01-02 15:04:05")
variables[0].TimeStamp = timeNowStr
variables[0].Value = framePayload[1]
variables[1].TimeStamp = timeNowStr
variables[1].Value = binary.BigEndian.Uint16(framePayload[2:])
}else if framePayload[0] == CmdAck_ReadRTC {
timeNowStr := time.Now().Format("2006-01-02 15:04:05")
variables[3].TimeStamp = timeNowStr
variables[3].Value = fmt.Sprintf("20%d-%02d-%02d %02d:%02d:%02d",framePayload[1],
framePayload[2],
framePayload[3],
framePayload[4],
framePayload[5],
framePayload[6])
}
timeNowStr := time.Now().Format("2006-01-02 15:04:05")
variables[0].TimeStamp = timeNowStr
variables[0].Value = framePayload[1]
variables[1].TimeStamp = timeNowStr
variables[1].Value = binary.BigEndian.Uint16(framePayload[2:])
status<-true
return status
}else{
break

Binary file not shown.