Once the package is installed in Delphi, an ActiveHome component is
added to its IDE's
ActiveX palette. The TActiveHome component is
pretty sparse. It has an
overloaded function, SendAction, which is
used to send messages to the
CM15A with different numbers of parameters :
function SendAction(bszAction: OleVariant): OleVariant; overload;
function SendAction(bszAction, bstrParam: OleVariant): OleVariant;
overload;
function SendAction(bszAction, bstrParam, vReserved1:
OleVariant): OleVariant; overload
function SendAction(bszAction, bstrParam,
vReserved1, vReserved2: OleVariant): OleVariant; overload;
SendAction("sendplc",
"B9 ON").
Of course, you will probably want to change the address B9 to the
address of a nearby device to easily verify that the command was send
over the power line. TActiveHome
component (make it invisible), a memo, and a button on a form and in the button's OnClick
event handler add this: function ovtoint(value: OleVariant): integer;
begin
result := value;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
res: OleVariant;
Action: string;
Param: string;
begin
Action := 'sendplc';
Param := 'B9 ON'; // case insensitive
res := ActiveHome1.SendAction(Action, Param);
Memo1.Lines.add(Format('Send: "%s" "%s", result =
%d', [Action, Param, ovtoint(res)]));
end;ovtoint,
that
converts an OleVariant to an integer, is
needed because Format does not know how to
handle an OleVariant directly.Action
:= 'sendplc' to Action :=
'sendrf'. ActiveHomeScriptLib_TLB.dcu has not
been found, you can
move the file from the folder C:\Users\{user}\Documents\RAD
Studio\7.0\Imports to the folder C:\Program
Files (x86)\Embarcadero\RAD
Studio\7.0\Imports which contains many other imported type
library
units and which is in Delphi 2010's default search path.SendFunction? Strictly speaking
none it would seem. Change Param := 'B9 ON'
to 'B9 ONF' and run
the example again. You will get a rather lengthy generic
syntax error
message :
SendFunction does not
return an error code when it cannot parse 'ONF', instead it raises an
exception. Furthermore, SendFunction
will not report a syntax
error when Param := 'B9ON'
while
that is clearly an error since the device will not be turned on.
Similarly, using the 3 strings version of SendAction('sendplc',
'A9', 'on') will not work and yet will neither raise an
exception nor return an error code. On the other hand, the 4 strings
version SendAction('sendplc', 'A', '9', 'on')
will raise an exception.
cm15_test.dpr
cm15_test.res
Unit1.pas
Unit1.dfm
OnRecvAction
it is of type TActiveHomeRecvAction which has
the following declaration :
TActiveHomeRecvAction =
procedure(ASender: TObject;
bszAction: OleVariant;
bszParm1, bszParm2, bszParm3, bszParm4,
bszParm5:
OleVariant;
bszReserved: OleVariant) of object;
OleVariant
results to strings and concatenates them all to a single string that is
copied to the memo. There's only one problem. It does not work.