Arsip: Comport


by reza in Networking more 13 years ago 1848
Comport
reposting artikel rekan eksant
unit CommExample;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, ExtCtrls;
type
TCommDemoObject = class(TObject)
private
public
commPortHandle:THandle;
dcbInfo:Tdcb;
errorFlag:Boolean;
commPortError:DWORD;
commPortStatus:TComStat;
numberOfCharsActuallyWritten:DWORD;
//     numberOfCharsToRead:DWORD;
     numberOfCharsActuallyRead:DWORD;
commPortInputBuffer:array[0..100] of Char;
commPortOpen:Boolean;
function  OpenCommPort(whichPort:String):Boolean;
procedure   CloseCommPort;
procedure   TransmitSingleChar(theChar:Char);
procedure   TransmitString(theString:String);
function  ReadCommPort:Integer;
end;
implementation
function  TCommDemoObject.OpenCommPort(whichPort:String):Boolean;
begin
// undo comm port if open
   if (commPortOpen) then
CloseCommPort();
// set flag state
   commPortOpen:=false;
// open COMM port
   // use 'COM1','COM2','COM3', and 'COM4' to choose which port
   // to open
   commPortHandle:=CreateFile(PChar(whichPort),GENERIC_READ OR
GENERIC_WRITE,0,
nil,OPEN_EXISTING,0,0);
//   commPortHandle:=CreateFile('COM1',GENERIC_READ OR GENERIC_WRITE,0,
//     nil,OPEN_EXISTING,0,0);
   // exit on failure
   if (commPortHandle=INVALID_HANDLE_VALUE) then
begin
Result:=false;
exit;
end;
// get comm port state
   errorFlag:=GetCommState(commPortHandle,dcbInfo);
// bail on failure
   if (NOT(errorFlag)) then
begin
CloseHandle(commPortHandle);
Result:=false;
exit;
end;
// setup COMM port settings
   dcbInfo.BaudRate:=9600;
dcbInfo.ByteSize:=8;
dcbInfo.Parity:=NOPARITY;
dcbInfo.StopBits:=ONESTOPBIT;
// set COMM port state
   errorFlag:=SetCommState(commPortHandle,dcbInfo);
// bail on failure
   if (NOT(errorFlag)) then
begin
CloseHandle(commPortHandle);
Result:=false;
exit;
end;
// show opened port
   ShowMessage('Opened Comm Port: ' + whichPort);
// set flag
   commPortOpen:=true;
// set flag
   Result:=true;
end;
procedure  TCommDemoObject.CloseCommPort;
begin
// close comm port handle
   CloseHandle(commPortHandle);
end;
procedure  TCommDemoObject.TransmitSingleChar(theChar:Char);
label
CHECKSTATUS;
begin
//   // you can use escape function to manually raise/lower DTR
//   EscapeCommFunction(commPortHandle,SETDTR);
//   EscapeCommFunction(commPortHandle,CLRDTR);
//   // you can use escape function to manaully raise/lower RTS
//   EscapeCommFunction(commPortHandle,SETRTS);
//   EscapeCommFunction(commPortHandle,CLRRTS);
   // transmit char
   TransmitCommChar(commPortHandle,theChar);
// wait for char to exit 1 char buffer
CHECKSTATUS:
ClearCommError(commPortHandle,commPortError,@commPortStatus);
if ( fTxim in commPortStatus.Flags) then
begin;
// read status again
       goto CHECKSTATUS;
end;
end;
procedure  TCommDemoObject.TransmitString(theString:String);
var
charBuffer:array[0..100] of Char;
begin
// convert string to chars
   StrPCopy(charBuffer,theString);
// send string to comm port
   WriteFile(commPortHandle,charBuffer,Length(theString),
numberOfCharsActuallyWritten,nil);
end;
function  TCommDemoObject.ReadCommPort:Integer;
begin
// read comm port status
   ClearCommError(commPortHandle,commPortError,@commPortStatus);
// check input buffer count
   if (commPortStatus.cbInQue>0) then
ReadFile(commPortHandle,commPortInputBuffer,
commPortStatus.cbInQue,numberOfCharsActuallyRead,nil);
// set number of chars actually read
   Result:=numberOfCharsActuallyRead;
end;
end.
Syntax Highlighted with https://pascal-id.org/syntax

[red: penulis asli (eksant) spertinya lupa menuliskan kegunaan skrip ini, jadi.. belajar sendiri ya]
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com