From 9bb9f2576852af9ffcec4a2b62ea6d6f948bd24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kontor=20Krist=C3=B3f?= Date: Tue, 6 Sep 2016 16:16:49 +0200 Subject: [PATCH] Adding extra open parameters: dataBits parity stopBits --- README.md | 1 + hub.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++---- serialport.go | 9 +++++-- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5a94c94..0eea6b8 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ Command | Example | Description ------- | ------- | ------- list | | Lists all available serial ports on your device open portName baudRate [bufferAlgorithm] | open /dev/ttyACM0 115200 tinyg | Opens a serial port. The comPort should be the Name of the port inside the list response such as COM2 or /dev/ttyACM0. The baudrate should be a rate from the baudrates command or a typical baudrate such as 9600 or 115200. A bufferAlgorithm can be optionally specified such as "tinyg" (or in the future "grbl" if somebody writes it) or write your own. +open portName baudRate dataBits parity stopBits [bufferAlgorithm] | open COM4 9600 7 EVEN 1 tinyg | detailed open command where you can set 3 extra parameters: dataBits (usually: 7 | 8) parity (NONE | ODD | EVEN | MARK | SPACE) stopBits (1 | 1.5 | 2) sendjson {} | {"P":"COM22","Data":[{"D":"!~\n","Id":"234"},{"D":"{\"sr\":\"\"}\n","Id":"235"}]} | See Wiki page at https://github.com/johnlauer/serial-port-json-server/wiki send portName data | send /dev/ttyACM0 G1 X10.5 Y2 F100\n | Send your data to the serial port. Remember to send a newline in your data if your serial port expects it. sendnobuf portName data | send COM22 {"qv":0}\n | Send your data and bypass the bufferFlowAlgorithm if you specified one. diff --git a/hub.go b/hub.go index 7a9f147..c39ee4c 100755 --- a/hub.go +++ b/hub.go @@ -54,7 +54,7 @@ func (h *hub) run() { h.connections[c] = true // send supported commands c.send <- []byte("{\"Version\" : \"" + version + "\"} ") - c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud] [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"sendjson {P:portName, Data:[{D:cmdStr, Id:idStr}]}\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"broadcast [anythingToRegurgitate]\", \"hostname\", \"version\", \"program [portName] [core:architecture:name] [path/to/binOrHexFile]\", \"programfromurl [portName] [core:architecture:name] [urlToBinOrHexFile]\", \"execruntime\", \"exec [command] [arg1] [arg2] [...]\"]} ") + c.send <- []byte("{\"Commands\" : [\"list\", \"open portName baud [bufferAlgorithm (optional)]\", \"open portName baud databits parity stopbits [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"sendjson {P:portName, Data:[{D:cmdStr, Id:idStr}]}\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"broadcast [anythingToRegurgitate]\", \"hostname\", \"version\", \"program [portName] [core:architecture:name] [path/to/binOrHexFile]\", \"programfromurl [portName] [core:architecture:name] [urlToBinOrHexFile]\", \"execruntime\", \"exec [command] [arg1] [arg2] [...]\"]} ") c.send <- []byte("{\"Hostname\" : \"" + *hostname + "\"} ") case c := <-h.unregister: delete(h.connections, c) @@ -116,6 +116,38 @@ func (h *hub) run() { } } +func parseParity(s string) int { + switch s { + case "NONE": + return 0 + case "ODD": + return 1 + case "EVEN": + return 2 + case "MARK": + return 3 + case "SPACE": + return 4 + } + + go spErr("Problem parsing parity " + s) + return 0 +} + +func parseStopBits(s string) int { + switch s { + case "1": + return 0 + case "1.5": + return 1 + case "2": + return 2 + } + + go spErr("Problem parsing stopBits " + s) + return 0 +} + func checkCmd(m []byte) { //log.Print("Inside checkCmd") s := string(m[:]) @@ -137,10 +169,12 @@ func checkCmd(m []byte) { // remove newline args := strings.Split(strings.TrimSpace(s), " ") + if len(args) < 3 { go spErr("You did not specify a port and baud rate in your open cmd") return } + if len(args[1]) < 1 { go spErr("You did not specify a serial port") return @@ -152,16 +186,38 @@ func checkCmd(m []byte) { go spErr("Problem converting baud rate " + args[2]) return } + + // default values + byteSize := 8 + parity := 0 + stopBits := 0 + bufferAlgorithmIndex := 3; + + if len(args) > 3 { + byteSizeStr := strings.Replace(args[3], "\n", "", -1) + byteSize, err = strconv.Atoi(byteSizeStr) + if err != nil { + go spErr("Problem converting byteSize " + args[3]) + return + } + + parity = parseParity(strings.Replace(args[4], "\n", "", -1)) + + stopBits = parseStopBits(strings.Replace(args[5], "\n", "", -1)) + + bufferAlgorithmIndex = 6; + } + // pass in buffer type now as string. if user does not // ask for a buffer type pass in empty string bufferAlgorithm := "" - if len(args) > 3 { + if len(args) > bufferAlgorithmIndex { // cool. we got a buffer type request - buftype := strings.Replace(args[3], "\n", "", -1) + buftype := strings.Replace(args[bufferAlgorithmIndex], "\n", "", -1) bufferAlgorithm = buftype } - go spHandlerOpen(args[1], baud, bufferAlgorithm, isSecondary) + go spHandlerOpen(args[1], baud, uint8(byteSize), uint8(parity), uint8(stopBits), bufferAlgorithm, isSecondary) } else if strings.HasPrefix(sl, "close") { log.Printf("About to split close commands. cmd:\"%v\"", s) @@ -241,7 +297,6 @@ func checkCmd(m []byte) { execRuntime() } else if strings.HasPrefix(sl, "exec") { go execRun(s) - /* } else if strings.HasPrefix(sl, "gethost") { hostname, err := gpio.Host() diff --git a/serialport.go b/serialport.go index 1ffda31..c32b67a 100755 --- a/serialport.go +++ b/serialport.go @@ -368,9 +368,9 @@ func (p *serport) writerNoBuf() { var spmutex = &sync.Mutex{} var spIsOpening = false -func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool) { +func spHandlerOpen(portname string, baud int, byteSize byte, parity byte, stopBits byte, buftype string, isSecondary bool) { - log.Print("Inside spHandler") + log.Printf("Inside spHandler: %v %v %v %v %v %v", portname, baud, byteSize, parity, stopBits, buftype) spmutex.Lock() if spIsOpening { log.Println("We are currently in the middle of opening a port. Returning...") @@ -406,6 +406,11 @@ func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool) conf.Name = portname conf.RtsOn = true conf.DtrOn = false + conf.ByteSize = byteSize + conf.Parity = parity + conf.StopBits = stopBits + + // Needed for Arduino serial library /*