-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
59 lines (50 loc) · 1.1 KB
/
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
package main
import (
"io"
"log"
"net"
)
type Iec104Client struct {
Host string
Port string
conn *net.TCPConn
//command chan []byte
}
type receiverHandler func([]byte)
func (c *Iec104Client) Connect() {
srvAddr := c.Host + ":" + c.Port
tcpAddr, err := net.ResolveTCPAddr("tcp", srvAddr)
if err != nil {
log.Fatal("ResolveTCPAddr failed:", err.Error())
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Fatal("Dial failed:", err.Error())
}
c.conn = conn
}
func (c *Iec104Client) addReceiverHandler(handle receiverHandler) {
go func() {
for {
buff := make([]byte, 1024)
size, err := c.conn.Read(buff)
if err != nil && err != io.EOF {
log.Println("Read error: ", err)
}
if size > 0 {
handle(buff[:size])
}
}
}()
}
func (c *Iec104Client) SendCommand(cmd []byte) {
sendCommand := []byte{0x68, byte(len(cmd))}
sendCommand = append(sendCommand, cmd...)
log.Printf("Send command: %x", sendCommand)
if _, err := c.conn.Write(sendCommand); err != nil {
log.Println("Write command error: ", err)
}
}
func (c *Iec104Client) Close() {
c.conn.Close()
}