Delphi Sample Code for callback and learnir

Interested in integrating USB-UIRT support into an application? Look here!

Moderator: jrhees

Delphi Sample Code for callback and learnir

Postby DanG » Fri Dec 15, 2006 7:41 pm

Any one have sample code in Delphi that uses the UUIRTSetReceiveCallback and UUIRTLearnIR.
I am using the ported unit UIRT.pas from VB, but this unit does not include the above 2 functions.

Also. Jon if you can send me the API docs that would be great.

Thanks
DanG
 
Posts: 2
Joined: Fri Dec 15, 2006 6:56 pm

Postby jrhees » Wed Dec 20, 2006 5:39 am

Here is some sample Delphi code I got working quite some time ago:

unit UIRT;

interface

uses SysUtils, Classes, Windows, SyncObjs, Dialogs;

// 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 = 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 = tUuInfo;

TUIRT = class

private

FDrvHandle: longint; // Handle used to communicate with the UIRT.

udata: integer;

protected

// Open the UIRT and get version information from it.

procedure openUIRT;

procedure closeUIRT;

public

Info: tUuInfo;

constructor Create;

destructor Destroy; override;

procedure transmit(IRCode: string);

function GetConfig: integer;

function SetConfig(config: integer): integer;

end;

procedure Rcv(p: pChar; userData: longint); stdcall;

implementation

uses TestUIRT;

type

TLongPtr = ^longint;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// USB-UIRT DLL API Function Declarations....

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 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

inherited Destroy;

end;

// ---------------------------------------------------------------

procedure TUIRT.openUIRT;

var

drvAPIVersion: longint;

drvDLLVersion: longint;

res: longint;

dllErr: longint;

sErrMsg: string;

begin

//ShowMessage('TUIRT.OpenUIRT');

udata := 166;


// 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();

// 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

res := UUIRTGetUUIRTInfo(FDrvHandle, Info);

if res = 0 then

raise Exception.Create('GetUUIRTInfo falied!');

// 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(FDrvHandle, integer(@Rcv), integer(self));

end;

end;

// ---------------------------------------------------------------

procedure TUIRT.closeUIRT;

begin

if FDrvHandle <> 0 then UUIRTClose(FDrvHandle);

end;

// ---------------------------------------------------------------

function TUIRT.SetConfig(config: integer): integer;

begin

// Result = 1 if Set ok, otherwise it will = 0.

Result := UUIRTSetUUIRTConfig(FDrvHandle, config);

end;

// ---------------------------------------------------------------

function TUIRT.GetConfig: integer;

var

uconfig: longint;

begin

if UUIRTGetUUIRTConfig(FDrvHandle, uconfig) <> 0 then begin

Result := uconfig;

end else begin

Result := -1;

end;

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;

procedure Rcv(p: pChar; userData: longint); stdcall;

var

myObj: TUIRT;

j: integer;

begin

myObj := TUIRT(userData);

Form1.Memo1.Lines.Add(' udata='+IntToStr(myObj.udata));

Form1.Memo1.Lines.Add(' IREventStr='+String(p));

end;

end
jrhees
Site Admin
 
Posts: 1652
Joined: Tue Jan 28, 2003 11:49 pm

Postby DanG » Wed Dec 20, 2006 1:16 pm

Thanks Jon

Thats solved my problem.

Your support is excellent.
DanG
 
Posts: 2
Joined: Fri Dec 15, 2006 6:56 pm


Return to Developers

Who is online

Users browsing this forum: No registered users and 20 guests

cron