Socket
Socket
Sign inDemoInstall

github.com/wwhai/tarmserial

Package Overview
Dependencies
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/wwhai/tarmserial

Goserial is a simple go package to allow you to read and write from the serial port as a stream of bytes. It aims to have the same API on all platforms, including windows. As an added bonus, the windows package does not use cgo, so you can cross compile for windows from another platform. Unfortunately goinstall does not currently let you cross compile so you will have to do it manually: Currently there is very little in the way of configurability. You can set the baud rate. Then you can Read(), Write(), or Close() the connection. Read() will block until at least one byte is returned. Write is the same. There is currently no exposed way to set the timeouts, though patches are welcome. Currently all ports are opened with 8 data bits, 1 stop bit, no parity, no hardware flow control, and no software flow control. This works fine for many real devices and many faux serial devices including usb-to-serial converters and bluetooth serial ports. You may Read() and Write() simulantiously on the same connection (from different goroutines). Example usage:


Version published

Readme

Source

go 串口

貌似原始仓库作者很多年不更新,本仓库把原始仓库底下的一些不错的pr合并进来了,所以原始文档直接查看 https://github.com/tarm/serial 即可。

快速开始

package main

import (
        "log"

        "github.com/wwhai/tarmserial"
)

func main() {
        c := &serial.Config{Name: "COM45", Baud: 115200}
        s, err := serial.OpenPort(c)
        if err != nil {
                log.Fatal(err)
        }

        n, err := s.Write([]byte("test"))
        if err != nil {
                log.Fatal(err)
        }

        buf := make([]byte, 128)
        n, err = s.Read(buf)
        if err != nil {
                log.Fatal(err)
        }
        log.Printf("%q", buf[:n])
}

超时

新增了个读写超时控制:

s1.SetReadDeadline(time.Now().Add(30 * time.Millisecond))
n, err := s1.Read(buf)
s1.SetReadDeadline(time.Time{})

原来的仓库使用的是全局 serial.Config{}, 里面有个 ReadTimeout 参数,因为这个参数是全局参数,一次配置无法修改,所以不适合动态读写超时控制,所以在原来的基础上扩充了标准的Context Timeout形式。SetReadDeadline(time.Time{}) 表示取消本次超时控制, 这样看起来就和TCP等网络操作形式一样了。

FAQs

Last updated on 19 Apr 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc