|
| 1 | +import ftd2xx |
| 2 | +from ftd2xx import FTD2XX |
| 3 | + |
| 4 | +from mecompyapi.phy_wrapper.int_mecom_phy import ( |
| 5 | + IntMeComPhy, MeComPhyInterfaceException, MeComPhyTimeoutException |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +class MeComPhyFtdi(IntMeComPhy): |
| 10 | + """ |
| 11 | + Implements the IMeComPhy interface for the FTDI chip drivers. |
| 12 | + """ |
| 13 | + def __init__(self): |
| 14 | + """ |
| 15 | + Implements the IMeComPhy interface for the FTDI chip drivers. |
| 16 | + """ |
| 17 | + super().__init__() |
| 18 | + self.ftdi = None |
| 19 | + |
| 20 | + def mecom_set_default_settings(self, baudrate: int): |
| 21 | + """ |
| 22 | + Initializes the FTDI default settings, so that is it usually running |
| 23 | + with Meerstetter products. |
| 24 | +
|
| 25 | + This function is not part of the IMeComPhy interface. |
| 26 | + |
| 27 | + :param baudrate: Baud Rate for the Serial Interface. |
| 28 | + :type baudrate: int |
| 29 | + """ |
| 30 | + raise NotImplementedError |
| 31 | + |
| 32 | + def connect(self, id_str: str, timeout: int = 1, baudrate: int = 57600) -> None: |
| 33 | + """ |
| 34 | + Open a handle to an usb device by serial number(default), description or |
| 35 | + location(Windows only) depending on value of flags and return an FTD2XX |
| 36 | + instance for it. |
| 37 | +
|
| 38 | + :param id_str: ID string from listDevices |
| 39 | + :type id_str: str |
| 40 | + :param timeout: Time in seconds for read timeout. If timeout happens, read returns empty string. |
| 41 | + :type timeout: int |
| 42 | + :param baudrate: The baud rate setting. |
| 43 | + :type baudrate: int |
| 44 | + :raises SerialException: |
| 45 | + :raises InstrumentConnectionError: |
| 46 | + :return: None |
| 47 | + """ |
| 48 | + id_str_bytes: bytes = id_str.encode() |
| 49 | + self.ftdi: FTD2XX = ftd2xx.openEx(id_str=id_str_bytes) |
| 50 | + self.ftdi.purge(mask=3) # purges receive and transmit buffer in the device |
| 51 | + self.ftdi.setTimeouts(read=timeout * 1000, write=timeout * 1000) |
| 52 | + |
| 53 | + def tear(self): |
| 54 | + """ |
| 55 | + Tear should always be called when the instrument is being disconnected. It should |
| 56 | + also be called when the program is ending. |
| 57 | + """ |
| 58 | + self.ftdi.purge(mask=3) # purges receive and transmit buffer in the device |
| 59 | + self.ftdi.close() |
| 60 | + |
| 61 | + def send_string(self, stream: str): |
| 62 | + """ |
| 63 | + Sends data to the physical interface. |
| 64 | +
|
| 65 | + :param stream: The whole content of the Stream is sent to the physical interface. |
| 66 | + :type stream: str |
| 67 | + """ |
| 68 | + self.ftdi.purge(mask=3) # purges receive and transmit buffer in the device |
| 69 | + |
| 70 | + stream_bytes = stream.encode() |
| 71 | + |
| 72 | + self.ftdi.write(data=stream_bytes) |
| 73 | + |
| 74 | + def get_data_or_timeout(self): |
| 75 | + """ |
| 76 | + Tries to read data from the physical interface or throws a timeout exception. |
| 77 | +
|
| 78 | + Reads the available data in the physical interface buffer and returns immediately. |
| 79 | + If the receiving buffer is empty, it tries to read at least one byte. |
| 80 | + It will wait till the timeout occurs if nothing is received. |
| 81 | + Must probably be called several times to receive the whole frame. |
| 82 | +
|
| 83 | + :raises MeComPhyInterfaceException: Thrown when the underlying physical interface |
| 84 | + is not OK. |
| 85 | + :raises MeComPhyTimeoutException: Thrown when 0 bytes were received during the |
| 86 | + specified timeout time. |
| 87 | + """ |
| 88 | + try: |
| 89 | + # initialize response and carriage return |
| 90 | + cr = "\r".encode() |
| 91 | + response_frame = b'' |
| 92 | + response_byte = self.ftdi.read(nchars=1) # read one byte at a time, timeout is set on instance level |
| 93 | + |
| 94 | + # read until stop byte |
| 95 | + while response_byte != cr: |
| 96 | + response_frame += response_byte |
| 97 | + response_byte = self.ftdi.read(nchars=1) |
| 98 | + |
| 99 | + return response_frame.decode() |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + raise MeComPhyInterfaceException(f"Failure during receiving: {e}") |
| 103 | + |
| 104 | + def change_speed(self, baudrate: int): |
| 105 | + """ |
| 106 | + Used to change the Serial Speed in case of serial communication interfaces. |
| 107 | +
|
| 108 | + :param baudrate: Baud Rate for the Serial Interface. |
| 109 | + :type baudrate: int |
| 110 | + """ |
| 111 | + raise NotImplementedError |
| 112 | + |
| 113 | + def set_timeout(self, milliseconds: int): |
| 114 | + """ |
| 115 | + Used to modify the standard timeout of the physical interface. |
| 116 | +
|
| 117 | + :param milliseconds: |
| 118 | + :type milliseconds: int |
| 119 | + """ |
| 120 | + raise NotImplementedError |
0 commit comments