dou9022 2019-09-24 04:38
浏览 90

[] byte {10}或[] byte(“ ”)与[] byte {92,110}

I'm working with github.com/tarm/serial to interface with some serial instrumentation. In development, I'm working with the /dev/ttyp0 and /dev/ptyp0 pair, where my go process connects to one and I use screen to connect to the other. I've written a function that, combined with serial.Config.ReadTimeout, reads for up to ReadTimeout or a given byte sequence is received. That function is:

func readToTermination(s serial.Port, termination []byte, rate time.Duration) []byte {
    var out []byte
    lterm := len(termination)
    for {
        buf := make([]byte, 128)
        n, _ := s.Read(buf)
        out = append(out, buf[:n]...)
        l := len(out)
        if l >= lterm {
            if bytes.Compare(out[l-lterm:], termination) == 0 {
                break
            }
        }
        time.Sleep(rate)
    }
    return out
}

This avoids burning up CPU cycles doing nothing with a debounce. When I test with termination = []byte(" ") and screen, the break never triggers because it turns into []byte{97, 11} (two distinct elements, something like screen flushing after each keystroke). On the other hand, if I do something like echo "foo" > /dev/ptyp0, the break triggers correctly. Echo implicitly seems to be sending a , or the closing of the connection does so. I see for echo foo and for echo "foo ".

So my question is:

(1) why is there a difference in behavior here?

(2) how do I get the behavior I really am after with a carriage return for the termination? Perhaps I should use EOT instead? A human will never be typing into this directly.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用