ComPort for Android FT31xD Library 1.2 Full Source
ComPort for Android FT31xD Library 1.2 Full Source | 1 Mb
Delphi and C++ Builder communication library for FTDI FT311D and FT312D devices for Android.
Features:
Supports the complete FT311D/FT312D functionality
No special root access privileges needed
Available for Delphi/C++ Builder XE6 - 10.1
Source code included in full version
Royalty free distribution in applications
TComPort
Unit
Winsoft.Android.FTDI31XD
Types
type TI2cFrequency = (fr23, fr44, fr60, fr92);
I2C frequency in units of kHz. Possible values are 23kHz, 44kHz, 60kHz and 92kHz.
type TMode = (moUART, moGPIO, moPWM, moI2C, moSPISlave, moSPIMaster);
The interface mode selection for FT311D. For FT312D it has to be moUART.
type TPwmChannel = (ch0, ch1, ch2, ch3);
PWM channel number.
type TSpiClockPhase = (ph0, ph1);
The clock phase for SPI.
type TSpiClockPolarity = (po0, po1);
The clock polarity for SPI.
type TSpiDataOrder = (doMsb, doLsb);
Data order on the SPI bus.
type TUartDataBits = (db7, db8);
UART data bits.
type TUartFlowControl = (fcNone, fcCtsRts);
UART flow control.
type TUartParity = (paNone, paOdd, paEven, paMark, paSpace);
UART parity.
type TUartStopBits = (sb1, sb2);
UART stop bits.
Properties
property Accessory: JUsbAccessory; // read only
Android USB accessory.
property InputStream: JFileInputStream; // read only
USB input stream.
property OutputStream: JFileOutputStream; // read only
USB output stream.
property UsbManager: JUsbManager; // read only
Android USB manager.
Methods
function AccessoryOpened: Boolean;
Returns true if USB accessory was opened.
procedure ClearInput;
Clears input buffer.
procedure CloseAccessory;
Closes the USB accessory.
constructor Create(Mode: TMode);
Creates ComPort object with the specified interface mode selection.
procedure GpioConfig(InMap, OutMap: Byte);
Configures the GPIO. InMap parameter is bitmap of input port IOs, set bits will be configured as INPUT. OutMap parameter is bitmap of output port IOs, set bits will be configured as OUTPUT.
function GpioRead(var dаta: Byte; Timeout: Cardinal = 0): Boolean;
Returns true if GPIO read packet was received. FT311D sends sends the port data to Android layer, whenever there is a change in the signal levels on input ports.
Data parameter is GPIO status, only input pins are read.
Timeout parameter is read timeout measured in ms. Timeout 0 determines no waiting for data.
procedure GpioReset;
Resets the GPIO module, and configures all GPIOs as inputs.
procedure GpioWrite(dаta: Byte);
Writes FT311D GPIO output ports. The output configured ports will only be driven.
function I2cRead(DeviceAddress, TransferOptions: Byte; Count: Integer; Timeout: Cardinal; var Status: Byte): TArray;
Read I2C data.
Parameter DeviceAddress is 7-bit device address.
Parameter TransferOptions specifies the data transfer options:
Bit 0: if set then a start condition is generated in the I2C bus before the transfer begins.
Bit 1: if set then a stop condition is generated in the I2C bus after the transfer ends.
Bit 2: some I2C slaves require the I2C master to generate a NAK for the last data byte read. Setting this bit enables working with such I2C slaves.
Bit 3: the deviceAddress parameter is ignored if this bit is set. This feature may be useful in generating a special I2C bus conditions that do not require any address to be passed.
Bit 4..7: reserved
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Status is a bitmap of 8bits:
Bit 0: command status (0 - commmand passed, 1 - error)
Bit 1: 1 - NACK, device can't accept any more data
Bit 2: 1 - invalid address
Bit 3..7: reserved for future use
procedure I2cReset;
Sets the I2C interface to default state, 92kHz frequency.
procedure I2cSetFrequency(Frequency: TI2cFrequency);
Sets I2C frequency.
function I2cWrite(DeviceAddress, TransferOptions: Byte; dаta: TArray; Timeout: Cardinal; var Status: Byte; var DataWritten: Integer; Offset: Integer = 0; Count: Integer = -1): Boolean;
Writes the I2C data to slave. Returns true if the write operation was correctly completed.
Parameter DeviceAddress is 7-bit device address.
Parameter TransferOptions specifies the data transfer options:
Bit 0: if set then a start condition is generated in the I2C bus before the transfer begins.
Bit 1: if set then a stop condition is generated in the I2C bus after the transfer ends.
Bit 2: reserved Bit 3: the deviceAddress parameter is ignored if this bit is set. This feature may be useful in generating a special I2C bus conditions that do not require any address to be passed.
Bit 4..7: reserved
Parameters Data, Offset and Count specify data to be written, maximum is 252 bytes per transfer. If Count = -1, then all bytes from the specified Offset will be written.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Status is a bitmap of 8bits:
Bit 0: command status (0 - commmand passed, 1 - error)
Bit 1: 1 - NACK, device can't accept any more data
Bit 2: 1 - invalid address
Bit 3..7: reserved for future use
Parameter DataWritten contains actual number of bytes sent.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
function InputCount: Integer;
Returns the number of received bytes in input buffer.
procedure OpenAccessory;
Opens the USB accessory.
procedure PwmReset;
Brings the PWM interface to its default state. In the default state, the period is 1ms, and the duty cyle of all the channels is 0.
procedure PwmSetDutyCycle(Channel: TPwmChannel; DutyCycle: Byte);
Sets the duty cycle of specified PWM channel.
Parameter DutyCycle is the percentage value of the duty cycle: to set 50% duty cycle, specify this value as 50. Minimum is 5% and maximum is 95%.
procedure PwmSetPeriod(Period: Byte);
Sets the period of all PWM channels.
Parameter Period is the period in milliseconds. Possible values are 1ms to 250ms.
procedure SpiMasterConfig(Polarity: TSpiClockPolarity; Phase: TSpiClockPhase; DataOrder: TSpiDataOrder; ClockSpeed: Integer);
Configures clock polarity, clock phase, data order and clock speed of SPI master interface.
function SpiMasterRead(Count: Integer; Timeout: Cardinal): TArray;
Read SPI data.
Parameter Count determines the number of bytes to be read.
Parameter Timeout is read timeout measured in ms.
procedure SpiMasterReset;
Resets the SPI master interface into default state: clock frequency of 3MHz, clock phase to ph1, polarity to po0 and data order to MSB.
function SpiMasterSend(dаta: TArray; Timeout: Cardinal; Offset: Integer = 0; Count: Integer = -1): TArray;
Sends the specified data to SPI slave.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
Returns the data that was read while sending the data.
procedure SpiSlaveConfig(Polarity: TSpiClockPolarity; Phase: TSpiClockPhase; DataOrder: TSpiDataOrder);
Configures clock polarity, phase and data order of SPI slave interface.
function SpiSlaveRead(Count: Integer; Timeout: Cardinal): TArray;
Returns received data from SPI master.
Parameter Count determines the number of bytes to be read.
Parameter Timeout is read timeout measured in ms.
procedure SpiSlaveReset;
Resets the SPI slave interface of the FT311D. The default setting sets the clock phase to ph1 and polarity to po0.
function SpiSlaveSend(dаta: TArray; Timeout: Cardinal; Offset: Integer = 0; Count: Integer = -1): Integer;
Schedules the specified data to be sent to the SPI master. Eventually the SPI master has to initiate a read command to read this data.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
procedure UartConfig(BaudRate: Integer = 9600; DataBits: TUartDataBits = db8; StopBits: TUartStopBits = sb1; Parity: TUartParity = paNone; FlowControl: TUartFlowControl = fcNone);
Sets baud rate, data bits, stop bits, parity and flow control of FT311D/FT312D UART interface. Possible baud rates are 300 to 921600.
function UartRead(Count: Integer = -1; Timeout: Cardinal = 0): TArray;
Read data from the UART interface.
Parameter Count determines the requested number of bytes to be read. If Count is -1, then this method doesn't wait until some data is received, but returns already received data from input buffer.
Timeout is read timeout measured in ms. Timeout 0 determines no waiting for data.
function UartReadByte(Timeout: Cardinal = 0): Byte;
Read byte from the UART interface.
Parameter Timeout determines the read timeout and is measured in ms. Timeout 0 specifies no waiting for input data.
procedure UartSend(dаta: TArray; Offset: Integer = 0; Count: Integer = -1);
Sends the specified data to the UART interface. Maximum is 256 bytes per transfer.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
procedure UartSendByte(dаta: Byte);
Sends the specified byte to UART interface.
function WaitForInput(Count: Integer; Timeout: Cardinal = 0): Boolean;
Waits until the specified number of bytes is available in input buffer. Returns True if input data are available or False if timeout occured.
Timeout parameter is measured in ms. Timeout 0 determines no waiting for data.
Events
property OnRxChar: TNotifyEvent;
Occurs on each byte received from USB input stream. This event is raised directly from the communication thread, so use TThread.Queue or similar methods to synchronize code with the Android UI.
property OnRxFull: TNotifyEvent;
Occurs when input buffer is full. This event is raised directly from the communication thread, so use TThread.Queue or similar methods to synchronize code with the Android UI.
Unit
Winsoft.Android.FTDI31XD
Types
type TI2cFrequency = (fr23, fr44, fr60, fr92);
I2C frequency in units of kHz. Possible values are 23kHz, 44kHz, 60kHz and 92kHz.
type TMode = (moUART, moGPIO, moPWM, moI2C, moSPISlave, moSPIMaster);
The interface mode selection for FT311D. For FT312D it has to be moUART.
type TPwmChannel = (ch0, ch1, ch2, ch3);
PWM channel number.
type TSpiClockPhase = (ph0, ph1);
The clock phase for SPI.
type TSpiClockPolarity = (po0, po1);
The clock polarity for SPI.
type TSpiDataOrder = (doMsb, doLsb);
Data order on the SPI bus.
type TUartDataBits = (db7, db8);
UART data bits.
type TUartFlowControl = (fcNone, fcCtsRts);
UART flow control.
type TUartParity = (paNone, paOdd, paEven, paMark, paSpace);
UART parity.
type TUartStopBits = (sb1, sb2);
UART stop bits.
Properties
property Accessory: JUsbAccessory; // read only
Android USB accessory.
property InputStream: JFileInputStream; // read only
USB input stream.
property OutputStream: JFileOutputStream; // read only
USB output stream.
property UsbManager: JUsbManager; // read only
Android USB manager.
Methods
function AccessoryOpened: Boolean;
Returns true if USB accessory was opened.
procedure ClearInput;
Clears input buffer.
procedure CloseAccessory;
Closes the USB accessory.
constructor Create(Mode: TMode);
Creates ComPort object with the specified interface mode selection.
procedure GpioConfig(InMap, OutMap: Byte);
Configures the GPIO. InMap parameter is bitmap of input port IOs, set bits will be configured as INPUT. OutMap parameter is bitmap of output port IOs, set bits will be configured as OUTPUT.
function GpioRead(var dаta: Byte; Timeout: Cardinal = 0): Boolean;
Returns true if GPIO read packet was received. FT311D sends sends the port data to Android layer, whenever there is a change in the signal levels on input ports.
Data parameter is GPIO status, only input pins are read.
Timeout parameter is read timeout measured in ms. Timeout 0 determines no waiting for data.
procedure GpioReset;
Resets the GPIO module, and configures all GPIOs as inputs.
procedure GpioWrite(dаta: Byte);
Writes FT311D GPIO output ports. The output configured ports will only be driven.
function I2cRead(DeviceAddress, TransferOptions: Byte; Count: Integer; Timeout: Cardinal; var Status: Byte): TArray;
Read I2C data.
Parameter DeviceAddress is 7-bit device address.
Parameter TransferOptions specifies the data transfer options:
Bit 0: if set then a start condition is generated in the I2C bus before the transfer begins.
Bit 1: if set then a stop condition is generated in the I2C bus after the transfer ends.
Bit 2: some I2C slaves require the I2C master to generate a NAK for the last data byte read. Setting this bit enables working with such I2C slaves.
Bit 3: the deviceAddress parameter is ignored if this bit is set. This feature may be useful in generating a special I2C bus conditions that do not require any address to be passed.
Bit 4..7: reserved
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Status is a bitmap of 8bits:
Bit 0: command status (0 - commmand passed, 1 - error)
Bit 1: 1 - NACK, device can't accept any more data
Bit 2: 1 - invalid address
Bit 3..7: reserved for future use
procedure I2cReset;
Sets the I2C interface to default state, 92kHz frequency.
procedure I2cSetFrequency(Frequency: TI2cFrequency);
Sets I2C frequency.
function I2cWrite(DeviceAddress, TransferOptions: Byte; dаta: TArray; Timeout: Cardinal; var Status: Byte; var DataWritten: Integer; Offset: Integer = 0; Count: Integer = -1): Boolean;
Writes the I2C data to slave. Returns true if the write operation was correctly completed.
Parameter DeviceAddress is 7-bit device address.
Parameter TransferOptions specifies the data transfer options:
Bit 0: if set then a start condition is generated in the I2C bus before the transfer begins.
Bit 1: if set then a stop condition is generated in the I2C bus after the transfer ends.
Bit 2: reserved Bit 3: the deviceAddress parameter is ignored if this bit is set. This feature may be useful in generating a special I2C bus conditions that do not require any address to be passed.
Bit 4..7: reserved
Parameters Data, Offset and Count specify data to be written, maximum is 252 bytes per transfer. If Count = -1, then all bytes from the specified Offset will be written.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Status is a bitmap of 8bits:
Bit 0: command status (0 - commmand passed, 1 - error)
Bit 1: 1 - NACK, device can't accept any more data
Bit 2: 1 - invalid address
Bit 3..7: reserved for future use
Parameter DataWritten contains actual number of bytes sent.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
function InputCount: Integer;
Returns the number of received bytes in input buffer.
procedure OpenAccessory;
Opens the USB accessory.
procedure PwmReset;
Brings the PWM interface to its default state. In the default state, the period is 1ms, and the duty cyle of all the channels is 0.
procedure PwmSetDutyCycle(Channel: TPwmChannel; DutyCycle: Byte);
Sets the duty cycle of specified PWM channel.
Parameter DutyCycle is the percentage value of the duty cycle: to set 50% duty cycle, specify this value as 50. Minimum is 5% and maximum is 95%.
procedure PwmSetPeriod(Period: Byte);
Sets the period of all PWM channels.
Parameter Period is the period in milliseconds. Possible values are 1ms to 250ms.
procedure SpiMasterConfig(Polarity: TSpiClockPolarity; Phase: TSpiClockPhase; DataOrder: TSpiDataOrder; ClockSpeed: Integer);
Configures clock polarity, clock phase, data order and clock speed of SPI master interface.
function SpiMasterRead(Count: Integer; Timeout: Cardinal): TArray;
Read SPI data.
Parameter Count determines the number of bytes to be read.
Parameter Timeout is read timeout measured in ms.
procedure SpiMasterReset;
Resets the SPI master interface into default state: clock frequency of 3MHz, clock phase to ph1, polarity to po0 and data order to MSB.
function SpiMasterSend(dаta: TArray; Timeout: Cardinal; Offset: Integer = 0; Count: Integer = -1): TArray;
Sends the specified data to SPI slave.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
Returns the data that was read while sending the data.
procedure SpiSlaveConfig(Polarity: TSpiClockPolarity; Phase: TSpiClockPhase; DataOrder: TSpiDataOrder);
Configures clock polarity, phase and data order of SPI slave interface.
function SpiSlaveRead(Count: Integer; Timeout: Cardinal): TArray;
Returns received data from SPI master.
Parameter Count determines the number of bytes to be read.
Parameter Timeout is read timeout measured in ms.
procedure SpiSlaveReset;
Resets the SPI slave interface of the FT311D. The default setting sets the clock phase to ph1 and polarity to po0.
function SpiSlaveSend(dаta: TArray; Timeout: Cardinal; Offset: Integer = 0; Count: Integer = -1): Integer;
Schedules the specified data to be sent to the SPI master. Eventually the SPI master has to initiate a read command to read this data.
Parameter Timeout determines timeout for the response packet and is measured in ms.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
procedure UartConfig(BaudRate: Integer = 9600; DataBits: TUartDataBits = db8; StopBits: TUartStopBits = sb1; Parity: TUartParity = paNone; FlowControl: TUartFlowControl = fcNone);
Sets baud rate, data bits, stop bits, parity and flow control of FT311D/FT312D UART interface. Possible baud rates are 300 to 921600.
function UartRead(Count: Integer = -1; Timeout: Cardinal = 0): TArray;
Read data from the UART interface.
Parameter Count determines the requested number of bytes to be read. If Count is -1, then this method doesn't wait until some data is received, but returns already received data from input buffer.
Timeout is read timeout measured in ms. Timeout 0 determines no waiting for data.
function UartReadByte(Timeout: Cardinal = 0): Byte;
Read byte from the UART interface.
Parameter Timeout determines the read timeout and is measured in ms. Timeout 0 specifies no waiting for input data.
procedure UartSend(dаta: TArray; Offset: Integer = 0; Count: Integer = -1);
Sends the specified data to the UART interface. Maximum is 256 bytes per transfer.
Parameter Offset determines index of the first byte in Data array and Count is the number of bytes to be sent. If Count is -1, then all Data starting from the specified Offset will be sent.
procedure UartSendByte(dаta: Byte);
Sends the specified byte to UART interface.
function WaitForInput(Count: Integer; Timeout: Cardinal = 0): Boolean;
Waits until the specified number of bytes is available in input buffer. Returns True if input data are available or False if timeout occured.
Timeout parameter is measured in ms. Timeout 0 determines no waiting for data.
Events
property OnRxChar: TNotifyEvent;
Occurs on each byte received from USB input stream. This event is raised directly from the communication thread, so use TThread.Queue or similar methods to synchronize code with the Android UI.
property OnRxFull: TNotifyEvent;
Occurs when input buffer is full. This event is raised directly from the communication thread, so use TThread.Queue or similar methods to synchronize code with the Android UI.
Version 1.2
added Delphi/C++ Builder 10.1 files
added Delphi/C++ Builder 10.1 files
[/b]
[b] Only for V.I.P
Warning! You are not allowed to view this text.