From 5050c501856544bdfc7e145c49347600f133a81a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sat, 8 Oct 2016 11:15:50 +0200 Subject: [PATCH] Added USB API --- example_getdetailedportlist_test.go | 29 +++++++++++++++++++++++++++++ serial.go | 23 +++++++++++++++++++++++ usb_darwin.go | 12 ++++++++++++ usb_freebsd.go | 12 ++++++++++++ usb_linux.go | 12 ++++++++++++ usb_windows.go | 12 ++++++++++++ 6 files changed, 100 insertions(+) create mode 100644 example_getdetailedportlist_test.go create mode 100644 usb_darwin.go create mode 100644 usb_freebsd.go create mode 100644 usb_linux.go create mode 100644 usb_windows.go diff --git a/example_getdetailedportlist_test.go b/example_getdetailedportlist_test.go new file mode 100644 index 0000000..1c7a491 --- /dev/null +++ b/example_getdetailedportlist_test.go @@ -0,0 +1,29 @@ +// +// Copyright 2014-2016 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial_test + +import "fmt" +import "log" +import "go.bug.st/serial.v1" + +func ExampleGetDetailedPortsList() { + ports, err := serial.GetDetailedPortsList() + if err != nil { + log.Fatal(err) + } + if len(ports) == 0 { + fmt.Println("No serial ports found!") + return + } + for _, port := range ports { + fmt.Printf("Found port: %s\n", port.Name) + if port.IsUSB { + fmt.Printf(" USB ID %s:%s\n", port.VID, port.PID) + fmt.Printf(" USB serial %s\n", port.SerialNumber) + } + } +} diff --git a/serial.go b/serial.go index 331db34..91b3332 100644 --- a/serial.go +++ b/serial.go @@ -55,6 +55,25 @@ func GetPortsList() ([]string, error) { return nativeGetPortsList() } +// PortDetails contains detailed information about USB serial port. +// Use GetDetailedPortsList function to retrieve it. +type PortDetails struct { + Name string + IsUSB bool + VID string + PID string + SerialNumber string + Manufacturer string + Product string +} + +// GetDetailedPortsList retrieve ports details like USB VID/PID. +// Please note that this function may not be available on all OS: +// in that case a FunctionNotImplemented error is returned. +func GetDetailedPortsList() ([]*PortDetails, error) { + return nativeGetDetailedPortsList() +} + // Mode describes a serial port configuration. type Mode struct { BaudRate int // The serial port bitrate (aka Baudrate) @@ -121,6 +140,8 @@ const ( ErrorEnumeratingPorts // PortClosed the port has been closed while the operation is in progress PortClosed + // FunctionNotImplemented the requested function is not implemented + FunctionNotImplemented ) // EncodedErrorString returns a string explaining the error code @@ -146,6 +167,8 @@ func (e PortError) EncodedErrorString() string { return "Could not enumerate serial ports" case PortClosed: return "Port has been closed" + case FunctionNotImplemented: + return "Function not implemented" default: return "Other error" } diff --git a/usb_darwin.go b/usb_darwin.go new file mode 100644 index 0000000..8a60a19 --- /dev/null +++ b/usb_darwin.go @@ -0,0 +1,12 @@ +// +// Copyright 2014-2016 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial // import "go.bug.st/serial.v1" + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + // TODO + return nil, &PortError{code: FunctionNotImplemented} +} diff --git a/usb_freebsd.go b/usb_freebsd.go new file mode 100644 index 0000000..8a60a19 --- /dev/null +++ b/usb_freebsd.go @@ -0,0 +1,12 @@ +// +// Copyright 2014-2016 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial // import "go.bug.st/serial.v1" + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + // TODO + return nil, &PortError{code: FunctionNotImplemented} +} diff --git a/usb_linux.go b/usb_linux.go new file mode 100644 index 0000000..8a60a19 --- /dev/null +++ b/usb_linux.go @@ -0,0 +1,12 @@ +// +// Copyright 2014-2016 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial // import "go.bug.st/serial.v1" + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + // TODO + return nil, &PortError{code: FunctionNotImplemented} +} diff --git a/usb_windows.go b/usb_windows.go new file mode 100644 index 0000000..8a60a19 --- /dev/null +++ b/usb_windows.go @@ -0,0 +1,12 @@ +// +// Copyright 2014-2016 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package serial // import "go.bug.st/serial.v1" + +func nativeGetDetailedPortsList() ([]*PortDetails, error) { + // TODO + return nil, &PortError{code: FunctionNotImplemented} +}