-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialClient.go
94 lines (77 loc) · 2.08 KB
/
SerialClient.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
package main
import (
"fmt"
"github.com/tarm/serial"
"log"
"net"
"os"
"runtime"
)
func main() {
fmt.Println("starting the server")
//key := pbkdf2.Key([]byte("demo pass"), []byte("demo salt"), 1024, 32, sha1.New)
//block, _ := kcp.NewAESBlockCrypt(key)
var remoteAddress, _ = net.ResolveTCPAddr("tcp4", "47.101.214.72:9010") //生成一个net.TcpAddr对像。
var conn, err = net.DialTCP("tcp4", nil, remoteAddress) //传入协议,本机地址(传了nil),远程地址,获取连接。
if err != nil { //如果连接失败。则返回。
fmt.Println("连接出错:", err)
return
}
c := &serial.Config{Name: "COM1", Baud: 115200}
serial, _ := serial.OpenPort(c)
go func() {
buf := make([]byte, 512)
for {
len, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading", err.Error())
return //终止程序
}
bytetoserial_TCP(serial, buf[:len])
}
}()
//接收串口来的数据,通过TCP发送回PC
go func() {
buf := make([]byte, 512)
for {
data := serialtobyte_TCP(serial, buf)
_, err = conn.Write(data)
if err != nil {
log.Println(err)
return
}
fmt.Printf("[SEVER] Serial->TCP: %v\n", string(data))
}
}()
go runtime.GC()
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}
func bytetoserial_TCP(serialport *serial.Port, usrdataget []byte) {
//log.Println("bytetoserial")
d := usrdataget
_, err := serialport.Write(d)
if err != nil {
log.Fatal(err)
}
d = nil
return
}
//从串口读取数据并存至通道缓存
func serialtobyte_TCP(serialport *serial.Port, dserialtobyte []byte) (data []byte) {
//func serialtobyte(serialport *serial.Port, dserialtobyte chan []byte) {
//log.Println("serialtobyte")
serialbuf := make([]byte, 256)
n, err := serialport.Read(serialbuf)
if err != nil {
serialtobyte_TCP(serialport, dserialtobyte)
}
// fmt.Println("serial to byte:", string(serialbuf[:n]))
//dserialtobyte <- serialbuf[:n]
data = serialbuf[:n]
return data
}