golang libusb wraper
2021/11/18 23:42:56
本文主要是介绍golang libusb wraper,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
https://pkg.go.dev/github.com/google/gousb
package main import ( "fmt" "log" "github.com/google/gousb" ) func main() { // Initialize a new Context. ctx := gousb.NewContext() defer ctx.Close() // Iterate through available Devices, finding all that match a known VID/PID. vid, pid := gousb.ID(0x04f2), gousb.ID(0xb531) devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool { // this function is called for every device present. // Returning true means the device should be opened. return desc.Vendor == vid && desc.Product == pid }) // All returned devices are now open and will need to be closed. for _, d := range devs { defer d.Close() } if err != nil { log.Fatalf("OpenDevices(): %v", err) } if len(devs) == 0 { log.Fatalf("no devices found matching VID %s and PID %s", vid, pid) } // Pick the first device found. dev := devs[0] // Switch the configuration to #2. cfg, err := dev.Config(2) if err != nil { log.Fatalf("%s.Config(2): %v", dev, err) } defer cfg.Close() // In the config #2, claim interface #3 with alt setting #0. intf, err := cfg.Interface(3, 0) if err != nil { log.Fatalf("%s.Interface(3, 0): %v", cfg, err) } defer intf.Close() // In this interface open endpoint #6 for reading. epIn, err := intf.InEndpoint(6) if err != nil { log.Fatalf("%s.InEndpoint(6): %v", intf, err) } // And in the same interface open endpoint #5 for writing. epOut, err := intf.OutEndpoint(5) if err != nil { log.Fatalf("%s.OutEndpoint(5): %v", intf, err) } // Buffer large enough for 10 USB packets from endpoint 6. buf := make([]byte, 10*epIn.Desc.MaxPacketSize) total := 0 // Repeat the read/write cycle 10 times. for i := 0; i < 10; i++ { // readBytes might be smaller than the buffer size. readBytes might be greater than zero even if err is not nil. readBytes, err := epIn.Read(buf) if err != nil { fmt.Println("Read returned an error:", err) } if readBytes == 0 { log.Fatalf("IN endpoint 6 returned 0 bytes of data.") } // writeBytes might be smaller than the buffer size if an error occurred. writeBytes might be greater than zero even if err is not nil. writeBytes, err := epOut.Write(buf[:readBytes]) if err != nil { fmt.Println("Write returned an error:", err) } if writeBytes != readBytes { log.Fatalf("IN endpoint 5 received only %d bytes of data out of %d sent", writeBytes, readBytes) } total += writeBytes } fmt.Printf("Total number of bytes copied: %d\n", total) }
这篇关于golang libusb wraper的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享
- 2024-12-10搭建个人博客网站之一、使用hugo创建个人博客网站