0

I have a simple binary byte array that I need to send to USB serial port using ftd2xx library in Python.

import ftd2xx as ft
import time

message = [86, 0xff, 0xff, 0, 0, 72, 40 ]

def openPort(index):
  s = r'\000' * 64
  port = ft.open(index, False)  # open port
  port.setUSBParameters(25600, 1024)    # setup necessary parameters
  port.setBaudRate(2000000)
  port.setDataCharacteristics(8, 0, 0)
  port.setTimeouts(16, 500)
  port.clrRts()  # power reset for attached hardware
  time.sleep(3)
  port.setRts()
  time.sleep(0.1)
  return port

cp = openPort(0)
cp.write(message)

In this case cp.write(message) throws WrongType exception. If I try to replace the array with message = b'V\xFF\xFF\x00\x00\x48\x28', it raises the same exception. In documentation I cannot see any hint what is in fact expected here. The original, underlying function in FTD2XX (FT_Write) will accept any byte buffer up to a certain size.

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Passing a `bytes` object (i.e. your second attempt) should work according to https://github.com/snmishra/ftd2xx/blob/master/ftd2xx/ftd2xx.py. Please show the exact error message with traceback. – mkrieger1 May 07 '22 at 16:40
  • @mkrieger1 Oops, now it works! I wonder what I did wrong when debugging variant 2... Now I need to find some docs how to build such object from byte array or byte list. – Jindrich Vavruska May 07 '22 at 17:54
  • Maybe this one? https://stackoverflow.com/questions/30658193/python3-how-to-make-a-bytes-object-from-a-list-of-integers – mkrieger1 May 07 '22 at 18:04
  • That was too easy :) thanks. I was probably going to try it anyway. OT: but I have a different problem - sending the message to the hardware does not return the expected response, and inside the library no one checks what is the return status of calling the FT_.. functions. The same scenario in VB.NET causes all calls except FT_Open to return status 1 (FT_INVALID_HANDLE) and since ther's no data exchange it seems to be the same in Python except I cannot see the error status. But thanks, it was so pleasant to try python after a long time. So clean, straight compared to VB.NET. Rapid coding. – Jindrich Vavruska May 07 '22 at 18:23

0 Answers0