zhangqian
2023-09-16 560985f421189fde3963b8c5af7d7ee1312f125b
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package service
 
import (
    "apsClient/conf"
    "apsClient/constvar"
    "apsClient/model"
    "apsClient/pkg/logx"
    "apsClient/pkg/plc"
    "apsClient/pkg/plc/modbusx"
    "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 (
        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)
        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.CurrentTryTimes > plcConfig.MaxTryTimes {
        logx.Errorf("plc write try time beyond max try times, err: %v", plcConfig.CurrentErr)
        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)
 
        err = modbusx.Write(ipAddr, uint16(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)
        }
        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.CurrentTryTimes > plcConfig.MaxTryTimes {
        logx.Errorf("plc read try time beyond max try times, err: %v", plcConfig.CurrentErr)
        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)
        //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))
        if err != nil {
            plcConfig.CurrentErr = err
            return PlcReadDirect(plcConfig, address, dataLength, valueType)
        }
        switch valueType {
        case constvar.PlcStartAddressValueTypeString:
            return string(value), nil
        case constvar.PlcStartAddressValueTypeInt:
            if len(value) == 2 {
                return int(binary.BigEndian.Uint16(value)), 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
}