-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.go
51 lines (45 loc) · 1.04 KB
/
serial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gokbus
import (
"time"
)
// Waits for the bus to clear, then returns the Source of the next packet
func (kbus *KBUS) getSourceByte() ([]byte, error) {
var lastByteRecievedTime = time.Now()
for {
srcPacket, err := kbus.readBytes()
if err != nil {
return nil, err
}
if srcPacket == nil {
return nil, nil
}
if time.Since(lastByteRecievedTime) > time.Millisecond*10 {
return srcPacket, nil
}
lastByteRecievedTime = time.Now()
}
}
// Gets the remaining packets in this data stream
func (kbus *KBUS) getDataBytes(length int) ([]byte, error) {
var returnBytes []byte
for i := 0; i < length; i++ {
newByte, err := kbus.readBytes()
if err != nil {
return nil, err
}
if newByte == nil {
return nil, nil
}
returnBytes = append(returnBytes, newByte[0])
}
return returnBytes, nil
}
func (kbus *KBUS) readBytes() ([]byte, error) {
buf := make([]byte, 32)
// Read until there is activity on the bus, or 200ms have passed
n, err := kbus.Port.Read(buf)
if n == 0 {
return nil, nil
}
return buf[:n], err
}