require 'ffi'
module HidApi
extend FFI::Library
ffi_lib 'hidapi'
attach_function :hid_open, [:int, :int, :int], :pointer
attach_function :hid_write, [:pointer, :pointer, :int], :int
attach_function :hid_read_timeout, [:pointer, :pointer, :int, :int], :int
attach_function :hid_close, [:pointer], :void
REPORT_SIZE = 65 # 64 bytes + 1 byte for report type
def self.pad_to_report_size(bytes)
(bytes+[0]*(REPORT_SIZE-bytes.size)).pack("C*")
end
end
# USAGE
# CONNECT
vendor_id = 1234
product_id = 65432
serial_number = 0
device = HidApi.hid_open(vendor_id, product_id, serial_number)
# SEND
command_to_send = HidApi.pad_to_report_size([0]+ARGV.map(&:hex)).pack("C*")
res = HidApi.hid_write dev, command_to_send, HidApi::REPORT_SIZE
raise "command write failed" if res <= 0
# READ
buffer = FFI::Buffer.new(:char, HidApi::REPORT_SIZE)
res = HidApi.hid_read_timeout device, buffer, HidApi::REPORT_SIZE, 1000
raise "command read failed" if res <= 0
p buffer.read_bytes(HidApi::REPORT_SIZE).unpack("C*")
分享到:
相关推荐
Ruby工具 windows 环境 Ruby 脚本 语言
ruby 在 WINDOWS 下的开发环境工具 ,下载后点击运行后即可用CMD窗口操作RUBY命令了,我是用URTALEDIT 作编辑器的再执行的。
RUBY在windows操作系统下的UI安装程序,很方便
rubyinstaller-devkit-2.7.2-1-x86.exe 是ruby 在windows 下的安装包,包含了ruby 的一些lib库。因为该安装包下载一般会比较慢,这里提供一个镜像。
ruby 2.3.1 windows64位安装包
在Windows系统上安装Ruby是一项相对简单的任务。以下是在Windows系统上安装Ruby的一般步骤: 下载Ruby Installer: 访问Ruby官方网站(https://www.ruby-lang.org/en/downloads/)下载适用于Windows系统的Ruby ...
Clipboard - Ruby访问Windows、Linux、macOS、Java 和Cygwin中剪贴板
Windows上配置Ruby运行环境详解
ruby windows
Ruby/GraphViz - GraphViz绘图工具的Ruby接口
Tilt - 多个Ruby模板引擎的通用接口
windows版本ruby运行环境安装,下载后解压即可,直接双击运行
ruby安装到windows比较麻烦,提供的一键安装省去了很多麻烦。
此工具为ruby和redis接口,用于搭建redisCluster集群,好用!
Watir - 在Ruby中Web应用程序测试
在工作中,他希望有一种比 Perl 强大,比 Python 更面向对象的语言。从1993年2月,他开始设计一个全新的自己的语言,1994年12月发布了第一个 alpha版本,并且将这种新语言定名为Ruby(红宝石)。 本教程内含多部中文...
[Ruby_on_Rails][資料庫]_03._[Windows]_MySQL安裝
Ruby on Rails中文指南
镜像比较难下载,老是失败。
ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码