@@ -29,25 +29,10 @@ import (
29
29
"github.com/sirupsen/logrus"
30
30
)
31
31
32
- type writeRequest struct {
33
- p * serport
34
- d string
35
- buffer string
36
- }
37
-
38
32
type serialhub struct {
39
33
// Opened serial ports.
40
34
ports map [* serport ]bool
41
35
42
- //write chan *serport, chan []byte
43
- write chan writeRequest
44
-
45
- // Register requests from the connections.
46
- register chan * serport
47
-
48
- // Unregister requests from connections.
49
- unregister chan * serport
50
-
51
36
mu sync.Mutex
52
37
}
53
38
@@ -75,46 +60,39 @@ type SpPortItem struct {
75
60
var serialPorts SerialPortList
76
61
77
62
var sh = serialhub {
78
- //write: make(chan *serport, chan []byte),
79
- write : make (chan writeRequest ),
80
- register : make (chan * serport ),
81
- unregister : make (chan * serport ),
82
- ports : make (map [* serport ]bool ),
63
+ ports : make (map [* serport ]bool ),
83
64
}
84
65
85
- func (sh * serialhub ) run () {
86
-
87
- //log.Print("Inside run of serialhub")
88
- //cmdIdCtr := 0
89
-
90
- for {
91
- select {
92
- case p := <- sh .register :
93
- sh .mu .Lock ()
94
- //log.Print("Registering a port: ", p.portConf.Name)
95
- h .broadcastSys <- []byte ("{\" Cmd\" :\" Open\" ,\" Desc\" :\" Got register/open on port.\" ,\" Port\" :\" " + p .portConf .Name + "\" ,\" Baud\" :" + strconv .Itoa (p .portConf .Baud ) + ",\" BufferType\" :\" " + p .BufferType + "\" }" )
96
- sh .ports [p ] = true
97
- sh .mu .Unlock ()
98
- case p := <- sh .unregister :
99
- sh .mu .Lock ()
100
- //log.Print("Unregistering a port: ", p.portConf.Name)
101
- h .broadcastSys <- []byte ("{\" Cmd\" :\" Close\" ,\" Desc\" :\" Got unregister/close on port.\" ,\" Port\" :\" " + p .portConf .Name + "\" ,\" Baud\" :" + strconv .Itoa (p .portConf .Baud ) + "}" )
102
- delete (sh .ports , p )
103
- close (p .sendBuffered )
104
- close (p .sendNoBuf )
105
- sh .mu .Unlock ()
106
- case wr := <- sh .write :
107
- // if user sent in the commands as one text mode line
108
- switch wr .buffer {
109
- case "send" :
110
- wr .p .sendBuffered <- wr .d
111
- case "sendnobuf" :
112
- wr .p .sendNoBuf <- []byte (wr .d )
113
- case "sendraw" :
114
- wr .p .sendRaw <- wr .d
115
- }
116
- // no default since we alredy verified in spWrite()
117
- }
66
+ // Register serial ports from the connections.
67
+ func (sh * serialhub ) Register (port * serport ) {
68
+ sh .mu .Lock ()
69
+ //log.Print("Registering a port: ", p.portConf.Name)
70
+ h .broadcastSys <- []byte ("{\" Cmd\" :\" Open\" ,\" Desc\" :\" Got register/open on port.\" ,\" Port\" :\" " + port .portConf .Name + "\" ,\" Baud\" :" + strconv .Itoa (port .portConf .Baud ) + ",\" BufferType\" :\" " + port .BufferType + "\" }" )
71
+ sh .ports [port ] = true
72
+ sh .mu .Unlock ()
73
+ }
74
+
75
+ // Unregister requests from connections.
76
+ func (sh * serialhub ) Unregister (port * serport ) {
77
+ sh .mu .Lock ()
78
+ //log.Print("Unregistering a port: ", p.portConf.Name)
79
+ h .broadcastSys <- []byte ("{\" Cmd\" :\" Close\" ,\" Desc\" :\" Got unregister/close on port.\" ,\" Port\" :\" " + port .portConf .Name + "\" ,\" Baud\" :" + strconv .Itoa (port .portConf .Baud ) + "}" )
80
+ delete (sh .ports , port )
81
+ close (port .sendBuffered )
82
+ close (port .sendNoBuf )
83
+ sh .mu .Unlock ()
84
+ }
85
+
86
+ // Write data to the serial port.
87
+ func (sh * serialhub ) Write (port * serport , data string , sendMode string ) {
88
+ // if user sent in the commands as one text mode line
89
+ switch sendMode {
90
+ case "send" :
91
+ port .sendBuffered <- data
92
+ case "sendnobuf" :
93
+ port .sendNoBuf <- []byte (data )
94
+ case "sendraw" :
95
+ port .sendRaw <- data
118
96
}
119
97
}
120
98
@@ -296,38 +274,30 @@ func spWrite(arg string) {
296
274
spErr (errstr )
297
275
return
298
276
}
277
+ bufferingMode := args [0 ]
299
278
portname := strings .Trim (args [1 ], " " )
300
- //log.Println("The port to write to is:" + portname + "---")
301
- //log.Println("The data is:" + args[2] + "---")
279
+ data := args [2 ]
302
280
303
- // see if we have this port open
304
- myport , isFound := sh . FindPortByName ( portname )
281
+ //log.Println("The port to write to is:" + portname + "---")
282
+ //log.Println("The data is:" + data + "---" )
305
283
306
- if ! isFound {
284
+ // See if we have this port open
285
+ port , ok := sh .FindPortByName (portname )
286
+ if ! ok {
307
287
// we couldn't find the port, so send err
308
288
spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
309
289
return
310
290
}
311
291
312
- // we found our port
313
- // create our write request
314
- var wr writeRequest
315
- wr .p = myport
316
-
317
- // see if args[0] is send or sendnobuf or sendraw
318
- switch args [0 ] {
292
+ // see if bufferingMode is valid
293
+ switch bufferingMode {
319
294
case "send" , "sendnobuf" , "sendraw" :
320
- wr . buffer = args [ 0 ]
295
+ // valid buffering mode, go ahead
321
296
default :
322
297
spErr ("Unsupported send command:" + args [0 ] + ". Please specify a valid one" )
323
298
return
324
299
}
325
300
326
- // include newline or not in the write? that is the question.
327
- // for now lets skip the newline
328
- //wr.d = []byte(args[2] + "\n")
329
- wr .d = args [2 ] //[]byte(args[2])
330
-
331
301
// send it to the write channel
332
- sh .write <- wr
302
+ sh .Write ( port , data , bufferingMode )
333
303
}
0 commit comments