dousi1994 2017-07-15 19:04
浏览 23

找到特定的定界符后,如何从串行中读取数据并进行处理

I have a device, which continues to send data over a serial port. Now I want to read this and process it. The data send this delimiter "!" and as soon as this delimiter appears I want to pause reading to processing the data thats already been received. How can I do that? Is there any documentation or examples that I can read or follow.

  • 写回答

2条回答 默认 最新

  • dongmi3203 2017-07-17 12:35
    关注

    For reading data from a serial port you can find a few packages on Github, e.g. tarm/serial.

    You can use this package to read data from your serial port. In order to read until a specific delimiter is reached, you can use something like:

    config := &serial.Config{Name: "/dev/ttyUSB", Baud: 9600}
    
    s, err := serial.OpenPort(config)
    if err != nil {
        // stops execution
        log.Fatal(err)
    }
    
    // golang reader interface
    r := bufio.NewReader(s)
    
    // reads until delimiter is reached
    data, err := r.ReadBytes('\x21')
    if err != nil {
        // stops execution
        log.Fatal(err)
    }
    // or use fmt.Printf() with the right verb
    // https://golang.org/pkg/fmt/#hdr-Printing
    fmt.Println(data)
    

    See also: Reading from serial port with while-loop

    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab