Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/wwhai/tarmserial

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/wwhai/tarmserial

  • v1.0.0
  • Source
  • Go
  • Socket score

Version published
Created
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

Package last updated on 19 Apr 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc