我正在尝试在Arduino UNO板和我的PC之间建立USB通信路径。因此,我首先为Arduino编写了以下代码,该代码通过了编译并上传到了主板:
int led =13;
char password = 'a';
void setup() {
// initialize the digital pin as an output
// open the serial port:
Serial.begin(9600);
}
void loop() {
// check for incoming serial data:
if (Serial.available() > 0) {
// read incoming serial data:
char inChar = Serial.read();
// check if it matches
if (inChar == password){
for (int i=0; i<=10; i++)
blink();
}
}
}
void blink() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
然后我编写了以下python代码:
import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0x2341, idProduct=0x0043) # Arduino SA Uno R3
# was it found?
if dev is None:
raise ValueError('Device not found')
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
#assert ep is not None
# write the data
ep.write('a')
要激活对Arduino板USB端口的访问,我创建了以下UDEV规则文件,将其保存在/etc/udev/rules.d/下,然后重启我的系统:
SUBSYSTEM=="usb", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0043", MODE="0666", GROUP="root", OWNER="xxxx"
现在,当我运行我的python代码时,我总是收到此错误:
runfile('/home/xxxx/Data/dev/python/usb_test.py', wdir=r'/home/xxxx/Data/dev/python')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/bhan/Data/dev/python/usb_test.py", line 20, in <module>
dev.set_configuration()
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 799, in set_configuration
self._ctx.managed_set_configuration(self, configuration)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 128, in managed_set_configuration
self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 730, in set_configuration
_check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy
似乎Arduino驱动程序或其他东西占用了设备访问权限,但我不知道如何关闭或禁用它。我该如何解决这个问题?