无法使用mikepb的go-serial打开串行端口

时间:2018-09-22 20:02:15

标签: go serial-port

我尝试使用this package与串行设备进行通信。我根据需要采用了提供的示例代码:

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)
  }
}

但是,我无法打开串行端口。引发错误:

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

如何缩小范围?我正在使用来自Homebrew的golang 1.11的MacOS 10.10.5。串行设备是Arduino上的FTDI FT232或Atmega 16U2(与Arduino IDE配合使用)。


由于评论而更新:

我的代码中有错字。 /dev/cu.usbmodem641实际上是/dev/cu.usbmodem411,它在设备目录中可用,如ll /dev/cu.*所示:

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-serial软件包本身可以通过以下方式列出设备:

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())
  }
}

提供以下输出:

$ go run list_serial.go 
/dev/cu.Bluetooth-Incoming-Port
/dev/cu.Bluetooth-Modem
/dev/cu.usbmodem411

我在上面的问题中提供的片段中修正了错字,然后再次尝试导致无效参数错误:

$ 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

我仔细检查了所有内容,发现代码中没有其他错误,然后再次运行。这次又出现另一个错误:

$ 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    

备注:macOS上的串行端口具有到设备本身的两个接口。一个是tty.*,另一个是cu.*。有关简短说明,请参见this SO answer。我无法在两个界面中的任何一个界面上运行Go代码,并引发与前面所述相同的错误。

0 个答案:

没有答案