MCPcopy Create free account
hub / github.com/distributed/sers / ParseModestring

Function ParseModestring

modestring.go:28–110  ·  view source on GitHub ↗

Parses a modestring like "115200,8n1,rtscts" into a struct Mode. The format is baudrate,framestring,handshake. Either the handshake part or both the framestring and handshake parts can be omitted. For the omitted parts, defaults of 8 data bits, no parity, 1 stop bit and no handshaking will be filled

(s string)

Source from the content-addressed store, hash-verified

26// 19200,72 - 19200 baud, 7 data bits, no parity, 2 stop bits, no handshake
27// 9600,2,rtscts - 9600 baud, 8 data bits, no parity, 2 stop bits, rts/cts handshake
28func ParseModestring(s string) (Mode, error) {
29 var mode Mode
30
31 commaparts := strings.Split(strings.ToUpper(s), ",")
32
33 brpart := commaparts[0]
34 br64, err := strconv.ParseUint(brpart, 10, 32)
35 if err != nil {
36 return mode, fmt.Errorf("modestring %q cannot parse baudrate: %v", s, err)
37 }
38
39 mode.Baudrate = int(br64)
40
41 mode.DataBits = 8
42 mode.Parity = N
43 mode.Stopbits = 1
44 if len(commaparts) >= 2 {
45 framepart := commaparts[1]
46 idx := 0
47 lastidx := len(framepart) - 1
48
49 if idx <= lastidx {
50 switch framepart[idx] {
51 case '5':
52 mode.DataBits = 5
53 idx++
54 case '6':
55 mode.DataBits = 6
56 idx++
57 case '7':
58 mode.DataBits = 7
59 idx++
60 case '8':
61 mode.DataBits = 8
62 idx++
63 }
64 }
65
66 if idx <= lastidx {
67 switch framepart[idx] {
68 case 'N':
69 mode.Parity = N
70 idx++
71 case 'O':
72 mode.Parity = O
73 idx++
74 case 'E':
75 mode.Parity = E
76 idx++
77 }
78 }
79
80 if idx <= lastidx {
81 switch framepart[idx] {
82 case '1':
83 mode.Stopbits = 1
84 idx++
85 case '2':

Callers 2

MainFunction · 0.92
TestParseModestringFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseModestringFunction · 0.68