zhangqian
2023-08-25 48ef530180aeec4f91517e2b86ef75745c28241c
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
package plc
 
import (
    "errors"
    "fmt"
    apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
    "github.com/apache/plc4x/plc4go/spi/options"
    "sync"
    "time"
 
    plc4go "github.com/apache/plc4x/plc4go/pkg/api"
    "github.com/apache/plc4x/plc4go/pkg/api/drivers"
    "github.com/apache/plc4x/plc4go/pkg/api/transports"
)
 
type ConnectionManager struct {
    connections map[string]plc4go.PlcConnection
    mu          sync.Mutex
}
 
func NewPlcConnectionManager() *ConnectionManager {
    return &ConnectionManager{
        connections: make(map[string]plc4go.PlcConnection),
    }
}
 
func (cm *ConnectionManager) GetConnection(address string) (plc4go.PlcConnection, bool) {
    cm.mu.Lock()
    defer cm.mu.Unlock()
 
    conn, ok := cm.connections[address]
    return conn, ok
}
 
var connectionManager = NewPlcConnectionManager()
 
func (cm *ConnectionManager) AddConnection(address string, connection plc4go.PlcConnection) {
    cm.mu.Lock()
    defer cm.mu.Unlock()
 
    cm.connections[address] = connection
}
func GetModbusConnection(ipAddr string) (plc4go.PlcConnection, error) {
    if conn, ok := connectionManager.GetConnection(ipAddr); ok {
        if conn.IsConnected() {
            return conn, nil
        }
    }
 
    // 创建驱动管理器
    option := options.WithReceiveTimeout(time.Second * 5) //五秒超时
    driverManager := plc4go.NewPlcDriverManager(option)
    // 注册TCP传输
    transports.RegisterTcpTransport(driverManager)
 
    // 注册驱动
    //drivers.RegisterKnxDriver(driverManager)
    drivers.RegisterModbusTcpDriver(driverManager)
 
    // 通过TCP连接PLC设备
    connectionString := fmt.Sprintf("modbus-tcp://%s", ipAddr)
    connectionRequestChanel := driverManager.GetConnection(connectionString)
 
    // 等待连接响应
    connectionResult := <-connectionRequestChanel
 
    // 判断是否连接成功
    if err := connectionResult.GetErr(); err != nil {
        return nil, err
    }
 
    conn := connectionResult.GetConnection()
    connectionManager.AddConnection(ipAddr, conn)
    return conn, nil
}
func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
    tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
 
    // 读模式
    readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
    if err != nil {
        fmt.Printf("preparing read-request:%s\n", err.Error())
        return nil, err
    }
 
    // 执行
    readResult := <-readRequest.Execute()
    if err := readResult.GetErr(); err != nil {
        fmt.Printf("execting read-request:%s\n", err.Error())
        return nil, err
    }
 
    // 判断响应码是否正确
    if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
        fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName())
        return nil, nil
    }
 
    value := readResult.GetResponse().GetValue("tag")
 
    return value.GetRaw(), err
 
}
 
func ReadHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
    tagAddress := fmt.Sprintf("holding-register:%d:UINT[%d]", address, length)
 
    // 读模式
    readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
    if err != nil {
        fmt.Printf("preparing read-request:%s\n", err.Error())
        return nil, err
    }
 
    // 执行
    readResult := <-readRequest.Execute()
    if err := readResult.GetErr(); err != nil {
        fmt.Printf("execting read-request:%s\n", err.Error())
        return nil, err
    }
 
    // 判断响应码是否正确
    if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
        fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName())
        return nil, errors.New("error  code: " + readResult.GetResponse().GetResponseCode("tag").GetName())
    }
 
    value := readResult.GetResponse().GetValue("tag")
 
    var result []byte
 
    for _, val := range value.GetList() {
        result = append(result, val.GetRaw()...)
    }
 
    return result, nil
}
 
func ReadHoldingRegister(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
    if length > 1 {
        return ReadHoldingRegisterList(connection, address, length)
    }
 
    return ReadHoldingRegisterSingle(connection, address)
}
 
func WriteHoldingRegister(connection plc4go.PlcConnection, address int, value any) (string, error) {
    tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
 
    // 写模式
    writeRequest, err := connection.WriteRequestBuilder().AddTagAddress("tag", tagAddress, value).Build()
    if err != nil {
        fmt.Printf("preparing read-request:%s\n", err.Error())
        return "", err
    }
 
    // 执行
    writeResult := <-writeRequest.Execute()
    if err := writeResult.GetErr(); err != nil {
        fmt.Printf("execting read-request:%s\n", err.Error())
        return "", err
    }
 
    // 判断响应码是否正确
    if writeResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
        fmt.Printf("error an non-ok return code: %s", writeResult.GetResponse().GetResponseCode("tag").GetName())
        return "", errors.New("error  code: " + writeResult.GetResponse().GetResponseCode("tag").GetName())
    }
 
    result := writeResult.GetResponse().String()
 
    return result, nil
}