Skip to content

Commit a3e822f

Browse files
committed
optimize serial.go
1 parent 842cc88 commit a3e822f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

controllers/serial.go

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ var Serials map[string]io.ReadWriteCloser
1818

1919
var ReceiverWs = make(map[string]*websocket.Conn, 100)
2020

21+
//字符串每隔N个字符用某个字符串分割
22+
func SpliteN(src string, sep string, n int) (des string) {
23+
if len(src) == 0 {
24+
return ""
25+
}
26+
tempN := len(src) / n
27+
tempN2 := len(src) % n
28+
29+
temp := make([]string, tempN+1)
30+
if tempN == 0 {
31+
return src
32+
}
33+
var temp2 = make([]string, 0)
34+
for k, _ := range temp {
35+
if k == tempN {
36+
temp2 = append(temp2, src[n*k:(n*k+tempN2)])
37+
temp2 = append(temp2, sep)
38+
break
39+
}
40+
temp2 = append(temp2, src[n*k:(n*k+n)])
41+
temp2 = append(temp2, sep)
42+
}
43+
for _, v := range temp2 {
44+
des += v
45+
}
46+
return des
47+
}
2148
func init() {
2249
Serials = make(map[string]io.ReadWriteCloser)
2350

@@ -30,6 +57,7 @@ func init() {
3057
data := string(tmp[:n])
3158
if ReceiveType == 1 {
3259
data = hex.EncodeToString(tmp[:n])
60+
data = SpliteN(data, " ", 2)
3361
}
3462
msg := map[string]interface{}{"code": 0, "message": "serial receive success", "id": k, "data": data}
3563
err := v.WriteJSON(msg)

0 commit comments

Comments
 (0)