Guest,
I don't use dynamic loading, I use static loading. I have included my Delphi unit for managing the UIRT so you can take a look at it. Obviously a good portion of it is code ported from the VB app.
Note to Jon: Let me know if this post is too long and I'll remove it.
Edited: It looks like the forum is stripping the tabs, don't know how to fix it.
Here's the code:
unit UIRT;
interface
uses SysUtils, Classes, Windows, SyncObjs;
// USB-UIRT DLL API Constants...
const
CRLF: string = '\r\n';
INVALID_HANDLE_VALUE: integer = -1;
ERROR_IO_PENDING: integer = 997;
UUIRTDRV_ERR_NO_DEVICE: integer = $20000001;
UUIRTDRV_ERR_NO_RESP: integer = $20000002;
UUIRTDRV_ERR_NO_DLL: integer = $20000003;
UUIRTDRV_ERR_VERSION: integer = $20000004;
UUIRTDRV_CFG_LEDRX: integer = $1;
UUIRTDRV_CFG_LEDTX: integer = $2;
UUIRTDRV_CFG_LEGACYRX: integer = $4;
UUIRTDRV_IRFMT_UUIRT: integer = $0;
UUIRTDRV_IRFMT_PRONTO: integer = $10;
UUIRTDRV_IRFMT_LEARN_FORCERAW: integer = $100;
UUIRTDRV_IRFMT_LEARN_FORCESTRUC: integer = $200;
UUIRTDRV_IRFMT_LEARN_FORCEFREQ: integer = $400;
UUIRTDRV_IRFMT_LEARN_FREQDETECT: integer = $800;
UUIRTDRV_IRFMT_TRANSMIT_DC: integer = $80;
type
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Structure definitions:
// Define a structure for received IR codes.
// Received IR codes are 12-ASCII bytes longint, and are passed via a callback
// as a null-terminted string. Since VB handles strings differently, we
// define a byte array and convert it to a VB string inside the callback function.
tIRCode = record
codeData: array[0..15] of byte;
end;
TDynamicIRCodeArray = array of tIRCode;
// Define a structure to hold UUINFO data for the UUIRTGetUUIRTInfo function call...
tUuInfo = record
fwVersion: longint;
protVersion: longint;
fwDateDay: byte;
fwDateMonth: byte;
fwDateYear: byte;
end;
TDynamicUuInfoArray = array of tUuInfo;
TUIRTCommThread = class;
TUIRT = class
private
FDrvHandle: longint; // Handle used to communicate with the UIRT.
FUIRTCommThread: TUIRTCommThread;
protected
// Open the UIRT and get version information from it.
procedure openUIRT;
procedure closeUIRT;
public
constructor Create;
destructor Destroy; override;
procedure transmit(IRCode: string);
end;
TUIRTCommThread = class(TThread)
protected
FUIRT: TUIRT;
public
constructor Create(uirt: TUIRT);
procedure Execute; override;
end;
implementation
type
TLongPtr = ^longint;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// USB-UIRT DLL API Function Declarations....
// function UUIRTGetDrvInfo(var uVersion: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTGetDrvVersion';
function UUIRTGetDrvInfo(var uVersion: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTGetDrvInfo';
function UUIRTGetDrvVersion(var uVersion: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTGetDrvVersion';
function UUIRTOpen : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTOpen';
// function UUIRTOpen : TLongPtr; stdcall; external 'uuirtdrv.dll' name 'UUIRTOpen';
function UUIRTClose(hHandle: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTClose';
function UUIRTGetUUIRTInfo(hHandle: longint; var UuInfo: tUuInfo) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTGetUUIRTInfo';
function UUIRTGetUUIRTConfig(hHandle: longint; var uConfig: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTGetUUIRTConfig';
function UUIRTSetUUIRTConfig(hHandle: longint; uConfig: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTSetUUIRTConfig';
function UUIRTTransmitIR(
hHandle: longint;
sIRCode: PChar;
uCodeFormat: longint;
uRepeatCount: longint;
uInactivityWaitTime: longint;
hEvent: longint;
reserved0: longint;
reserved1: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTTransmitIR';
function UUIRTLearnIR(
hHandle: longint;
uCodeFormat: longint;
szIRCode: longint;
pProgressProc: longint;
userData: longint;
var pAbort: boolean;
uParam1: longint;
hEvent: longint;
reserved0: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTLearnIR';
function UUIRTSetReceiveCallback(
hHandle: longint;
pReceiveProc: longint;
userData: longint) : longint; stdcall; external 'uuirtdrv.dll' name 'UUIRTSetReceiveCallback';
// ---------------------------------------------------------------
constructor TUIRT.Create;
begin
inherited Create;
openUIRT;
end;
destructor TUIRT.Destroy;
begin
closeUIRT;
inherited Destroy;
end;
// ---------------------------------------------------------------
procedure TUIRT.openUIRT;
var
drvAPIVersion: longint;
drvDLLVersion: longint;
res: longint;
dllErr: longint;
sErrMsg: string;
// tlp: TLongPtr;
begin
// Get USB-UIRT *driver* information. Note that this call does not actually
// communicate with the USB-UIRT hardware. Instead, its purpose is solely to
// retrieve version information about the API *driver*. This and GetDrvVersion are
// the only API calls (besides UUIRTOpen) which can be performed without a driver
// handle (prior to opening the driver via UUIRTOpen).
res := UUIRTGetDrvInfo(drvAPIVersion);
if res = 0 then
raise Exception.Create('(TUIRT.open) Unable to retrieve uuirtdrv version!');
// Check the API version. This Application is written to be compatible with
// driver API version 1.0 (0x100)
if drvAPIVersion <> $100 then
raise Exception.Create('(TUIRT.open) Invalid uuirtdrv version!');
// Get the Driver DLL Revision #. This is necessary for this VB app since it this
// app relies on a newer feature of the LearnIR() function which does not block. This
// new non-blocking ability is only available in DLL versions 2.6.1 or later.
// note: This API call does not exist in versions prior to 2.6.1, so we use try/except
// to trap this condition.
res := 0;
try
res := UUIRTGetDrvVersion(drvDLLVersion);
except
On E: Exception do; // Swallow exception.
end;
if (res = 0) or (drvDLLVersion < 2610) then
raise Exception.Create(
'(TUIRT.open) This application requires USB-UIRT API driver DLL (UUIRTDRV.DLL) version 2.6.1 or later. ' +
CRLF +
'Please verify you are running the latest API DLL and that you''re using the latest version of USB-UIRT firmware!' +
CRLF +
'If the problem persists, contact Technical Support!');
// Open a communications link with the USB-UIRT...
// Obtain a handle to the USB-UIRT via the UUIRTOpen call. if successful, a handle
// is returned which we *must* use in all subsequent APi calls.
FDrvHandle := UUIRTOpen();
// FDrvHandle := 0;
// FUIRTCommThread := TUIRTCommThread.Create(Self);
// if Open call failed, we will be returned an INVALID_HANDLE_VALUE and need to look
// at the error to determine what went wrong...
if FDrvHandle = INVALID_HANDLE_VALUE then
begin
// Get the last DLL error using the WIN API GetLastError() call.
dllErr := GetLastError();
if dllErr = UUIRTDRV_ERR_NO_DLL then
sErrMsg := ('Unable to find USB-UIRT Driver. Please make sure driver is Installed!')
else if dllErr = UUIRTDRV_ERR_NO_DEVICE then
sErrMsg := ('Unable to connect to USB-UIRT device! Please ensure device is connected to the computer!')
else if dllErr = UUIRTDRV_ERR_NO_RESP then
sErrMsg := ('Unable to communicate with USB-UIRT device! Please check connections and try again. if you still have problems, try unplugging and reconnecting your USB-UIRT. if problem persists, contact Technical Support!')
else if dllErr = UUIRTDRV_ERR_VERSION then
sErrMsg := ('Your USB-UIRT//s firmware is not compatible with this API DLL. Please verify you are running the latest API DLL and that you//re using the latest version of USB-UIRT firmware! if problem persists, contact Technical Support!')
else
sErrMsg := ('Unable to initialize USB-UIRT (unknown error)!');
raise Exception.Create('(TUIRT.open) ' + sErrMsg);
end
else
begin
// NOTE!!!: Learn function sample code and associated callback usage is not implemented yet.
// if Open was successful, we//ll want to register a receive callback function
// for the USB-UIRT API to call each time an IR code is received.
// res = UUIRTSetReceiveCallback(hDrvHandle, AddressOf IRReceiveCallback, Me);
end;
end;
// ---------------------------------------------------------------
procedure TUIRT.closeUIRT;
begin
if FDrvHandle <> 0 then
UUIRTClose(FDrvHandle);
end;
// ---------------------------------------------------------------
procedure TUIRT.transmit(IRCode: string);
var
res: longint;
IRCodeFormat: longint;
begin
If FDrvHandle = 0 then
raise Exception.Create('(TUIRT.transmit) UIRT communication handle is null.');
IRCodeFormat := UUIRTDRV_IRFMT_UUIRT;
res := UUIRTTransmitIR(FDrvHandle,
PChar(IRCode),
IRCodeFormat,
1,
0,
0,
0,
0);
if res = 0 then
raise Exception.Create('(TUIRT.transmit) UIRT transmit failed.');
end;
Thanks.