Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding extra open parameters: dataBits parity stopBits #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 60 additions & 5 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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[:])
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down Expand Up @@ -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
/*
Expand Down