forked from goburrow/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciiudpclient.go
98 lines (85 loc) · 2.18 KB
/
asciiudpclient.go
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
// Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package modbus
import (
"fmt"
"log"
"net"
"sync"
"time"
)
const (
udpTimeout = 10 * time.Second
)
// ASCIIUDPClientHandler implements Packager and Transporter interface.
type ASCIIUDPClientHandler struct {
asciiPackager
asciiUDPTransporter
}
// NewASCIIUDPClientHandler allocates and initializes a ASCIIClientHandler.
// The address format is ip:port
func NewASCIIUDPClientHandler(address string) *ASCIIUDPClientHandler {
handler := &ASCIIUDPClientHandler{}
handler.Address = address
handler.Timeout = udpTimeout
return handler
}
// ASCIIUDPClient creates ASCII client with default handler and given connect string.
func ASCIIUDPClient(address string, SlaveID int) Client {
handler := NewASCIIUDPClientHandler(address)
return NewClient(handler)
}
func (mb *asciiUDPTransporter) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Printf(format, v...)
}
}
// asciiUDPTransporter implements Transporter interface.
type asciiUDPTransporter struct {
// Connect string
Address string
// Connect & Read timeout
Timeout time.Duration
// Transmission logger
Logger *log.Logger
// UDP "connection"
mu sync.Mutex
conn *net.UDPConn
}
func (mb *asciiUDPTransporter) Send(aduRequest []byte) (aduResponse []byte, err error) {
mb.mu.Lock()
defer mb.mu.Unlock()
// Make sure port is connected
if err = mb.connect(); err != nil {
return
}
// Send the request
mb.logf("modbus: sending %q\n", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
// Get the response
var length int
data := make([]byte, asciiMaxSize)
mb.conn.SetDeadline(time.Now().Add(mb.Timeout))
length, _, err = mb.conn.ReadFromUDP(data)
if err != nil {
fmt.Println(err)
return
}
aduResponse = data[0:length]
mb.logf("modbus: received %q\n", aduResponse)
return
}
func (mb *asciiUDPTransporter) connect() error {
if mb.conn == nil {
s, err := net.ResolveUDPAddr("udp4", mb.Address)
conn, err := net.DialUDP("udp4", nil, s)
if err != nil {
return err
}
mb.conn = conn
}
return nil
}