|
My code can open the serial port, but cannot search for the device
unit UIDLL_VAR;
interface
Type Params = record
Port:array[0..2] of char;
CtrlID:array[0..2] of char;
Param:array[0..1023] of char;
IPPort:array[0..4] of char;
IPAddr:array[0..15] of char;
End;
Function SearchCtrl( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function OpenComm (var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function CloseComm( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function CollectAll( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
var
userid,user_name:string;
implementation
end.
////////////////////
unit URRMTEST;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TFRMTEST = class(TForm)
Button1: TButton;
PB1: TProgressBar;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
GroupBox1: TGroupBox;
RB1: TRadioButton;
RB2: TRadioButton;
Button2: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{Private declarations}
public
{Public declarations}
end;
var
FRMTEST: TFRMTEST;
implementation
uses
uidll_var,ufunction;
{$R *.dfm}
procedure TFRMTEST.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;
procedure TFRMTEST.Button1Click(Sender: TObject);
var
j,t,b,k,h :integer;
p:Params;
s:string;
begin
if rb1.Checked then
begin
p.Port[0]:='0';
p.Port[1]:='0';
p.Port[2]:='1';
//p.Port:='001';
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='1';
p.Param[0]:='9';
p.Param[1]:='6';
p.Param[2]:='0';
p.Param[3]:='0';
//p.Param[4]:='0';
//p.Param:='9600';
j:=2100;
end
else
begin
p.Port[0]:='0';
p.Port[1]:='0';
p.Port[2]:='1';
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='1';
p.Param[0]:='1';
p.Param[1]:='9';
p.Param[2]:='2';
p.Param[3]:='0';
p.Param[4]:='0';
j:=2201;
end;
b:=openComm(p,j);
showmessage(inttostr(b)+' OK');
if b=105 then
begin
showmessage('Serial port opened successfully!!');
k:=1;
PB1.Max:=strtoint(edit2.Text);
pb1.Step:= 1;
while k<= strtoint(edit2.Text) do
begin
case k of
1:
begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='1';
end;
2: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='2';
end;
3: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='3';
end;
4: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='4';
end;
5: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='5';
end;
6: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='6';
end;
7: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='7';
end;
8: begin
P.CtrlID[0]:='0';
P.CtrlID[1]:='0';
P.CtrlID[2]:='8';
end;
end;
s:= inttostr(k);
//showmessage(p.Port+''+p.CtrlID);
p.Param:='';
b:=SearchCtrl(p,j);
//showmessage(inttostr(b));
if b=105 then
begin
showmessage('Successfully searched for the device!!'+s[1]);
Break;
end
else
begin
showmessage('The device is not found successfully'+s[1]);
end;
pb1.StepIt;
//GB2.Caption:= fla(k/strtoint(edit2.Text))+'%';
k:=k+1;
end;
//end;
end
else
showmessage('Serial port opening is not successful');
//b:=SearchCtrl(p,j);
end;
procedure TFRMTEST.Button2Click(Sender: TObject);
var
s,l:string;
i:integer;
begin
{The following two methods can get the directory where the current application is located}
getdir(0,s);
showmessage(s);
s:=extractfilepath(paramstr(0));
showmessage(s);
//**************************************//
l:=DateTimeToStr(Now);
showmessage(l);
showmessage(inttostr(length(l)));
i:=length(l);
case i of
17:
begin
s:='data'+copy(l,1,4)+copy('00'+copy(l,6,1),2,2)+copy('00'+copy(l,8,1) ,2,2)+copy(l,10,2)+copy(l,13,2)+copy(l,15,2);
showmessage(s);
end;
18:
begin
s:=copy(l,11,2)+copy(l,14,2)+copy(l,17,2);
if copy(l,) = then
showmessage(s);
end;
19:
begin
s:='data'+copy(l,1,4)+copy('00'+copy(l,6,1),2,2)+copy('00'+copy(l,8,1) ,2,2)+copy(l,10,2)+copy(l,13,2)+copy(l,15,2);
showmessage(s);
end;
end;
//showmessage(inttostr(year()));
//showmessage(FormatDateTime('"The meeting is on "dddd, mmmm d, yyyy," at "hh:mm AM/PM', Now + 0.125));
end;
end. |
|