lsgunth / pyft232

Python bindings to d2xx and libftdi to access FT232 chips with the same interface as pyserial
GNU Lesser General Public License v2.1
11 stars 11 forks source link

AttributeError: module 'serial' has no attribute 'encode' #4

Closed rdpoor closed 6 years ago

rdpoor commented 6 years ago

I'm trying to bring up ft232 on my Win 10 32bit system under Python 3.6.4. I'm seeing the following:

PS C:\Users\Me> py
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> import ft232
>>> sp = ft232.Ft232(serial, baudrate=19200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ft232\d2xx.py", line 113, in __init__
    serial = c.create_string_buffer(port.encode())
AttributeError: module 'serial' has no attribute 'encode'

Of course, this could be a driver or configuration error with pyserial. Any recommendations on where to look?

(P.S.: I'm excited by the potential of pyft232 since it promises just what I need: bitbang access to the FT232R while maintaining pyserial and FTDI drivers.)

jrast commented 6 years ago

I think serial (in sp = ft232.Ft232(serial, baudrate=19200)) should be a FTDI Serial Number or a com port like COM3. It is (almost certainly) not the serial module which should be given.

The constructor of Ft232 can be found here: https://github.com/lsgunth/pyft232/blob/master/ft232/d2xx.py#L114

Note that D2xx is imported under another name in https://github.com/lsgunth/pyft232/blob/master/ft232/__init__.py

rdpoor commented 6 years ago

You are correct. The sample docs could be clearer that serial means serial_number, not a pySerial instance, perhaps along these lines:

>>> import ft232
>>> devices = ft232.list_devices()
>>> print(devices)
[(b'A932QG17', b'FT232R USB UART')]
>>> serial_number = devices[0][0].decode('utf-8')
>>> print(serial_number)
A932QG17
>>> sp = ft232.Ft232(serial_number)
>>> print(sp)
D2xx<id=0x3055650>(port='A932QG17', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=10, xonxoff=False, rtscts=False, dsrdtr=False)

I'll leave this open as an enhancement request, but I won't be upset if Mr. Gunthorpe simply closes it.

lsgunth commented 6 years ago

Yup, thanks. I've tweaked the example to be more clear.

Amigolinux commented 3 years ago

Hi All I am using the code to test, But is shows the error as below: image Can someone help to heck it?

lsgunth commented 3 years ago

The devices returned by list_devices() are strings not bytes. Calling decode on them is not correct. Just print it directly.

Amigolinux commented 3 years ago

Hi lsgunth

Thanks for your reply. Yes I can print the str. but How can I use ft232.Ft232 to open it?

image

nslee13066 commented 1 year ago

sp = ft232.Ft232(serial_number, baudrate=115200) is wrong, it should be sp = ft232.Ft232(serial_number=serial_number, baudrate=115200)