I følgende kode
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ComFile: Thandle;
BytesRead: Integer;
implementation
{$R *.dfm}
function OpenCOMPort: Boolean;
var
DeviceName: array[0..80] of Char;
begin
StrPCopy(DeviceName, 'COM2:');
ComFile := CreateFile(DeviceName, GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
if ComFile = INVALID_HANDLE_VALUE then
Result := False
else
Result := True;
end;
function SetupCOMPort: Boolean;
const
RxBufferSize = 256;
TxBufferSize = 256;
var
DCB: TDCB;
Config: String;
CommTimeouts: TCommTimeouts;
begin
Result := True;
if not SetupComm(ComFile, RxBufferSize, TxBufferSize) then
Result := False;
if not GetCommState(ComFile, DCB) then
Result := False;
Config := 'baud=9600 parity=n data=8 stop=1';
if not BuildCommDCB(@Config[1], DCB) then
Result := False;
if not SetCommState(ComFile, DCB) then
Result := False;
with CommTimeouts do
begin
ReadIntervalTimeout := 0;
ReadTotalTimeoutMultiplier := 0;
ReadTotalTimeoutConstant := 1000;
WriteTotalTimeoutMultiplier := 0;
WriteTotalTimeoutConstant := 1000;
end;
if not SetCommTimeouts(ComFile, CommTimeouts) then
Result := False;
end;
Procedure SendText(s: String);
var
BytesWritten: DWORD;
begin
s := s+#13+#10;
WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);
end;
Function ReadText: String;
var
d: array[1..80] of Char;
s: string;
i: Integer;
begin
Result := '';
if not ReadFile(ComFile, d, SizeOf(d), BytesRead, nil) then
begin
ShowMessage('Error!');
end;
s := '';
for i := 1 to BytesRead do s := s+d[i];
Result := s;
end;
Procedure CloseCOMPort;
begin
CloseHandle(ComFile);
end;
end.
Der siger den
"[Error] Unit1.pas(90): Types of actual and formal var parameters must be identical"
til BytesRead i linjen
if not ReadFile(ComFile, d, SizeOf(d), BytesRead, nil) then
hvorfor gøre den det?
SkypeR That\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'s ME
----------------------------------------------
%49%66%20%79%6F%75%27%76%65%20%74%72%61%
6E%73%6C%61%74%65%64%20%
74%68%69%73%2C%20%79%6F%75%20%6D%75%73%
74%20%62%65%20%62% 6F%72%65%64%2E%2E%2E
[Redigeret d. 04/08-05 17:29:41 af SkypeR]