LibLaserCut icon indicating copy to clipboard operation
LibLaserCut copied to clipboard

K40NanoDriver: LibUsbException: USB error 6: Could not claim the interface.: Resource busy

Open mgmax opened this issue 4 years ago • 5 comments

Discussed in https://github.com/t-oster/VisiCut/discussions/605

Originally posted by papapingouin-be April 21, 2021 Hello, I use visicut with my K40 lasercutter on a raspberry pi3b from 1year and all is ok but after updates i have this message and i can't rollback ... pi@raspberrypi:~ $ visicut openjdk version "11.0.9.1" 2020-11-04 ... org.usb4java.LibUsbException: USB error 6: Could not claim the interface.: Resource busy at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Usb.claimInterface(K40NanoDriver.java:1762) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Usb.open(K40NanoDriver.java:1433) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Queue.open(K40NanoDriver.java:1272) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Device.open(K40NanoDriver.java:469) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Device.open(K40NanoDriver.java:463) at de.thomas_oster.liblasercut.drivers.K40NanoDriver.sendJob(K40NanoDriver.java:109) at de.thomas_oster.visicut.VisicutModel.sendJob(VisicutModel.java:778) at de.thomas_oster.visicut.gui.MainView.lambda$executeOrSaveJob$7(MainView.java:2209) at java.base/java.lang.Thread.run(Thread.java:834) org.usb4java.LibUsbException: USB error 6: Could not claim the interface.: Resource busy at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Usb.claimInterface(K40NanoDriver.java:1762) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Usb.open(K40NanoDriver.java:1433) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Queue.open(K40NanoDriver.java:1272) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Device.open(K40NanoDriver.java:469) at de.thomas_oster.liblasercut.drivers.K40NanoDriver$K40Device.open(K40NanoDriver.java:463) at de.thomas_oster.liblasercut.drivers.K40NanoDriver.sendJob(K40NanoDriver.java:109) at de.thomas_oster.visicut.VisicutModel.sendJob(VisicutModel.java:778) at de.thomas_oster.visicut.gui.MainView.lambda$executeOrSaveJob$7(MainView.java:2209) at java.base/java.lang.Thread.run(Thread.java:834)

root@raspberrypi:/home/pi# lsusb Bus 001 Device 004: ID 1a86:5512 QinHeng Electronics CH341 in EPP/MEM/I2C mode, EPP/I2C adapter Bus 001 Device 005: ID 0424:7800 Standard Microsystems Corp. Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Thanks for your help Laurent

mgmax avatar Sep 11 '21 09:09 mgmax

As discussed here https://github.com/t-oster/VisiCut/discussions/605#discussioncomment-643395 , the error disappears if K40Whisperer is started (and probably closed again) before using VisiCut.

So the problem is that VisiCut does not detach the USB driver of the CH341 device. Probable solution is something like "int r = LibUsb.detachKernelDriver(handle, interfaceNum); " somewhere near claimInterface in https://github.com/t-oster/LibLaserCut/blob/master/src/main/java/de/thomas_oster/liblasercut/drivers/K40NanoDriver.java

@tatarize Could you maybe have a look? Or someone else who has access to such a lasercutter?

mgmax avatar Sep 11 '21 09:09 mgmax

Okay, cloned a fresh repo. Ran it.

LibUsbException: USB error 3: Could not claim the interface: Access denied (insufficient permissions)

Trying again, running netbeans as Administrator. Apparently not good enough I need to run Visicut directly as administrator.

-- Loading files suddenly broke.


In Raspberry pi, I believe there might be an issue with the order of operations a deep dive by a contributor in the MeerK40t project had the order there shifted a bit. As for windows I might have to figure out he proper method for DLL calls (to code a secondary default windows driver using the CH341DLL) because the need to run in admin mode is really really annoying.

tatarize avatar Sep 12 '21 10:09 tatarize

Attempted again, after uninstalling and reinstalling viscut. Gave another insufficient permission error, even from root.

I ran it successfully during testing. Not sure if anything changed or if it's just being weird.


Orderwise:

  • set_config *interface = get active config(device)
  • detach_kernel(device, interface)
  • claim interface
    • If interface fails, unclaim interface and claim interface again.

For some linux installs:

  • sudo su
  • cd /etc/udev/rules.d
  • echo "SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="5512", MODE="0666"" > 90-K40.laser.CH341.rules
  • sudo udevadm control --reload-rules
  • Unplug and replug K40 usb connection

Depending on things it might need the rule to permit you to access the K40. You can test this if it works with sudo visicut and works.

tatarize avatar Sep 12 '21 10:09 tatarize

It sounds like the "access denied" is a separate issue that will also affect any other lasercutting program (as far as it uses libusb). Also discussed in https://stackoverflow.com/questions/28884817/usb4java-library-error-while-claiming-an-interface

Regarding "Loading files suddenly broke": Opening SVG files works for me, so can you please create an issue with more details (which file, which OS)?

mgmax avatar Sep 12 '21 12:09 mgmax

Even with windows being weird raspberry pi should work, but sometimes the permission error is actually a permission error and the OS doesn't have access. If sudo fixes it, then it's actually permission.

My error recovered after reinstall. I think it was an old config file or something and didn't give me a highly specific error.


The access thing is a bit weird. Python code doesn't have that requirement. If I could call a little python script I could run this stuff without hitting any permissions issues. Though rPi would still need a rule (or sudo) to access the USB, as posted above. But, windows is fine with access without any USB snafus, for Python but not for Java.

I could add in lhyserver devices, where they accept TCP data and just send it over the USB to the device. Though this might be a somewhat rare construction. And it seems like it would need to be compiled or run in a venv or stored in the drivers or stored as a frozen .exe (which is even less desirable).

The network capabilities are desirable enough. Rig up a raspberry pi zero and control the M2 Nano by just TCP sending data to that server. Though asking that folks use a second program to get the first program to work isn't very helpful.

tatarize avatar Sep 12 '21 20:09 tatarize