ELC*_*ouz 2 delphi usb dll i2c ftdi
我知道一些 Delphi 的基础知识(实际上我已经使用它几年了)......
我正在用 DLL 撞墙(从来没有真正玩过这个)。
考虑这个例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type FT_Result = Integer;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
FT_HANDLE : DWord = 0;
implementation
{$R *.dfm}
function I2C_GetNumChannels(numChannels: dword):FT_Result; stdcall; external 'libmpsse.dll' name 'I2C_GetNumChannels';
function I2C_OpenChannel(index:dword;handle:pointer):FT_Result; stdcall; external 'libmpsse.dll' name 'I2C_OpenChannel';
procedure TForm1.Button1Click(Sender: TObject);
var
numofchannels:dword;
begin
i2c_getnumchannels(numofchannels);
showmessage(inttostr(numofchannels));
end;
end.
Run Code Online (Sandbox Code Playgroud)
我需要从 FTDI 连接 libmpsse.dll 以访问 USB 端口上的 I2C 设备。
当我调用函数 I2C_GetNumChannels 时,我得到了大量的 AccessViolation ......
我只想知道dll函数有什么问题?
I2C_GetNumChannels 也应该返回 2 个值......

从这里的官方 API 指南 --> http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf
非常感谢!
问候
你的翻译不正确。它应该是:
function I2C_GetNumChannels(out numChannels: Longword): FT_Result;
stdcall; external 'libmpsse.dll';
Run Code Online (Sandbox Code Playgroud)
您正在调用的函数接受 32 位无符号整数的地址。您的翻译按值传递了一个 32 位无符号整数。
您可以使用指针来翻译导入,但调用者使用 avar或out参数更容易做到这一点,就像我所做的那样。
我假设您已正确确定调用约定是stdcall. 您需要检查头文件才能确定。
您应该检查从函数调用返回的值是否有错误。这是人们在调用外部库时最常犯的一个错误。不要忽略返回值。检查错误。