From 06cff5f59c72d36b745515d98b9e645e92bb91a3 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 23 八月 2023 20:14:23 +0800 Subject: [PATCH] 修改获取工艺参数等待超时,修改生产进度返回值参数 --- api/v1/plc.go | 27 +++++++++++---------------- 1 files changed, 11 insertions(+), 16 deletions(-) diff --git a/api/v1/plc.go b/api/v1/plc.go index 2a93a15..3f8238d 100644 --- a/api/v1/plc.go +++ b/api/v1/plc.go @@ -9,9 +9,9 @@ "apsClient/pkg/logx" "apsClient/pkg/plc" "apsClient/service" - "encoding/json" + "encoding/binary" + "fmt" "github.com/gin-gonic/gin" - "github.com/spf13/cast" ) type PlcApi struct{} @@ -45,39 +45,34 @@ valueType = pc.Type dataLength = pc.Length } - ipAddr = plcConfig.Address + ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) } + resp := new(response.ProductProgress) if startAddress == 0 || valueType == "" { - ctx.FailWithMsg(ecode.UnknownErr, "璇峰厛閰嶇疆PLC") + ctx.OkWithDetailed(resp) + logx.Warnf("璇峰厛閰嶇疆PLC") return } conn, err := plc.NewModbusConnection(ipAddr) - if conn != nil { - defer conn.Close() - } - if err != nil { - ctx.FailWithMsg(ecode.UnknownErr, "杩炴帴plc澶辫触") + ctx.OkWithDetailed(resp) logx.Errorf("GetProductProgress 杩炴帴plc澶辫触: %v", err.Error()) return } + defer conn.Close() rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength) if err != nil { - ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇plc鏁版嵁澶辫触") + ctx.OkWithDetailed(resp) logx.Errorf("GetProductProgress 鑾峰彇plc鏁版嵁澶辫触: %v", err.Error()) return } - var finishNumber interface{} - err = json.Unmarshal(rawData, &finishNumber) + resp.FinishNumber = int(binary.BigEndian.Uint16(rawData)) if err != nil { - ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触") + ctx.OkWithDetailed(resp) logx.Errorf("GetProductProgress 鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触: %v, data: %v, valueType:%v", err.Error(), rawData, valueType) return - } - resp := response.ProductProgress{ - FinishNumber: cast.ToInt(finishNumber), } ctx.OkWithDetailed(resp) } -- Gitblit v1.8.0