从串口获取输入

时间:2017-12-15 05:46:31

标签: go

我正在尝试编写一个从RFID读卡器读取的程序,我已将该设备配置为字符输入并使用enter结束输入。我正在使用GoLang。这是我读取输入的代码部分:

//Using www.github.com/tarm/goserial and bufio

func getInput(input chan string) {
    c := &serial.Config{Name: "/dev/input/event5", Baud: 9600}
    s, err := serial.OpenPort(c)

    if err != nil {
        fmt.Println(err)
    }

    for {
        reader:= bufio.NewReader(s)
        result, err1:= reader.ReadBytes('\x0a')
        if err1!=nil {
            panic(err1)
        }
        input<-string(result)
    }
    s.Close()
}

但是,只要它执行函数reader.ReadBytes,并且看到没有输入,就会给我这个错误:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x93e432]

goroutine 53 [running]:

vendor/github.com/tarm/serial.(*Port).Read(0x0, 0xc420292000, 0x1000,      
0x1000, 0x18, 0xc4201b4d00, 0xc4201793f0) 
    vendor/github.com/tarm/serial/serial_linux.go:135 +0x22
bufio.(*Reader).fill(0xc420036f18)
    /usr/local/go/src/bufio/bufio.go:97 +0x117
bufio.(*Reader).ReadSlice(0xc420036f18, 0xc42029200a, 0x7fac0a47fab0, 
0x1, 0x101000000000000, 0x0, 0x1000)
    /usr/local/go/src/bufio/bufio.go:338 +0xbb
bufio.(*Reader).ReadBytes(0xc420036f18, 0x100a, 0x1000, 0xc420292000, 
0x1000, 0x1000, 0x0)
    /usr/local/go/src/bufio/bufio.go:416 +0x66
getInput(0xc4201e8c00)
    /home/shivam/go/src/main.go:150 +0x172
created by main.go.ReadCard
    /home/shivam/go/src/main.go:164 +0x72

exit status 2

我可以做什么让它等待输入并在收到&#39; \ x0a&#39;字节(返回)?

谢谢!

0 个答案:

没有答案