From ef7d02ce56e98e8cbc5ed6ec746ea39bf0b8c9e2 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 16 九月 2023 22:44:58 +0800
Subject: [PATCH] 增加实时获取生产进度接口

---
 service/plc.go |  145 +++++++++++++++++------------------------------
 1 files changed, 53 insertions(+), 92 deletions(-)

diff --git a/service/plc.go b/service/plc.go
index 0e5e49f..16c695c 100644
--- a/service/plc.go
+++ b/service/plc.go
@@ -5,66 +5,13 @@
 	"apsClient/constvar"
 	"apsClient/model"
 	"apsClient/pkg/logx"
-	"apsClient/pkg/plc"
+	"apsClient/pkg/plc/apacheplc4x"
 	"apsClient/pkg/plccom"
 	"encoding/binary"
 	"errors"
 	"fmt"
 	"github.com/spf13/cast"
 )
-
-func PlcRead(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, position int) (val interface{}, err error) {
-	var (
-		startAddress int
-		valueType    constvar.PlcStartAddressValueType
-		dataLength   int
-		ipAddr       string
-	)
-
-	for _, pc := range plcConfig.Details {
-		if pc.FieldName == fieldType && pc.Position == position {
-			startAddress = pc.StartAddress
-			valueType = pc.Type
-			dataLength = pc.Length
-		}
-	}
-
-	if plcConfig.Method == constvar.PlcMethodModbusTCP {
-		ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
-
-		conn, err := plc.GetModbusConnection(ipAddr)
-		if err != nil {
-			logx.Errorf("PlcRead 杩炴帴plc澶辫触: %v", err.Error())
-			return nil, err
-		}
-
-		rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength)
-		if err != nil {
-			logx.Errorf("PlcRead 鑾峰彇plc鏁版嵁澶辫触: %v", err.Error())
-			return nil, err
-		}
-		switch valueType {
-		case constvar.PlcStartAddressValueTypeString:
-			return string(rawData), nil
-		case constvar.PlcStartAddressValueTypeInt:
-			if len(rawData) == 2 {
-				return int(binary.BigEndian.Uint16(rawData)), nil
-			} else {
-				logx.Errorf("plc read get an unknown int value: %v, address:%v", rawData, startAddress)
-				return nil, errors.New(fmt.Sprintf("unknown int value锛�%v", rawData))
-			}
-		}
-		return nil, errors.New("undefined value type")
-	} else if plcConfig.Method == constvar.PlcMethodSerial {
-		ipAddr = conf.Conf.Services.Serial
-		if ipAddr == "" {
-			return nil, errors.New("conf.Conf.Services.Serial config not set yet")
-		}
-		label := fmt.Sprintf("D%d", startAddress)
-		return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label)
-	}
-	return nil, errors.New("interface type not support")
-}
 
 func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, position int, value interface{}) (err error) {
 	var (
@@ -85,20 +32,25 @@
 
 	if plcConfig.Method == constvar.PlcMethodModbusTCP {
 		ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
-		conn, err := plc.GetModbusConnection(ipAddr)
-		if err != nil {
-			logx.Errorf("plc write failed, 杩炴帴plc澶辫触: %v", err.Error())
-			plcConfig.CurrentErr = err
-			return PlcWrite(plcConfig, fieldType, position, value)
-		}
+		//conn, err := plc.GetModbusConnection(ipAddr)
+		//if err != nil {
+		//	logx.Errorf("plc write failed, 杩炴帴plc澶辫触: %v", err.Error())
+		//	plcConfig.CurrentErr = err
+		//	return PlcWrite(plcConfig, fieldType, position, value)
+		//}
+		//
+		//result, err := plc.WriteHoldingRegister(conn, startAddress, value)
 
-		result, err := plc.WriteHoldingRegister(conn, startAddress, value)
+		//err = modbusx.Write(ipAddr, uint16(startAddress), value)
+
+		_, err = apacheplc4x.WriteHoldingRegister(ipAddr, startAddress, value)
+
 		if err != nil {
 			logx.Errorf("plc write failed, address: %v, value: %v, err: %v", startAddress, value, err.Error())
 			plcConfig.CurrentErr = err
 			return PlcWrite(plcConfig, fieldType, position, value)
 		}
-		logx.Infof("plc write ok, address: %v, value: %v, result: %v", startAddress, value, result)
+		logx.Infof("plc write ok, address: %v, value: %v", startAddress, value)
 	} else if plcConfig.Method == constvar.PlcMethodSerial {
 		ipAddr = conf.Conf.Services.Serial
 		if ipAddr == "" {
@@ -114,26 +66,25 @@
 	var (
 		ipAddr string
 	)
-
-	if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes {
-		return plcConfig.CurrentErr
-	}
-	plcConfig.CurrentTryTimes++
 	if plcConfig.Method == constvar.PlcMethodModbusTCP {
 		ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
-		conn, err := plc.GetModbusConnection(ipAddr)
-		if err != nil {
-			logx.Errorf("plc write failed, 杩炴帴plc澶辫触: %v", err.Error())
-			plcConfig.CurrentErr = err
-			return PlcWriteDirect(plcConfig, address, value)
-		}
-		result, err := plc.WriteHoldingRegister(conn, address, value)
+		//conn, err := plc.GetModbusConnection(ipAddr)
+		//if err != nil {
+		//	logx.Errorf("plc write failed, 杩炴帴plc澶辫触: %v", err.Error())
+		//	plcConfig.CurrentErr = err
+		//	return PlcWriteDirect(plcConfig, address, value)
+		//}
+		//result, err := plc.WriteHoldingRegister(conn, address, value)
+
+		//err = modbusx.Write(ipAddr, uint16(address), value)
+
+		_, err = apacheplc4x.WriteHoldingRegister(ipAddr, address, value)
+
 		if err != nil {
 			logx.Errorf("plc write failed, address: %v, value: %v, err: %v", address, value, err.Error())
-			plcConfig.CurrentErr = err
-			return PlcWriteDirect(plcConfig, address, value)
+			return err
 		}
-		logx.Infof("plc write ok, address: %v, value: %v, result: %v", address, value, result)
+		logx.Infof("plc write ok, address: %v, value: %v", address, value)
 	} else if plcConfig.Method == constvar.PlcMethodSerial {
 		ipAddr = conf.Conf.Services.Serial
 		if ipAddr == "" {
@@ -145,28 +96,38 @@
 	return
 }
 
-func PlcReadDirect(plcConfig *model.DevicePlc, address int, dataLength int) (val interface{}, err error) {
+func PlcReadDirect(plcConfig *model.DevicePlc, address int, dataLength int, valueType constvar.PlcStartAddressValueType) (val interface{}, err error) {
 	var (
 		ipAddr string
 	)
-
-	if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes {
-		return nil, plcConfig.CurrentErr
-	}
-	plcConfig.CurrentTryTimes++
 	if plcConfig.Method == constvar.PlcMethodModbusTCP {
 		ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
-		conn, err := plc.GetModbusConnection(ipAddr)
+		//conn, err := plc.GetModbusConnection(ipAddr)
+		//if err != nil {
+		//	plcConfig.CurrentErr = err
+		//	return PlcReadDirect(plcConfig, address, dataLength, valueType)
+		//}
+		//value, err := plc.ReadHoldingRegister(conn, address, dataLength)
+
+		//value, err := modbusx.Read(ipAddr, uint16(address), uint16(dataLength))
+
+		value, err := apacheplc4x.ReadHoldingRegister(ipAddr, address, dataLength)
+
 		if err != nil {
-			logx.Errorf("plc write failed, 杩炴帴plc澶辫触: %v", err.Error())
-			plcConfig.CurrentErr = err
-			return PlcReadDirect(plcConfig, address, dataLength)
+			return nil, err
 		}
-		value, err := plc.ReadHoldingRegister(conn, address, dataLength)
-		if err != nil {
-			logx.Errorf("plc read failed, address: %v, err: %v", address, err.Error())
-			plcConfig.CurrentErr = err
-			return PlcReadDirect(plcConfig, address, dataLength)
+		switch valueType {
+		case constvar.PlcStartAddressValueTypeString:
+			return string(value), nil
+		case constvar.PlcStartAddressValueTypeInt:
+			if len(value) == 2 {
+				return int(binary.BigEndian.Uint16(value)), nil
+			} else if len(value) == 4 {
+				return int32(value[2])<<8 + int32(value[3]), nil
+			} else {
+				logx.Errorf("plc read get an unknown int value: %v, address:%v", value, address)
+				return nil, errors.New(fmt.Sprintf("unknown int value锛�%v", value))
+			}
 		}
 		logx.Infof("plc read ok, address: %v, result: %v", address, value)
 	} else if plcConfig.Method == constvar.PlcMethodSerial {

--
Gitblit v1.8.0