Nano.M.H

Mikrokontroller

materi

materi  SBP

{******************************************************************************}
{                                                                              }
{                              ActiveXperts                                    }
{                                                                              }
{                Active Comport: Delphi_Console_Query_Device                   }
{                                                                              }
{                                                                              }
{                                                                              }
{                                                                              }
{                                                                              }
{                                                                              }
{******************************************************************************}
program Delphi_Console_Query_Device;

{$APPTYPE CONSOLE}

uses
SysUtils, ActiveX,
ACOMPORTLib_TLB in ‘..\..\Imports\ACOMPORTLib_TLB.pas’;

var
objComport: ComPort;
strBaudRate:string;

{==============================================================================}
{= W r i t e   C o m m a n d ==================================================}
{==============================================================================}
function WriteCommand(objComport:ComPort ):boolean;
var
str:string;
begin
Writeln(‘Enter command (enter QUIT to stop the program):’);
Write(‘ > ‘);

readln(str);
objComport.WriteString(str);

if(str=’QUIT’)then WriteCommand := true;
// str <> ‘QUIT’;
end;

{==============================================================================}
{= R e a d   r e s p o n s e ==================================================}
{==============================================================================}
procedure ReadResponse(objcomport:Comport);
var
str:string;
begin
str:=’Notempty’;
objcomport.Sleep(200);

while(str <>”)do
begin
str:= objcomport.ReadString;
if(str<>”)then writeln(‘  <- ‘+str);
end;

end;

{==============================================================================}
{= A s k ======================================================================}
{==============================================================================}
function Ask(strTitle:string;bAllowEmpty:boolean):string;
var
strInput,
strReturn:string;
begin
strInput:= ”;
strReturn := ”;

writeLn(strTitle);
write(‘ > ‘);
readln(strInput);
if(length(strInput)>1)then strReturn:=strInput;

while((strReturn=”)and(bAllowEmpty=false))do
begin
write(‘ > ‘);
readln(strInput);
if(length(strInput)>1)then strReturn:=strInput;
end;

Ask:=strReturn;
end;

{==============================================================================}
{= A s k   d e v i c e ========================================================}
{==============================================================================}
function AskDevice(objComport:ComPort):string;
var
strInput:string;
strDevice:string;

i,j:integer;

begin
strInput:=”;
strDevice:=”;

WriteLn(‘Select a device: ‘);
for i := 1 to 8 do
begin
WriteLn(‘  ‘ + inttostr(i) + ‘: COM’ + inttostr(i));
end;

for j := 0 to objComport.GetDeviceCount() – 1 do
begin
WriteLn(‘  ‘ + IntToStr(i + j) + ‘: ‘ + objComport.GetDevice(j));
end;

while (strDevice = ”)do
begin
Write(‘  > ‘);
readln(strInput);
if ((StrToInt(strInput) < i))then
strDevice := ‘COM’ + strInput
else
if (StrToInt(strInput) < i + j)then
strDevice := objComport.GetDevice(StrToInt(strInput) – i);
end;

WriteLn(‘  Selected device: ‘ + strDevice);

AskDevice:= strDevice;
end;

{==============================================================================}
{==============================================================================}
{= T H E   P R O G R A M   I T S E L F ========================================}
{==============================================================================}
{==============================================================================}
begin
CoInitialize(nil);
strBaudRate:=”;

{To log, assign a valid filename}
// objComport.LogFile := ‘C:\Logfile.txt’;

objComport := TComPort.Create(nil).DefaultInterface;
objComport.Device := AskDevice(objComport);

{If a direct port is used, query for the baudrate}
if(copy(objComport.Device,0,3)=’COM’)then
begin
strBaudRate := Ask( ‘Enter baudrate (empty string means: default baudrate):’, true );
if( strBaudRate = ” )then strBaudRate := ’0′;

objComport.BaudRate := strToInt(strBaudRate);
end;

objComport.Open;
WriteLn(‘Open, result: ‘ + IntToStr(objComport.LastError) + ‘ (‘ + objComport.GetErrorDescription(objComport.LastError) + ‘)’);

if( objComport.LastError = 0 )then
begin
ReadResponse( objComport );
while( WriteCommand( objComport )=false)do
begin
ReadResponse( objComport );
end;
objComport.Close();
WriteLn(‘Close, result: ‘ + IntToStr(objComport.LastError) + ‘ (‘ + objComport.GetErrorDescription(objComport.LastError) + ‘)’);
end;

WriteLn(‘Ready.’);
sleep(3000);
writeln(‘Done. Press <ENTER> to close this window’);readln;
end.

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.