procedure TForm1.Button2Click(Sender: TObject);
begin
comm1.StopComm ;
end;
function StrToNum(const s: string; base: Integer;
neg: Boolean; max: Integer): Integer;
var
negate, done: Boolean;
i, ind, len, digit, mmb: Integer;
c: Char;
mdb, res: Integer;
begin
res := 0;
i := 1;
digit := 0;
if (base >= MinBase) and (base <= MaxBase) then
begin
mmb := max mod base;
mdb := max div base;
len := Length(s);
negate := False;
while (i <= len) and (s[i] = '') do
Inc(i);
if neg then
begin
case s[i] of
'+': Inc(i);
'-':
begin
Inc(i);
negate := TRUE;
end;
end; (* CASE *)
end; (* IF neg *)
done := len > i;
while (i <= len) and done do
begin
c := Upcase(s[i]);
case c of
'0'..'9': digit := ORD(c) - 48;
'A'..'Z': digit := ORD(c) - 55;
else
done := FALSE
end; (* CASE *)
done := done and (digit < base);
if done then
begin
done := (res < mdb) or ((res = mdb) and (digit <= mmb));
if done then
begin
res := res * base + digit;
Inc(i);
end; (* IF done *)
end; (* IF done *)
end; (* WHILE *)
if negate then
res := -res;
end; (* IF done *)
Result := res;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
//strRecv:array of Byte;
strRecv:string;
i:integer;
Temp:integer;
begin
setlength(strRecv,BufferLength);
copyMemory(pChar(strRecv),Buffer,7);
for i:=0 to High(strRecv) do
Memo1.Lines.Add(inttostr(strRecv[i]));
Memo1.Invalidate ;
end;
procedure TForm1.edtbaudExit(Sender: TObject);
var
i:integer;
begin
try
i:=strtoint(edtbaud.Text);
except
edtbaud.SetFocus;
raise exception.Create('错误');
end;
comm1.BaudRate :=strtoint(edtbaud.Text);
end;
procedure TForm1.cmbByteSizeExit(Sender: TObject);
begin
comm1.ByteSize :=TByteSize(cmbbytesize.ItemIndex);
end;
procedure TForm1.cmbstopsizeExit(Sender: TObject);
begin
comm1.StopBits :=TStopBits(cmbstopsize.ItemIndex);
end;
procedure TForm1.cmbparityChange(Sender: TObject);
begin
comm1.Parity :=TParity(cmbparity.ItemIndex);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
str: Pchar;
Count: integer;
i:array [0..2]of Byte;
begin
str := Pchar(edtSendText.Text);
Count := Length(str);
if Comm1.WriteCommData(@i, Count) then
Memo1.Lines.Add('已发送' + intTostr(Count) + '个字符')
else
raise exception.Create('发送错误');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CommPort : string;
hCommFile : THandle;
Buffer : PCommConfig;
size : DWORD;
begin
CommPort := 'COM1';
{Open the comm port}
hCommFile := CreateFile(PChar(CommPort),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hCommFile=INVALID_HANDLE_VALUE then
begin
ShowMessage('Unable to open '+ CommPort);
exit;
end;
{Allocate a temporary buffer}
GetMem(Buffer, sizeof(TCommConfig));
{Get the size of the CommConfig structure}
{as it may be different than documented}
size := 0;
GetCommConfig(hCommFile, Buffer^, size);
{Free the temporary buffer}
FreeMem(Buffer, sizeof(TCommConfig));
{Allocate the CommConfig structure}
GetMem(Buffer, size);
GetCommConfig(hCommFile, Buffer^, size);
{Change the baud rate}
Buffer^.dcb.BaudRate := 1200;
{Set the comm port to the new configuration}
SetCommConfig(hCommFile, Buffer^, size);
{Free the buffer}
FreeMem(Buffer, size);
{Close the comm port}
CloseHandle(hCommFile);
end;
//////////////////////////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
var
CommPort : string;
hCommFile : THandle;
Buffer : PCommConfig;
size : DWORD;
begin
CommPort := 'COM1';
{Open the comm port}
hCommFile := CreateFile(PChar(CommPort),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hCommFile=INVALID_HANDLE_VALUE then
begin
ShowMessage('Unable to open '+ CommPort);
exit;
end;
{Get a temporary buffer}
GetMem(Buffer, sizeof(TCommConfig));
{Get the size of the CommConfig structure}
{as it may be different than documented}
size := 0;
GetCommConfig(hCommFile, Buffer^, size);
{Free the temporary buffer}
FreeMem(Buffer, sizeof(TCommConfig));
{Get the CommConfig structure}
GetMem(Buffer, size);
GetCommConfig(hCommFile, Buffer^, size);
{Pop up the comm port config dialog}
if CommConfigDialog(PChar(CommPort),
Form1.Handle,
Buffer^) = true then begin
{Set the com port to the values entered}
{in the dialog if the user pressed ok}
SetCommConfig(hCommFile, Buffer^, size);
end;
{Free the buffer}
FreeMem(Buffer, size);
{Close the comm port}
CloseHandle(hCommFile);
end;