修改:1、增加获取设备变量的接口 2、修改command结构体中变量首字母大写

This commit is contained in:
969753572 2020-07-09 11:09:01 +08:00
parent e1a417270b
commit a59410476a
5 changed files with 165 additions and 48 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
device/.DS_Store vendored

Binary file not shown.

View File

@ -9,12 +9,12 @@ import (
type CommunicationCmd struct{
interfaceID int //接口ID
deviceAddr string //接口下设备地址
deviceType string
funName string
funIndex int
funPara interface{}
InterfaceID int //接口ID
DeviceAddr string //接口下设备地址
DeviceType string
FunName string
FunIndex int
FunPara interface{}
}
var (
@ -52,24 +52,65 @@ func CommunicationManageDel(){
case cmd := <-emergencyRequestChan:
{
log.Println("emergency chan")
log.Printf("funName %s\n", cmd.funName)
log.Printf("funName %s\n", cmd.FunName)
var status bool = false
//for _,v := range DeviceNodeManageMap[cmd.interfaceID].DeviceNodeMap{
// switch v.(type) {
// case DeviceNodeTemplate:
// {
// if v.(DeviceNodeTemplate).Addr == cmd.deviceAddr{
// //fcu := v.(DeviceNodeTemplate)
//
// }
// }
// }
//}
for k,v := range DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap{
if v.Addr == cmd.DeviceAddr {
log.Printf("index is %d\n",k)
//--------------组包---------------------------
txBuf := DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].GenerateGetRealVariables(v.Addr)
log.Printf("tx buf is %+v\n",txBuf)
//---------------发送-------------------------
setting.SerialInterface.SerialPort[cmd.InterfaceID].Write(txBuf)
//---------------等待接收----------------------
//阻塞读
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(timeOut)*time.Millisecond)
for {
select{
//是否正确收到数据包
case <-DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].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)
status = true
goto LoopEmerg
}
//是否接收超时
case <-timer.C:
{
log.Println("rx timeout")
//通信帧延时
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
time.Sleep(time.Duration(interval)*time.Millisecond)
status = false
goto LoopEmerg
}
//继续接收数据
default:
{
rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf)
if rxBufCnt > 0{
rxTotalBufCnt += rxBufCnt
//追加接收的数据到接收缓冲区
rxTotalBuf = append(rxTotalBuf,rxBuf[:rxBufCnt]...)
//清除本地接收数据
rxBufCnt = 0
log.Printf("rxbuf %+v\n",rxTotalBuf)
}
}
}
}
LoopEmerg:
}
}
emergencyAckChan<- status
//通信帧延时
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[0].Interval)
time.Sleep(time.Duration(interval)*time.Millisecond)
}
default:
{
@ -79,31 +120,30 @@ func CommunicationManageDel(){
log.Println("common chan")
//log.Printf("funName %s\n", cmd.funName)
for k,v := range DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap{
if v.Addr == cmd.deviceAddr{
for k,v := range DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap{
if v.Addr == cmd.DeviceAddr {
log.Printf("index is %d\n",k)
//--------------组包---------------------------
txBuf := DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].GenerateGetRealVariables(v.Addr)
txBuf := DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].GenerateGetRealVariables(v.Addr)
log.Printf("tx buf is %+v\n",txBuf)
//---------------发送-------------------------
setting.SerialInterface.SerialPort[cmd.interfaceID].Write(txBuf)
setting.SerialInterface.SerialPort[cmd.InterfaceID].Write(txBuf)
//---------------等待接收----------------------
//阻塞读
rxBuf := make([]byte, 256)
rxTotalBuf := make([]byte,0)
rxBufCnt := 0
rxTotalBufCnt := 0
timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.interfaceID].Timeout)
timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Timeout)
timer := time.NewTimer(time.Duration(timeOut)*time.Millisecond)
for {
select{
//是否正确收到数据包
case <-DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].AnalysisRx(v.Addr,v.VariableMap,rxTotalBuf,rxTotalBufCnt):
case <-DeviceInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].AnalysisRx(v.Addr,v.VariableMap,rxTotalBuf,rxTotalBufCnt):
{
log.Println("rx ok")
//通信帧延时
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.interfaceID].Interval)
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
time.Sleep(time.Duration(interval)*time.Millisecond)
goto Loop
}
@ -112,14 +152,14 @@ func CommunicationManageDel(){
{
log.Println("rx timeout")
//通信帧延时
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.interfaceID].Interval)
interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval)
time.Sleep(time.Duration(interval)*time.Millisecond)
goto Loop
}
//继续接收数据
default:
{
rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.interfaceID].Read(rxBuf)
rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf)
if rxBufCnt > 0{
rxTotalBufCnt += rxBufCnt
//追加接收的数据到接收缓冲区
@ -143,18 +183,18 @@ func CommunicationManageDel(){
}
}
func CommunicationManageAddEmergencyTest(){
cmd := CommunicationCmd{}
cmd.interfaceID = InterFaceID1
cmd.deviceAddr = "2"
cmd.funName = "FCUGetRealData"
cmd.funPara = struct{
addr byte
}{0x02}
CommunicationManageAddEmergency(cmd)
}
//func CommunicationManageAddEmergencyTest(){
// cmd := CommunicationCmd{}
//
// cmd.interfaceID = InterFaceID1
// cmd.DeviceAddr = "2"
// cmd.funName = "FCUGetRealData"
// cmd.funPara = struct{
// addr byte
// }{0x02}
//
// CommunicationManageAddEmergency(cmd)
//}
func CommunicationManagePoll(){
@ -162,10 +202,10 @@ func CommunicationManagePoll(){
for i:=0;i<DeviceInterfaceMap[InterFaceID0].DeviceNodeCnt;i++{
cmd.interfaceID = InterFaceID0
cmd.deviceAddr = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Addr
cmd.deviceType = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Type
cmd.funName = "GetDeviceRealVariables"
cmd.InterfaceID = InterFaceID0
cmd.DeviceAddr = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Addr
cmd.DeviceType = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Type
cmd.FunName = "GetDeviceRealVariables"
CommunicationManageAdd(cmd)
}

View File

@ -392,3 +392,77 @@ func apiGetTemplate(context *gin.Context){
context.String(http.StatusOK, string(sJson))
}
/**
从缓存中获取设备变量
*/
func apiGetNodeVariableFromCache(context *gin.Context){
sID := context.Query("interfaceID")
sAddr := context.Query("addr")
aParam := &struct{
Code string
Message string
Data []api.VariableTemplate
}{}
iID,_ := strconv.Atoi(sID)
for k,v := range device.DeviceInterfaceMap[iID].DeviceNodeMap{
if v.Addr == sAddr{
aParam.Code = "0"
aParam.Message = ""
aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].VariableMap
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
}
aParam.Code = "1"
aParam.Message = "node is noexist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
/**
从设备中获取设备变量
*/
func apiGetNodeVariableFromDevice(context *gin.Context){
sID := context.Query("interfaceID")
sAddr := context.Query("addr")
aParam := &struct{
Code string
Message string
Data []api.VariableTemplate
}{}
iID,_ := strconv.Atoi(sID)
for k,v := range device.DeviceInterfaceMap[iID].DeviceNodeMap{
if v.Addr == sAddr{
cmd := device.CommunicationCmd{}
cmd.InterfaceID = device.InterFaceID0
cmd.DeviceAddr = v.Addr
cmd.FunName = "GenerateGetRealVariables"
if device.CommunicationManageAddEmergency(cmd) == true{
aParam.Code = "0"
aParam.Message = ""
aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].VariableMap
}else{
aParam.Code = "1"
aParam.Message = ""
aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].VariableMap
}
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
}
aParam.Code = "1"
aParam.Message = "node is noexist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}

View File

@ -96,6 +96,9 @@ func NewVariables() []api.VariableTemplate{
return VariableMap
}
/**
生成读变量的数据包
*/
func GenerateGetRealVariables(sAddr string) []byte{
addr,_ := strconv.Atoi(sAddr)