接收方应该是BYTE,要不你就转换,(在网上找个SPComm控件)
源码如下!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, ExtCtrls, Buttons, Registry, Printers;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Memo2: TMemo;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
Button1: TButton;
Button2: TButton;
PrintDialog1: TPrintDialog;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure SpeedButton2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..7] of byte;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 7 do
begin
if not form1.Comm1.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2); {发送时字节间的延时}
viewstring:=viewstring+inttohex(sbuf,2)+' ';
end;
viewstring:='发送'+viewstring;
form1.memo1.lines.add(viewstring);
form1.memo1.lines.add('');
if not commflg then messagedlg('发送失败!',mterror,[mbyes],0);
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,BufferLength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf,2)+' ';
viewstring:='接受'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
sbuf[1]:=byte($f5); {帧头}
sbuf[2]:=byte($10); {命令号}
sbuf[3]:=byte($04);
sbuf[4]:=byte($c3);
sbuf[5]:=byte($60);
sbuf[6]:=byte($50);
sbuf[7]:=byte($12);
//sbuf[6]:=byte($45); {帧尾}
senddata;{调用发送函数}
end;
procedure TForm1.Button1Click(Sender: TObject);
var
reg : TRegistry;
ts : TStrings;
i : integer;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('hardware/devicemap/serialcomm',false);
ts := TStringList.Create;
try
reg.GetValueNames(ts); //获得子键下的所有项
for i := 0 to ts.Count -1 do
begin //把每一项下的串值显示在Memo1中
Memo1.Lines.Add(reg.ReadString(ts.Strings));
end;
finally
ts.Free;
end;
finally
reg.CloseKey;
reg.free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
pDevice : pChar;
pDriver : pChar;
pPort : pChar;
hDMode : THandle;
begin
if PrintDialog1.Execute then begin
GetMem(pDevice, cchDeviceName);
GetMem(pDriver, MAX_PATH);
GetMem(pPort, MAX_PATH);
Printer.GetPrinter(pDevice, pDriver, pPort, hDMode);
if lStrLen(pDriver) = 0 then begin
GetProfileString('Devices', pDevice, '', pDriver, MAX_PATH);
pDriver[pos(',', pDriver) - 1] := #0;
end;
if lStrLen(pPort) = 0 then begin
GetProfileString('Devices', pDevice, '', pPort, MAX_PATH);
lStrCpy(pPort, @pPort[lStrLen(pPort)+2]);
end;
Memo1.Lines.Add('Device := ' + StrPas(pDevice));
Memo1.Lines.Add('Driver := ' + StrPas(pDriver));
Memo1.Lines.Add('Port := ' + StrPas(pPort));
FreeMem(pDevice, cchDeviceName);
FreeMem(pDriver, MAX_PATH);
FreeMem(pPort, MAX_PATH);
end;
end;
end.