用MSCOMM控件写成DLL,串口通信,读工业秤上的重量数据,然后客户端能显示该重量数据,首先要对MSCOMM控件进行初始化,初始化成功后响应oncomm事件,都要将其封装在DLL中
请问DLL文件是不是要提供几个函数,比如初始化函数、显示重量函数??DLL文件中能不能响应ONCOMM事件??
请提供些思路,或者大概写些代码
请问DLL文件是不是要提供几个函数,比如初始化函数、显示重量函数??DLL文件中能不能响应ONCOMM事件??
请提供些思路,或者大概写些代码
可以啊, 其實無所謂, 但我以前做過類似的, 還是自己主動去讀 mscomm 看有沒有返回值, 而不是等待事件發生
我需要在计算机上面实时显示重量,所以要等待事件發生
當然, 如果你習慣用事件也可以啊, 簡單的, 直接加個Form, 在上面處理, 就自然有消息循環
请你帮我看看我的思路对不对?
1、建个DLL工程,里面有三个函数:OpenComm(),ShowWeight(),CloseComm()
2、在该工程里添加单元(无窗体),里面有构造函数创建MSCOMM1,然后初始化该对象,OpenComm调用她;响应ONCOMM事件,ShowWeight调用它;析构函数销毁MSCOMM1,CloseComm调用它
uses
SysUtils,
Classes,
Forms,
Dialogs,
MSCommLib_TLB,
uMSComm in 'uMSComm.pas';{$R *.res}function OpenComm():integer;StdCall;
//该函数可能有问题
begin
try
TWeight.Create;
Result := 1;
except
on E:Exception do
ShowMessage('OpenComm:'+#10+ E.Message );
end;
end;function ShowWeight():integer;StdCall;
begin
//该函数有问题
try
TWeight.MSComm1Comm();
Result := 1;
except
on E:Exception do
ShowMessage('ShowWeight:'+#10+ E.Message );
end;
end;function CloseComm():integer;StdCall;
begin
//该函数有问题
try
TWeight.Destory;
Result :=1;
except
on E:Exception do
ShowMessage('CloseComm:'+#10+ E.Message );
end;
end;exports
OpenComm,ShowWeight,CloseComm;
begin
end.
unit uMSComm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StrUtils,MSCommLib_TLB;const
WM_WeightChanged = WM_USER+200;
type
TWeight = class
MSComm1 : TMSComm;
Weight : TWeight;
procedure MSComm1Comm(Sender: TObject);
private
FWeight : integer;
public
constructor Create;
destructor Destory;
end;var iCom :integer;
strRate :string;
strPart :string;
strDbits:string;
strStopB:string;
fIni :TextFile;
str :string[11];
strCom :string ;implementation{ TWeight }constructor TWeight.Create;
//经测试该构造函数里的程序正确,可以不细看
begin
try
Weight := TWeight.Create();
MSComm1 := TMSComm.Create(nil);
MSComm1.OnComm := MSComm1Comm; //从文本文件中读串口号,波特率 ,效验位,数据位,停止位
try
AssignFile(fIni,ExtractFilePath(Application.ExeName) + 'MSComm.ini');
Reset(fIni);
try
while not Eof(fIni) do
begin
Readln(fIni,str,strCom);
if str='PORT =' then
iCom := strtoint(Trim(strCom)); if str='baud rate =' then
strRate:= Trim(strCom); if str='Parity =' then
strPart :=Trim(strCom); if str='DataBits =' then
strDbits:=Trim(strCom); if str='StopBits =' then
strStopB:=Trim(strCom);
end;
finally
CloseFile(fIni);
end;
except
on EInOutError do
ShowMessage('TWeight.Create:' + #10 + '访问初始化文件出错');
end; //初始化MSComm1
try
if MSComm1.PortOpen then
MSComm1.PortOpen := false;
with MSComm1 do
begin
CommPort :=iCom;
Settings :=strRate + strPart + strDbits + strStopB;
InBufferSize :=18;
InputLen :=18;
RThreshold :=10;
SThreshold :=0;
Handshaking := comNone;
PortOpen :=true;
InBufferCount:=0 ;
end;
except
on E:Exception do
ShowMessage('TWeight.Create:' + #10 + E.Message);
end;
except
on E:Exception do
ShowMessage('TWeight.Create:'+#10+ E.Message );
end;
end;destructor TWeight.Destory;
begin
try
FreeAndNil(MSComm1);
except
on E:Exception do
ShowMessage('TWeight.Destory:'+#10+ E.Message);
end;
end;procedure TWeight.MSComm1Comm(Sender: TObject);
//经测试该过程里的程序正确,可以不细看
var
STX :string;
strWei :string;
iWei :integer;
begin
try
if MSComm1.CommEvent = 2 then
begin
sleep(100);
strWei:= MSComm1.Input ;
MSComm1.InBufferCount :=0 ;
STX:=MidStr(strWei,1,1);
if STX= #2 then
begin
iWei:= strtoint(MidStr(strWei,5,6));
MSComm1.PortOpen :=false;
if MSComm1.PortOpen =false then
begin
MSComm1.PortOpen :=true;
end;
end;
end
else
exit;
except
on E:Exception do
ShowMessage('MSComm1Comm:'+#10+ E.Message);
end;
end;end.
在DLL中的函数是不是一定要用STDCALL?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, uMSComm;type
TfrmWei = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure WMWeightChanged(var Msg:TMessage);message WM_WeightChanged;
public
{ Public declarations }
end;var
frmWei: TfrmWei;implementation{$R *.dfm}procedure TfrmWei.FormCreate(Sender: TObject);
var
FHandle:THandle;
begin
Weight := TWeight.Create;
FHandle := frmWei.Handle;
Weight.SetHandle(FHandle);
end;procedure TfrmWei.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Weight.Destory;
end;procedure TfrmWei.WMWeightChanged(var Msg: TMessage);
begin
Edit1.Text := inttostr(Msg.WParam);
end;end.
unit uMSComm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StrUtils,MSCommLib_TLB;const
WM_WeightChanged = WM_USER + 200;
type
TWeight = class
MSComm1 : TMSComm;
procedure MSComm1Comm(Sender: TObject);
private
FWeight :integer;
iCom :integer;
strRate :string;
strPart :string;
strDbits:string;
strStopB:string;
fIni :TextFile;
str :string[11];
strCom :string ;
public
constructor Create;
destructor Destory;
procedure SetHandle(Handle :THandle);
//procedure SetWeight(iWei :integer; FHandle:THandle);
procedure SetWeight(iWei :integer);
end;
var
Weight :TWeight;
FHandle :cardinal;
implementation{ TWeight }constructor TWeight.Create;
begin
try
MSComm1 := TMSComm.Create(nil);
MSComm1.OnComm := MSComm1Comm; //从文本文件中读串口号,波特率 ,效验位,数据位,停止位
AssignFile(fIni,ExtractFilePath(Application.ExeName) + 'Config\MSComm.ini');
Reset(fIni);
try
while not Eof(fIni) do
begin
Readln(fIni,str,strCom);
if str='PORT =' then
iCom := strtoint(Trim(strCom)); if str='baud rate =' then
strRate:= Trim(strCom); if str='Parity =' then
strPart :=Trim(strCom); if str='DataBits =' then
strDbits:=Trim(strCom); if str='StopBits =' then
strStopB:=Trim(strCom);
end;
finally
CloseFile(fIni);
end; //初始化MSComm1
if MSComm1.PortOpen then
MSComm1.PortOpen := false;
with MSComm1 do
begin
CommPort :=iCom;
Settings :=strRate + strPart + strDbits + strStopB;
InBufferSize :=18;
InputLen :=18;
RThreshold :=10; //18??
SThreshold :=0;
Handshaking := comNone;
PortOpen :=true;
//sleep(500);
InBufferCount:=0 ;
end;
except
on EInOutError do
ShowMessage('TWeight.Create:' + #10 + '访问初始化文件出错');
on E:Exception do
ShowMessage('TWeight.Create:'+#10+ E.Message );
end;
end;destructor TWeight.Destory;
begin
try
FreeAndNil(MSComm1);
except
on E:Exception do
ShowMessage('TWeight.Destory:'+#10+ E.Message);
end;
end;procedure TWeight.MSComm1Comm(Sender: TObject);
var
STX :string;
strWei :string;
iWei :integer;
begin
try
if MSComm1.CommEvent = 2 then
begin
sleep(100);
strWei:= MSComm1.Input ;
MSComm1.InBufferCount :=0 ;
STX:=MidStr(strWei,1,1);
if STX= #2 then
begin
iWei:= strtoint(MidStr(strWei,5,6));
//SetWeight(iWei,FHandle);
SetWeight(iWei);
MSComm1.PortOpen :=false;
if MSComm1.PortOpen =false then
begin
//sleep(100);
MSComm1.PortOpen :=true;
end;
end;
end
else
exit;
except
on E:Exception do
ShowMessage('MSComm1Comm:'+#10+ E.Message);
end;
end;procedure TWeight.SetHandle(Handle :THandle);
begin
try
FHandle := Handle;
except
on E:Exception do
ShowMessage('TWeight.SetHandle:' + #10 + E.Message);
end;
end;//procedure TWeight.SetWeight(iWei :integer;FHandle :THandle);
procedure TWeight.SetWeight(iWei :integer);
begin
try
FWeight:= iWei;
PostMessage(FHandle,WM_WeightChanged,FWeight,0);
except
on E:Exception do
ShowMessage('TWeight.SetWeight:' + #10 + E.Message);
end;
end;end.