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

Added in "ModeFromString" for turning standard serial port mode notat… #151

Open
wants to merge 4 commits 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
47 changes: 47 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,50 @@ func (e PortError) Error() string {
func (e PortError) Code() PortErrorCode {
return e.code
}

// ModeFromString uses a string description of a serial mode (such as "8N1")
// to partially populate a Mode structure.
// See https://en.wikipedia.org/wiki/8-N-1 for details
func ModeFromString(smode string, mode *Mode) error {
if len(smode) > 0 {
switch smode[0] {
case '8':
mode.DataBits = 8
case '7':
mode.DataBits = 7
case '6':
mode.DataBits = 6
case '5':
mode.DataBits = 5
default:
return &PortError{code: InvalidDataBits}
}
}
if len(smode) > 1 {
switch smode[1] {
case 'E':
mode.Parity = EvenParity
case 'N':
mode.Parity = NoParity
case 'O':
mode.Parity = OddParity
case 'M':
mode.Parity = MarkParity
case 'S':
mode.Parity = SpaceParity
default:
return &PortError{code: InvalidParity}
}
}
if len(smode) > 2 {
switch smode[2] {
case '1':
mode.StopBits = OneStopBit
case '2':
mode.StopBits = TwoStopBits
default:
return &PortError{code: InvalidStopBits}
}
}
return nil
}
39 changes: 39 additions & 0 deletions serial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package serial

import (
"reflect"
"testing"
)

func TestModeFromString(t *testing.T) {
good_cases := map[string]*Mode{
"8N1": {DataBits: 8, Parity: NoParity, StopBits: OneStopBit},
"7S2": {DataBits: 7, Parity: SpaceParity, StopBits: TwoStopBits},
}

bad_cases := map[string]*PortError{
"9N1": {code: InvalidDataBits},
"8N3": {code: InvalidStopBits},
"8R1": {code: InvalidParity},
}

for s, m := range good_cases {
mode := &Mode{}
err := ModeFromString(s, mode)
if err != nil {
t.Errorf("Failed to convert mode %q: %s", s, err)
} else if !reflect.DeepEqual(mode, m) {
t.Errorf("Mode %q should convert to %+v, got %+v", s, m, mode)
}
}

for s, e := range bad_cases {
mode := &Mode{}
err := ModeFromString(s, mode)
if err == nil {
t.Errorf("Mode %q should be invalid, got %v", s, mode)
} else if pe, ok := err.(*PortError); !ok || pe.code != e.code {
t.Errorf("Mode %q should fail with %v, got %v", s, e, err)
}
}
}