I try to communicate with a serial device using this package. I adopted the provided sample code to my needs as follows:
package main
import (
"github.com/mikepb/go-serial"
"log"
)
func main() {
options := serial.RawOptions
options.BitRate = 9600
p, err := options.Open("/dev/cu.usbmodem641")
if err != nil {
log.Panic(err)
}
defer p.Close()
buf := make([]byte, 1)
if _, err := p.Read(buf); err != nil {
log.Panic(err)
} else {
log.Println(buf)
}
}
However, I am not able to open the serial port. An error is raised:
2018/09/22 21:50:24 The requested operation is not supported by this system or device
panic: The requested operation is not supported by this system or device
goroutine 1 [running]:
log.Panic(0xc000049f50, 0x1, 0x1)
/usr/local/Cellar/go/1.11/libexec/src/log/log.go:326 +0xc0
main.main()
/Users/Albert/go/src/serial-test/test.go:14 +0xda
exit status 2
How could I narrow this down? I am working on MacOS 10.10.5 with golang 1.11 from Homebrew. The serial device is an FTDI FT232 or an Atmega 16U2 on-board an Arduino (working with Arduino IDE).
Update due to comments:
I had a typo in my code. /dev/cu.usbmodem641
is actually /dev/cu.usbmodem411
which is available in device directory as ll /dev/cu.*
shows:
crw-rw-rw- 1 root wheel 17, 1 16 Sep 20:46 /dev/cu.Bluetooth-Incoming-Port
crw-rw-rw- 1 root wheel 17, 3 16 Sep 20:46 /dev/cu.Bluetooth-Modem
crw-rw-rw- 1 root wheel 17, 151 23 Sep 13:21 /dev/cu.usbmodem411
Go with go-serial package itself lists the device as well by using:
package main
import (
"fmt"
"log"
"github.com/mikepb/go-serial"
)
func main() {
portInfo, err := serial.ListPorts()
if err != nil {
log.Panic(err)
}
for _, info := range portInfo {
fmt.Println(info.Name())
}
}
Giving the following output:
$ go run list_serial.go
/dev/cu.Bluetooth-Incoming-Port
/dev/cu.Bluetooth-Modem
/dev/cu.usbmodem411
I fixed the typo in the snipped I provided in my question above and tried again which leads to an invalid arguments error:
$ go run test.go
2018/09/23 13:33:32 Invalid arguments were passed to the function
panic: Invalid arguments were passed to the function
goroutine 1 [running]:
log.Panic(0xc000049f50, 0x1, 0x1)
/usr/local/Cellar/go/1.11/libexec/src/log/log.go:326 +0xc0
main.main()
/Users/Albert/go/src/go-echo-vue/test.go:13 +0xda
exit status 2
I double-checked everything, found no additional error in my code and ran again. This time getting another error:
$ go run test.go
2018/09/23 13:37:26 The requested operation is not supported by this system or device
panic: The requested operation is not supported by this system or device
goroutine 1 [running]:
log.Panic(0xc000044f50, 0x1, 0x1)
/usr/local/Cellar/go/1.11/libexec/src/log/log.go:326 +0xc0
main.main()
/Users/Albert/go/src/go-echo-vue/test.go:13 +0xda
exit status 2
Remark: Serial ports on macOS have two interfaces to the device itself. One is the tty.*
, the other is cu.*
. For a short explanation see this SO answer. I was not able to get the Go code running with either of those interfaces raising same errors as stated before.