D
doby_li
Unregistered / Unconfirmed
GUEST, unregistred user!
请问我在dll中使用了spcomm控件,现在打开串口后关闭串口都正常,但再次打开串口时就打不开了,请问是为什么?
dll的源文件为:
library Project1;
uses
SysUtils,
Classes,
spcomm;
{$R *.res}
function opencomm(scommchar):LongBool;stdcall;
var
comm1:TComm;
begin
comm1:=TComm.Create(nil);
try
try
comm1.CommName:=scomm;
Comm1.StartComm;
Result:=true;
except
Result:=false;
end;
finally
comm1.Free;
end;
end;
function closecomm(scommchar):LongBool;stdcall;
var
comm1:TComm;
begin
comm1:=TComm.Create(nil);
try
try
comm1.CommName:=scomm;
Comm1.stopComm;
Result:=true;
except
Result:=false;
end;
finally
comm1.Free;
end;
end;
exports
opencomm,closecomm;
begin
end.
调用程序的相关代码为:
function opencomm(scomm:string):longbool;stdcall;external 'project1.dll';
function closecomm(scomm:string):longbool;stdcall;external 'project1.dll';
打开串口的按纽:
procedure TFCOMM.btnopenClick(Sender: TObject);
begin
if opencomm(slt_com.Text) then
begin
label5.Caption:=slt_com.Text+'口已经打开!';
btnopen.Enabled:=false;
btnclose.Enabled:=true;
tagcomm:='open';
messagedlg('打开'+slt_com.Text+'串口成功!',mtinformation,[mbyes],0);
end;
else
messagedlg('打开'+slt_com.Text+'串口时失败,可能该串口已被占用或不存在,请换另一个串口!',mterror,[mbyes],0);
end;
关闭串口的按纽:
procedure TFCOMM.btncloseClick(Sender: TObject);
begin
if closecomm(slt_com.Text) then
begin
label5.Caption:=slt_com.Text+'口已经关闭!';
btnclose.Enabled:=false;
btnopen.Enabled:=true;
tagcomm:='close';
messagedlg('关闭'+slt_com.Text+'串口成功!',mtinformation,[mbyes],0);
end;
else
messagedlg('关闭'+slt_com.Text+'串口时失败,可能该串口不存在或尚未打开!',mterror,[mbyes],0);
end;
请高手指点,谢谢。
dll的源文件为:
library Project1;
uses
SysUtils,
Classes,
spcomm;
{$R *.res}
function opencomm(scommchar):LongBool;stdcall;
var
comm1:TComm;
begin
comm1:=TComm.Create(nil);
try
try
comm1.CommName:=scomm;
Comm1.StartComm;
Result:=true;
except
Result:=false;
end;
finally
comm1.Free;
end;
end;
function closecomm(scommchar):LongBool;stdcall;
var
comm1:TComm;
begin
comm1:=TComm.Create(nil);
try
try
comm1.CommName:=scomm;
Comm1.stopComm;
Result:=true;
except
Result:=false;
end;
finally
comm1.Free;
end;
end;
exports
opencomm,closecomm;
begin
end.
调用程序的相关代码为:
function opencomm(scomm:string):longbool;stdcall;external 'project1.dll';
function closecomm(scomm:string):longbool;stdcall;external 'project1.dll';
打开串口的按纽:
procedure TFCOMM.btnopenClick(Sender: TObject);
begin
if opencomm(slt_com.Text) then
begin
label5.Caption:=slt_com.Text+'口已经打开!';
btnopen.Enabled:=false;
btnclose.Enabled:=true;
tagcomm:='open';
messagedlg('打开'+slt_com.Text+'串口成功!',mtinformation,[mbyes],0);
end;
else
messagedlg('打开'+slt_com.Text+'串口时失败,可能该串口已被占用或不存在,请换另一个串口!',mterror,[mbyes],0);
end;
关闭串口的按纽:
procedure TFCOMM.btncloseClick(Sender: TObject);
begin
if closecomm(slt_com.Text) then
begin
label5.Caption:=slt_com.Text+'口已经关闭!';
btnclose.Enabled:=false;
btnopen.Enabled:=true;
tagcomm:='close';
messagedlg('关闭'+slt_com.Text+'串口成功!',mtinformation,[mbyes],0);
end;
else
messagedlg('关闭'+slt_com.Text+'串口时失败,可能该串口不存在或尚未打开!',mterror,[mbyes],0);
end;
请高手指点,谢谢。