Skip to content

Commit f04d5cc

Browse files
committed
move spHandlerOpen to hub from serialport.go
1 parent fac0b10 commit f04d5cc

File tree

2 files changed

+86
-84
lines changed

2 files changed

+86
-84
lines changed

hub.go

+86
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package main
1717

1818
import (
19+
"bytes"
1920
"encoding/json"
2021
"fmt"
2122
"html"
@@ -30,6 +31,7 @@ import (
3031
"github.com/arduino/arduino-create-agent/tools"
3132
"github.com/arduino/arduino-create-agent/upload"
3233
log "github.com/sirupsen/logrus"
34+
"go.bug.st/serial"
3335
)
3436

3537
type hub struct {
@@ -280,6 +282,90 @@ func (h *hub) checkCmd(m []byte) {
280282
}
281283
}
282284

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+
283369
type logWriter struct {
284370
onWrite func([]byte)
285371
}

serialport.go

-84
Original file line numberDiff line numberDiff line change
@@ -277,90 +277,6 @@ func (p *serport) writerRaw() {
277277
p.OnMessage([]byte(msgstr))
278278
}
279279

280-
func (h *hub) spHandlerOpen(portname string, baud int, buftype string) {
281-
282-
log.Print("Inside spHandler")
283-
284-
var out bytes.Buffer
285-
286-
out.WriteString("Opening serial port ")
287-
out.WriteString(portname)
288-
out.WriteString(" at ")
289-
out.WriteString(strconv.Itoa(baud))
290-
out.WriteString(" baud")
291-
log.Print(out.String())
292-
293-
conf := &SerialConfig{Name: portname, Baud: baud, RtsOn: true}
294-
295-
mode := &serial.Mode{
296-
BaudRate: baud,
297-
}
298-
299-
sp, err := serial.Open(portname, mode)
300-
log.Print("Just tried to open port")
301-
if err != nil {
302-
//log.Fatal(err)
303-
log.Print("Error opening port " + err.Error())
304-
//h.broadcastSys <- []byte("Error opening port. " + err.Error())
305-
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
306-
307-
return
308-
}
309-
log.Print("Opened port successfully")
310-
//p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp}
311-
// we can go up to 256,000 lines of gcode in the buffer
312-
p := &serport{
313-
sendBuffered: make(chan string, 256000),
314-
sendNoBuf: make(chan []byte),
315-
sendRaw: make(chan string),
316-
portConf: conf,
317-
portIo: sp,
318-
portName: portname,
319-
BufferType: buftype,
320-
}
321-
322-
p.OnMessage = func(msg []byte) {
323-
h.broadcastSys <- msg
324-
}
325-
p.OnClose = func(port *serport) {
326-
h.serialPortList.MarkPortAsClosed(p.portName)
327-
h.serialPortList.List()
328-
}
329-
330-
var bw Bufferflow
331-
332-
switch buftype {
333-
case "timed":
334-
bw = NewBufferflowTimed(portname, h.broadcastSys)
335-
case "timedraw":
336-
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
337-
case "default":
338-
bw = NewBufferflowDefault(portname, h.broadcastSys)
339-
default:
340-
log.Panicf("unknown buffer type: %s", buftype)
341-
}
342-
343-
bw.Init()
344-
p.bufferwatcher = bw
345-
346-
h.serialHub.Register(p)
347-
defer h.serialHub.Unregister(p)
348-
349-
h.serialPortList.MarkPortAsOpened(portname)
350-
h.serialPortList.List()
351-
352-
// this is internally buffered thread to not send to serial port if blocked
353-
go p.writerBuffered()
354-
// this is thread to send to serial port regardless of block
355-
go p.writerNoBuf()
356-
// this is thread to send to serial port but with base64 decoding
357-
go p.writerRaw()
358-
359-
p.reader(buftype)
360-
361-
h.serialPortList.List()
362-
}
363-
364280
func (p *serport) Close() {
365281
p.isClosing.Store(true)
366282

0 commit comments

Comments
 (0)