bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
658 stars 200 forks source link

Trouble using GetDetailedPortsList on Mac #30

Closed jcw closed 7 years ago

jcw commented 7 years ago

Hello - I'm trying out the GetDetailedPortsList(), because it'd be a great way to help choosing from a list of serial ports to connect to, but get the following errors on macOS 10.12.4:

# go.bug.st/serial.v1/enumerator
../../Go/src/go.bug.st/serial.v1/enumerator/usb_darwin.go:155: cannot use property (type C.CFTypeRef) as type *C.struct___CFString in argument to func literal
../../Go/src/go.bug.st/serial.v1/enumerator/usb_darwin.go:161: cannot use property (type C.CFTypeRef) as type *C.struct___CFString in argument to func literal
../../Go/src/go.bug.st/serial.v1/enumerator/usb_darwin.go:176: cannot use property (type C.CFTypeRef) as type *C.struct___CFNumber in argument to func literal

The irony being that on macOS, I don't really need this, since the /dev/tty.* names are usually self-descriptive already (i.e. serial # included in device name). For Linux and Windows however, that extra information would be very useful, especially since assigned device names can change.

Is there a way to disable the macOS variants to avoid the above errors, and still get this functionality for Linux and Windows? (I'm guessing it can be done with some mix of blah_arch.go and blah.go files)

cmaglie commented 7 years ago

Are you building natively on macos?

jcw commented 7 years ago

Yes.

cmaglie commented 7 years ago

argh, this should not happen, I'll check this out in the coming days (and also use that time to merge some pending PR...)

In the meantime, as a temporary workaround, you can replace the nativeGetDetailedPortList function in usb_darwin.go with a fake placeholder, something like:

func nativeGetDetailedPortsList() ([]*PortDetails, error) {
    var ports []*PortDetails
        // [...removed everything...]
    return ports, nil
}

// [...removed all the following functions...]
jcw commented 7 years ago

Thank you - that workaround works fine. No rush, btw.

facchinm commented 7 years ago

Untested fix (will report if ok after testing on real hardware):

diff --git a/vendor/go.bug.st/serial.v1/enumerator/usb_darwin.go b/vendor/go.bug.st/serial.v1/enumerator/usb_darwin.go
index b02de12..4cb9f61 100644
--- a/vendor/go.bug.st/serial.v1/enumerator/usb_darwin.go
+++ b/vendor/go.bug.st/serial.v1/enumerator/usb_darwin.go
@@ -152,13 +152,13 @@ func (me *C.io_registry_entry_t) GetStringProperty(key string) (string, error) {
        }
        defer C.CFRelease(property)

-       if ptr := C.CFStringGetCStringPtr(property, 0); ptr != nil {
+       if ptr := C.CFStringGetCStringPtr((C.CFStringRef)(unsafe.Pointer(property)), 0); ptr != nil {
                return C.GoString(ptr), nil
        }
        // in certain circumstances CFStringGetCStringPtr may return NULL
        // and we must retrieve the string by copy
        buff := make([]C.char, 1024)
-       if C.CFStringGetCString(property, &buff[0], 1024, 0) != C.true {
+       if C.CFStringGetCString((C.CFStringRef)(property), &buff[0], 1024, 0) != C.true {
                return "", fmt.Errorf("Property '%s' can't be converted", key)
        }
        return C.GoString(&buff[0]), nil
@@ -173,7 +173,7 @@ func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberTy
        }
        defer C.CFRelease(property)
        var res int
-       if C.CFNumberGetValue(property, intType, unsafe.Pointer(&res)) != C.true {
+       if C.CFNumberGetValue((C.CFNumberRef)(property), intType, unsafe.Pointer(&res)) != C.true {
                return res, fmt.Errorf("Property '%s' can't be converted or has been truncated", key)
        }
        return res, nil