蓝
蓝叶菱
Unregistered / Unconfirmed
GUEST, unregistred user!
不用那么麻烦,你要设定端口程序,必须先关闭,再设定参数,最后开启,否则够戗。
另外写字符的时候,不需要使用循环语句,多余。
我给你个精简的例子。
unit uDMCOMM;
interface
uses
SysUtils, Classes, SPComm;
type
TDMCOMM = class(TDataModule)
Comm1: TComm;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function COMInit(COM:Integer):Boolean;
function COMSendRead:Boolean;
function COMSendWrite(WStr:String):Boolean;
function COMStop:Boolean;
function COMReset:Boolean;
end;
var
DMCOMM: TDMCOMM;
implementation
uses CGlobal;
{$R *.dfm}
{ TDMCOMM }
function TDMCOMM.COMInit(COM: Integer): Boolean;
begin
if COM=1 then
Comm1.CommName:='COM1' else Comm1.CommName:='COM2';
try
Comm1.StartComm;
Result:=True;
except
Comm1.StopComm;
Result:=False;
end;
end;
function TDMCOMM.COMReset: Boolean;
var
FStr:String;
begin
FStr:=#$1b#97;//97
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMSendRead: Boolean;
var
FStr:String;
begin
FStr:=#$1B#$5D;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMSendWrite(WStr: String): Boolean;
var
FStr:String;
begin
FStr:=#$1b#$74+WStr+#$1d#$1b#$5c;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMStop: Boolean;
var
FStr:String;
begin
FStr:=#$1b#$30;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
procedure TDMCOMM.DataModuleCreate(Sender: TObject);
begin
if Logon.COM=1 then
Comm1.CommName:='COM1' else Comm1.CommName:='COM2';
end;
procedure TDMCOMM.DataModuleDestroy(Sender: TObject);
begin
Comm1.StopComm;
end;
end.
另外写字符的时候,不需要使用循环语句,多余。
我给你个精简的例子。
unit uDMCOMM;
interface
uses
SysUtils, Classes, SPComm;
type
TDMCOMM = class(TDataModule)
Comm1: TComm;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function COMInit(COM:Integer):Boolean;
function COMSendRead:Boolean;
function COMSendWrite(WStr:String):Boolean;
function COMStop:Boolean;
function COMReset:Boolean;
end;
var
DMCOMM: TDMCOMM;
implementation
uses CGlobal;
{$R *.dfm}
{ TDMCOMM }
function TDMCOMM.COMInit(COM: Integer): Boolean;
begin
if COM=1 then
Comm1.CommName:='COM1' else Comm1.CommName:='COM2';
try
Comm1.StartComm;
Result:=True;
except
Comm1.StopComm;
Result:=False;
end;
end;
function TDMCOMM.COMReset: Boolean;
var
FStr:String;
begin
FStr:=#$1b#97;//97
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMSendRead: Boolean;
var
FStr:String;
begin
FStr:=#$1B#$5D;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMSendWrite(WStr: String): Boolean;
var
FStr:String;
begin
FStr:=#$1b#$74+WStr+#$1d#$1b#$5c;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
function TDMCOMM.COMStop: Boolean;
var
FStr:String;
begin
FStr:=#$1b#$30;
Sleep(5);
Result:=Comm1.WriteCommData(pchar(FStr),Length(Fstr));
end;
procedure TDMCOMM.DataModuleCreate(Sender: TObject);
begin
if Logon.COM=1 then
Comm1.CommName:='COM1' else Comm1.CommName:='COM2';
end;
procedure TDMCOMM.DataModuleDestroy(Sender: TObject);
begin
Comm1.StopComm;
end;
end.