-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp_client.go
112 lines (94 loc) · 2.68 KB
/
tcp_client.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
99
100
101
102
103
104
105
106
107
108
109
110
111
package scpi
import (
"context"
"fmt"
"net"
"strings"
"time"
)
// TCPClient is an implementation of the Client interface for TCP network connections.
type TCPClient struct {
conn *net.TCPConn
}
// NewTCPClient returns a new TCP client of a device controlled using SCPI commands.
func NewTCPClient(addr string, timeout time.Duration) (*TCPClient, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
d := net.Dialer{
Timeout: timeout,
}
conn, err := d.Dial("tcp", tcpAddr.String())
if err != nil {
return nil, err
}
client := &TCPClient{
conn: conn.(*net.TCPConn),
}
return client, nil
}
// Close implements the Client Close method.
func (c *TCPClient) Close() error {
return c.conn.Close()
}
// Exec implements the Client Exec method.
func (c *TCPClient) Exec(cmd string) error {
return c.ExecContext(context.Background(), cmd)
}
// ExecContext implements the Client ExecContext method.
func (c *TCPClient) ExecContext(ctx context.Context, cmd string) error {
if err := c.exec(ctx, cmd); err != nil {
return fmt.Errorf("failed to execute the command '%s': %s", cmd, err)
}
return c.queryError(ctx, cmd)
}
func (c *TCPClient) exec(ctx context.Context, cmd string) error {
b := []byte(cmd + "\n")
if _, err := c.conn.Write(b); err != nil {
return err
}
return nil
}
func (c *TCPClient) queryError(ctx context.Context, prevCmd string) error {
res, err := c.QueryContext(ctx, "SYST:ERR?")
if err != nil {
return err
}
return confirmError(prevCmd, res)
}
// BulkExec implements the Client BulkExec method.
func (c *TCPClient) BulkExec(cmds ...string) error {
return c.BulkExecContext(context.Background(), cmds...)
}
// BulkExecContext implements the Client BulkExecContext method.
func (c *TCPClient) BulkExecContext(ctx context.Context, cmds ...string) error {
cmd := strings.Join(cmds, ";")
return c.ExecContext(ctx, cmd)
}
// Ping implements the Client Ping method.
func (c *TCPClient) Ping() error {
return c.PingContext(context.Background())
}
// PingContext implements the Client PingContext method.
func (c *TCPClient) PingContext(ctx context.Context) error {
// BUG(scizorman): PingContext is not implemented yet.
return nil
}
// Query implements the Client Query method.
func (c *TCPClient) Query(cmd string) (res string, err error) {
return c.QueryContext(context.Background(), cmd)
}
// QueryContext implements the Client QueryContext method.
func (c *TCPClient) QueryContext(ctx context.Context, cmd string) (res string, err error) {
if err := c.exec(ctx, cmd); err != nil {
return "", err
}
buf := make([]byte, 1024)
l, err := c.conn.Read(buf)
if err != nil {
return "", err
}
res = string(buf[:l])
return res, nil
}