0
votes

I am still learning from the DLL process and since no Delphi FTDI Libmpsse library exist, I must work my own way through with the DLL from scratch.

However, I'm far from perfect in Delphi! I've been using it for few years without touching pointers.

This is the particular line I'm now stuck with (This is what I've tried to translate):

function I2C_DeviceWrite(
  handle              :dword;
  deviceAddress       :uint32;
  sizeToTransfer      :uint32;
  out buffer          :byte;
  out sizeTransferred :uint32;
  options             :uint32
):FT_Result; cdecl; external 'libmpsse.dll';

From the API User guide it is describe the C way like this:

FT_STATUS I2C_DeviceWrite(
  FT_HANDLE handle, 
  uint32    deviceAddress,
  uint32    sizeToTransfer, 
  uint8    *buffer, 
  uint32   *sizeTransferred,
  uint32    options )

Asterix means pointer in C from what I understand.

I don't know how to declare them properly yet.

Any hints?

NB: I'm still reading (XE2 Foundations) and searching about pointers and buffers the most I can!

Thank you!

BTW, API Guide is here for consulation:PDF from FTDI

EDIT:

var
Buffer: array of Byte;
written,towrite:cardinal;
begin
SetLength(Buffer,I2C_DEVICE_BUFFER_SIZE);
buffer[0] := $07;
towrite := 1;
i2c_openchannel(1,FT_HANDLE); //open channel 1
i2c_initchannel(FT_HANDLE,I2C_CLOCK_STANDARD_MODE and LATENCY_TIMER and 0);
i2c_devicewrite(FT_HANDLE,I2C_DEVICE_ADDRESS,towrite,buffer,written,I2C_TRANSFER_OPTIONS_START_BIT);
i2c_closechannel(FT_HANDLE); //close device

So far so good everything else work except the i2c_devicewrite line!
It gives me E2033:unable to compile actual and formal var parameters must be identical

1
OT: define a type of FT_HANDLE; use data types from the original code (types as UInt32 exist in Delphi for a while).TLama
Cardinals aren't the same? FT_HANDLE is defined like this in my code: Var --> FT_HANDLE : DWord = 0;//it must always be 0 from the APIELCouz
If the original code asks for uint32, declare your type as uint32 and you don't have to worry about whether or not cardinal is the same. If the type is already declared in Delphi, use the type that is already declared. How is FT_HANDLE defined in the user's guide exactly?Ken White
FT_Handle is defined as a DWORD. Thanks for the tip! Edited my question accordingly.ELCouz
Another shameless tip: my article Pitfalls of converting describes most of this. Also read Addressing pointers.Rudy Velthuis

1 Answers

2
votes

You've translated the byte array parameter incorrectly. You used

out buffer: byte

which is a single byte, passed out by reference. It should be

buffer: PByte

which here means a pointer to array of byte.

Then pass the address of the first element of the array. Like this:

@Buffer[0]

or like this

PByte(Buffer)

Note that I've not checked anything other than the byte array parameter.

FWIW, a constant length array may be simpler here than a dynamic array.

I'd also comment that you appear to be ignoring return values. Never do that. Always check return values for errors.