-
Notifications
You must be signed in to change notification settings - Fork 2
/
Packager.go
46 lines (40 loc) · 1.24 KB
/
Packager.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
package modbus
import "errors"
// Transporter is the underlying connection interface. This is used to store
// either a TCP connection or a serial/comm port.
type Transporter interface {
Write([]byte) (int, error)
Read([]byte) (int, error)
Close() error
}
// Packager generates the raw bytes of a Modbus packet for a given Query,
// transmits the Query on the underlying Transporter interface, and returns and
// parses the response data. A Packager is implemented for the three modbus
// Modes: ASCIIPackager, RTUPackager and TCPPackager.
type Packager interface {
Send(q Query) ([]byte, error)
Transporter
SetDebug(debug bool)
}
// PackagerSettings holds settings and data that all packagers use.
// Packagers subclass this struct and implement the Packager interface for
// their respective Modbus protocols.
type packagerSettings struct {
Debug bool
}
func (ps *packagerSettings) SetDebug(debug bool) {
ps.Debug = debug
}
// NewPackager returns a Packager according to the cs.Mode.
func NewPackager(cs ConnectionSettings) (Packager, error) {
switch cs.Mode {
case ModeTCP:
return NewTCPPackager(cs)
case ModeRTU:
return NewRTUPackager(cs)
case ModeASCII:
return NewASCIIPackager(cs)
default:
return nil, errors.New("Invalid Mode")
}
}