Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `write' for nil:NilClass (NoMethodError) on macOS #7

Open
jhgorse opened this issue May 29, 2020 · 3 comments
Open

Comments

@jhgorse
Copy link
Contributor

jhgorse commented May 29, 2020

Hi Beau,

Dusted off a script from 4 years ago, hello 2016:
https://gist.github.com/jhgorse/a584724153ad75aff3d6fdd81c5c9242/bad6a471a4281fba8a3351f5050cfa907d66dd82

Ported it to the modern hidapi from hid_api.
https://gist.github.com/jhgorse/a584724153ad75aff3d6fdd81c5c9242

The write method appears to have changed as well, breaking what I had hoped would be a timeless and classic way to call a write():

buf = Array.new(61) { |i| (0) };
buf[0] = 0x2
handle.write(buf)

error message:

ruby irrad.rb
Traceback (most recent call last):
irrad.rb:40:in `<main>': undefined method `write' for nil:NilClass (NoMethodError)

FWIW, the perl script I wrote from this era still functions. The python 2 script does not yet recognize that hidapi was installed by its local pip, so it is not working either. This ruby script was not backward compatible, as we see.

Cheers,
Joe

@barkerest
Copy link
Owner

You need to verify your handle before attempting to write to it. The error indicates that the handle is "nil".

@barkerest
Copy link
Owner

I noticed in your script that you are running the setup task immediately before attempting to open the device. The setup task is only meant to be run once to configure the computer. In macOS, you would need to run the setup task and then reset the device by unplugging it or restarting the computer. Otherwise it will remain bound to the builtin HID driver.

As for backwards compatibility. My port was meant to just be an alternative to hid_api.

@jhgorse
Copy link
Contributor Author

jhgorse commented May 30, 2020

Okay. Thank you for writing back and clearing up some of my misunderstanding.

I tested the hidapi-testgui from the source C libhidapi library on osx. It just works for hid. No need for the helper kext. No issues opening, writing, or reading. Oops. Now I realize this is a libusb implementation. Fair enough.

So with the issues above addressed, my hello world script for hidapi now fails with #<LIBUSB::ERROR_ACCESS: LIBUSB::ERROR_ACCESS in libusb_claim_interface> (HIDAPI::DeviceOpenFailed) which seems to indicate that libusb cannot get the device. The codeless kext is installed.

require "hidapi"
vendor_id = 0x10c4
product_id = 0x63cd
handle = HIDAPI::open(vendor_id, product_id)
...
HIDAPI::hid_close(handle)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants