vpaeder / pymcp2221

A python driver for the Microchip MCP2221/MCP2221A USB 2.0 to I2C/UART protocol converters
https://pypi.org/project/pymcp2221
MIT License
5 stars 2 forks source link

Device not connected? #6

Closed enok71 closed 1 year ago

enok71 commented 1 year ago

I keep getting "Device not connected" error whatever I do. E.g. this simple code:

`import mcp2221

devices=mcp2221.find_devices() device=devices[0] print(device)

m=mcp2221.MCP2221(device) print(m.gpio0_read_function()) result: {'path': b'3-5.3.1:1.2', 'vendor_id': 1240, 'product_id': 221, 'serial_number': '', 'release_number': 256, 'manufacturer_string': '', 'product_string': '', 'usage_page': 0, 'usage': 0, 'interface_number': 2} Traceback (most recent call last): File "/home/dinbox/Dinbox/tbb/./flash2221.py", line 10, in print(m.gpio0_read_function()) File "/home/dinbox/Dinbox/tbb/venv/lib/python3.10/site-packages/mcp2221/init.py", line 1284, in gpio0_read_function ret = self._gpio_read_function(0, mem) File "/home/dinbox/Dinbox/tbb/venv/lib/python3.10/site-packages/mcp2221/init.py", line 1248, in _gpio_read_function return self._read_sram_byte(SramDataSubcode.GPSettings, gpio_num, [0, 1, 2]) File "/home/dinbox/Dinbox/tbb/venv/lib/python3.10/site-packages/mcp2221/init.py", line 429, in _read_sram_byte ret = self._read_sram(code) File "/home/dinbox/Dinbox/tbb/venv/lib/python3.10/site-packages/mcp2221/init.py", line 331, in _read_sram data = self._write(0x61) File "/home/dinbox/Dinbox/tbb/venv/lib/python3.10/site-packages/mcp2221/init.py", line 235, in _write raise IOException("Device not connected.") mcp2221.exceptions.IOException: Device not connected. `

What am I doing wrong? Obviously it is connected.

enok71 commented 1 year ago

It turns out I can run the above code successfully as root. But why not as the ordinary user who has write access to both the serial device /dev/ttyACM0 and the i2c device /dev/i2c-17 in my case?

The error message "Device not connected" is a bit misleading also.

vpaeder commented 1 year ago

I'd say it's related to hidapi, for which you need specific udev rules (see here).

enok71 commented 1 year ago

Ok thanks!

enok71 commented 1 year ago

I can confirm that udev rules work well. I used the following rules to set up permissions:

UBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00dd", MODE="0660", GROUP="plugdev", TAG+="uaccess", TAG+="udev-acl"
KERNEL=="hidraw*", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00dd", MODE="0660", GROUP="plugdev", TAG+="uaccess", TAG+="udev-acl"
# stop the ModemManager from trying to use this port as a modem!
ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00dd", ENV{ID_MM_DEVICE_IGNORE}="1"

Maybe an error message in pymcp2221 mentioning permissions failure instead of "device not connected" would still be helpful though, if it can be achieved?

vpaeder commented 1 year ago

Ok nice. Yes that's indeed a better behaviour. I fixed the open method to throw an IOException if it fails.