-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusb.go
44 lines (36 loc) · 930 Bytes
/
usb.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
package dexcom
import (
"log"
"github.com/ecc1/serial"
)
const (
// USB IDs for the Dexcom G4 receiver.
dexcomVendor = 0x22a3
dexcomProduct = 0x0047
)
type usbConn serial.Port
// OpenUSB opens the USB serial device for a Dexcom G4 receiver.
func OpenUSB() (Connection, error) {
device, err := serial.FindUSB(dexcomVendor, dexcomProduct)
if err != nil {
_, notFound := err.(serial.DeviceNotFoundError)
if !notFound {
log.Print(err)
}
return nil, err
}
port, err := serial.Open(device, 115200)
return (*usbConn)(port), err
}
// Send writes data over the USB connection.
func (conn *usbConn) Send(data []byte) error {
return (*serial.Port)(conn).Write(data)
}
// Receive reads data from the USB connection.
func (conn *usbConn) Receive(data []byte) error {
return (*serial.Port)(conn).Read(data)
}
// Close closes the USB connection.
func (conn *usbConn) Close() {
_ = (*serial.Port)(conn).Close()
}