16
16
package main
17
17
18
18
import (
19
+ "bytes"
19
20
"encoding/json"
20
21
"fmt"
21
22
"html"
@@ -30,6 +31,7 @@ import (
30
31
"github.com/arduino/arduino-create-agent/tools"
31
32
"github.com/arduino/arduino-create-agent/upload"
32
33
log "github.com/sirupsen/logrus"
34
+ "go.bug.st/serial"
33
35
)
34
36
35
37
type hub struct {
@@ -280,6 +282,90 @@ func (h *hub) checkCmd(m []byte) {
280
282
}
281
283
}
282
284
285
+ func (h * hub ) spHandlerOpen (portname string , baud int , buftype string ) {
286
+
287
+ log .Print ("Inside spHandler" )
288
+
289
+ var out bytes.Buffer
290
+
291
+ out .WriteString ("Opening serial port " )
292
+ out .WriteString (portname )
293
+ out .WriteString (" at " )
294
+ out .WriteString (strconv .Itoa (baud ))
295
+ out .WriteString (" baud" )
296
+ log .Print (out .String ())
297
+
298
+ conf := & SerialConfig {Name : portname , Baud : baud , RtsOn : true }
299
+
300
+ mode := & serial.Mode {
301
+ BaudRate : baud ,
302
+ }
303
+
304
+ sp , err := serial .Open (portname , mode )
305
+ log .Print ("Just tried to open port" )
306
+ if err != nil {
307
+ //log.Fatal(err)
308
+ log .Print ("Error opening port " + err .Error ())
309
+ //h.broadcastSys <- []byte("Error opening port. " + err.Error())
310
+ h .broadcastSys <- []byte ("{\" Cmd\" :\" OpenFail\" ,\" Desc\" :\" Error opening port. " + err .Error () + "\" ,\" Port\" :\" " + conf .Name + "\" ,\" Baud\" :" + strconv .Itoa (conf .Baud ) + "}" )
311
+
312
+ return
313
+ }
314
+ log .Print ("Opened port successfully" )
315
+ //p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp}
316
+ // we can go up to 256,000 lines of gcode in the buffer
317
+ p := & serport {
318
+ sendBuffered : make (chan string , 256000 ),
319
+ sendNoBuf : make (chan []byte ),
320
+ sendRaw : make (chan string ),
321
+ portConf : conf ,
322
+ portIo : sp ,
323
+ portName : portname ,
324
+ BufferType : buftype ,
325
+ }
326
+
327
+ p .OnMessage = func (msg []byte ) {
328
+ h .broadcastSys <- msg
329
+ }
330
+ p .OnClose = func (port * serport ) {
331
+ h .serialPortList .MarkPortAsClosed (p .portName )
332
+ h .serialPortList .List ()
333
+ }
334
+
335
+ var bw Bufferflow
336
+
337
+ switch buftype {
338
+ case "timed" :
339
+ bw = NewBufferflowTimed (portname , h .broadcastSys )
340
+ case "timedraw" :
341
+ bw = NewBufferflowTimedRaw (portname , h .broadcastSys )
342
+ case "default" :
343
+ bw = NewBufferflowDefault (portname , h .broadcastSys )
344
+ default :
345
+ log .Panicf ("unknown buffer type: %s" , buftype )
346
+ }
347
+
348
+ bw .Init ()
349
+ p .bufferwatcher = bw
350
+
351
+ h .serialHub .Register (p )
352
+ defer h .serialHub .Unregister (p )
353
+
354
+ h .serialPortList .MarkPortAsOpened (portname )
355
+ h .serialPortList .List ()
356
+
357
+ // this is internally buffered thread to not send to serial port if blocked
358
+ go p .writerBuffered ()
359
+ // this is thread to send to serial port regardless of block
360
+ go p .writerNoBuf ()
361
+ // this is thread to send to serial port but with base64 decoding
362
+ go p .writerRaw ()
363
+
364
+ p .reader (buftype )
365
+
366
+ h .serialPortList .List ()
367
+ }
368
+
283
369
type logWriter struct {
284
370
onWrite func ([]byte )
285
371
}
0 commit comments