zhangqian
2023-09-16 98ea38910b2795c86023c54b83d70030c7bb08c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package service
 
import (
    "apsClient/conf"
    "apsClient/constvar"
    "apsClient/model"
    "apsClient/pkg/logx"
    "apsClient/pkg/plc/apacheplc4x"
    "apsClient/pkg/plccom"
    "encoding/binary"
    "errors"
    "fmt"
    "github.com/spf13/cast"
)
 
func PlcWrite(plcConfig *model.DevicePlc, fieldType constvar.PlcStartAddressType, position int, value interface{}) (err error) {
    var (
        startAddress int
        ipAddr       string
    )
 
    if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes {
        return plcConfig.CurrentErr
    }
    plcConfig.CurrentTryTimes++
 
    for _, pc := range plcConfig.Details {
        if pc.FieldName == fieldType && pc.Position == position {
            startAddress = pc.StartAddress
        }
    }
 
    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)
        //}
        //
        //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", startAddress, value)
    } else if plcConfig.Method == constvar.PlcMethodSerial {
        ipAddr = conf.Conf.Services.Serial
        if ipAddr == "" {
            return errors.New("conf.Conf.Services.Serial config not set yet")
        }
        label := fmt.Sprintf("D%d", startAddress)
        return plccom.WritePLC(plccom.DeviceTypeMitsubishi, ipAddr, label, cast.ToInt(value))
    }
    return
}
 
func PlcWriteDirect(plcConfig *model.DevicePlc, address int, value interface{}) (err error) {
    var (
        ipAddr string
    )
    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)
 
        //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())
            return err
        }
        logx.Infof("plc write ok, address: %v, value: %v", address, value)
    } else if plcConfig.Method == constvar.PlcMethodSerial {
        ipAddr = conf.Conf.Services.Serial
        if ipAddr == "" {
            return errors.New("conf.Conf.Services.Serial config not set yet")
        }
        label := fmt.Sprintf("D%d", address)
        return plccom.WritePLC(plccom.DeviceTypeMitsubishi, ipAddr, label, cast.ToInt(value))
    }
    return
}
 
func PlcReadDirect(plcConfig *model.DevicePlc, address int, dataLength int, valueType constvar.PlcStartAddressValueType) (val interface{}, err error) {
    var (
        ipAddr string
    )
    if plcConfig.Method == constvar.PlcMethodModbusTCP {
        ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
        //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 {
            return nil, err
        }
        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 {
        ipAddr = conf.Conf.Services.Serial
        if ipAddr == "" {
            return nil, errors.New("conf.Conf.Services.Serial config not set yet")
        }
        label := fmt.Sprintf("D%d", address)
        return plccom.ReadPLC(plccom.DeviceTypeMitsubishi, ipAddr, label)
    }
    return
}