我要新建一个TWeight类,用MSComm控件通过串口从工业秤中读重量数据,用测试程序我已经把重量正确读出来了,现在要求我把它做成一个TWeight类,请高手帮我看看这样写程序对吗?我很急,请高手耐心看看,谢先!!    该单元有窗体,在该窗体上可以设置5个MSComm控件参数(用TComboBox),edtWeight控件用来显示重量数据
unit fWeight;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,uWeight, OleCtrls, MSCommLib_TLB;type
  TfrmWeight = class(TForm)
    edtWeight: TEdit;
    coboxPort: TComboBox;
    coboxBaudRate: TComboBox;
    coboxParity: TComboBox;
    coboxDataBits: TComboBox;
    coboxStopBits: TComboBox;
    btnSetPara: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnSetParaClick(Sender: TObject);
  private
    procedure WMWeightChanged(var Msg:TMessage);message WM_WeightChanged;
  public
    { Public declarations }
  end;var
  frmWeight: TfrmWeight;implementation
{$R *.dfm}procedure TfrmWeight.FormCreate(Sender: TObject);
var
  strCom  :string;
begin
  try
    Weight:=TWeight.Create;                      //实例化MSComm控件,转到uWeight单元的TWeight.Create构造函数    Weight.InitReadComm;                         //初始化通信参数,转到uWeight单元的TWeight.InitReadComm过程    if not Weight.InitComm then                  //初始化MSComm1成功,转到uWeight单元的TWeight.InitComm函数      ShowMessage('串口初始化错误');    case giCom of
      1: strCom:='COM1';
      2: strCom:='COM2';
      3: strCom:='COM3';
      4: strCom:='COM4';
    end;
    coboxPort.Text:=strCom;
    coboxBaudRate.Text:=inttostr(giRate);
    coboxParity.Text:=gstrPart;
    coboxDataBits.Text:=inttostr(giDbits);
    coboxStopBits.Text:=inttostr(giStopB);
  except
    on E:Exception do
      ShowMessage('TfrmWeight.FormCreate:'+#10+ E.Message);
  end;
end;procedure TfrmWeight.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  try
    Weight.Destory;
  except
    on E:Exception do
      ShowMessage('TfrmWeight.FormClose:'+#10+ E.Message);
  end;
end;//响应uWeight单元的消息
procedure TfrmWeight.WMWeightChanged(var Msg: TMessage);
begin
  try
    edtWeight.Text := inttostr(Msg.WParam);
  except
    on E:Exception do
      ShowMessage('TfrmWeight.WMWeight:'+#10+ E.Message );
  end;
end;
//设置参数
procedure TfrmWeight.btnSetParaClick(Sender: TObject);
var
  fWrite :TextFile;
begin
  AssignFile(fWrite,'d:\MSComm.ini');
  try
    Rewrite(fWrite);
    try
    begin
      Writeln(fWrite,'PORT      =',trim(coboxPort.Text));
      Writeln(fWrite,'baud rate =',trim(coboxBaudRate.Text));
      Writeln(fWrite,'Parity    =',trim(coboxParity.Text));
      Writeln(fWrite,'DataBits  =',trim(coboxDataBits.Text));
      Writeln(fWrite,'StopBits  =',trim(coboxStopBits.Text));
    end;
    finally
      CloseFile(fWrite);
    end;    if 'COM1'=trim(coboxPort.Text) then
      giCom:=1
    else if 'COM2'=trim(coboxPort.Text) then
      giCom:=2
    else if 'COM3'=trim(coboxPort.Text) then
      giCom:=3
    else if 'COM4'=trim(coboxPort.Text) then
      giCom:=4;
    giRate  :=strtoint(trim(coboxBaudRate.Text));
    gstrPart:=trim(coboxParity.Text);
    giDbits :=strtoint(trim(coboxDataBits.Text));
    giStopB :=strtoint(trim(coboxStopBits.Text));    if not Weight.InitComm then
      ShowMessage('设置串口参数错误');
  except
    on E:Exception do
      ShowMessage('btnSetParaClick:'+#10+ E.Message);
  end;
end;end.该单元无窗体,在该单元动态创建一个TWeight类
unit uWeight;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,StrUtils,MSCommLib_TLB;const
  WM_WeightChanged = WM_USER+100;
type
  TWeight = class
    MSComm1 : TMSComm;    procedure MSComm1Comm(Sender: TObject);   //这为MSComm控件的onComm事件,在没有窗体的单元不知道这样写对吗???能不能响应onComm事件???????  private
    FWeight : integer;
  public
    constructor Create;      //mscomm1:TMSComm
    destructor Destory;
    procedure SetWeight(iWei:integer);
    procedure InitReadComm;
    function InitComm:boolean;
  end;var
  Weight  :TWeight;
  giCom   :integer;
  giRate  :integer;
  gstrPart:string;
  giDbits :integer;
  giStopB :integer;implementationuses fWeight;{ TWeight }constructor TWeight.Create;
begin
  try
    MSComm1 := TMSComm.Create(nil);            //动态创建MSComm1控件
  except
    on E:Exception do
      ShowMessage('TWeight.Create:'+#10+ E.Message );
  end;
end;destructor TWeight.Destory;                  //释放MSComm1控件
begin
  try
    FreeAndNil(MSComm1);
  except
    on E:Exception do
      ShowMessage('TWeight.Destory:'+#10+ E.Message );
  end;
end;
//初始化MSComm1
function TWeight.InitComm: boolean;;
begin
  try
    if MSComm1.PortOpen then
      MSComm1.PortOpen := false;
    with MSComm1 do
    begin
      CommPort :=giCom;
      Settings :=inttostr(giRate)+gstrPart+inttostr(giDbits)+inttostr(giStopB);
      InBufferSize :=18;
      InputLen :=18;
      RThreshold :=10;                //18??
      SThreshold :=0;
      Handshaking := comNone;
      PortOpen :=true;
      InBufferCount:=0 ;
    end;
    except
    on E:Exception do
      ShowMessage('InitComm:'+#10+E.Message);
  end;
  InitComm:=true;
end;

解决方案 »

  1.   

    //从文本文件中读串口号,波特率 ,效验位,数据位,停止位
    procedure TWeight.InitReadComm;
    var
      fIni   :TextFile;
      str    :string[11];
      strCom :string ;
    begin
      giCom   :=0;
      giRate  :=0;
      gstrPart:='';
      giDbits :=0;
      giStopB :=0;
      
      AssignFile(fIni,'d:\MSComm.ini');
      try
      Reset(fIni);
      try
        while not Eof(fIni) do
        begin
          Readln(fIni,str,strCom);
          if str='PORT      =' then
          begin
            //strPort:=leftstr(strPort,4);
            if strCom='COM1' then
              giCom:=1
            else if strCom='COM2' then
              giCom:=2
            else if strCom='COM3' then
              giCom:=3
            else if strCom='COM4' then
              giCom:=4;
          end;      if str='baud rate =' then
            giRate:=strtoint(Trim(strCom));      if str='Parity    =' then
          begin
            gstrPart:=Trim(strCom);
            if gstrPart='EVEN' then
              gstrPart:='e'
            else if gstrPart='ODD' then
              gstrPart:='o'
            else if gstrPart='NONE' then
              gstrPart:='n';
          end;      if str='DataBits  =' then
            giDbits:=strtoint(Trim(strCom));      if str='StopBits  =' then
            giStopB:=strtoint(Trim(strCom));
        end;
      finally
        CloseFile(fIni);
      end;
      except
        on EInOutError do
          ShowMessage('访问初始化文件出错');
      end;
    end;//现在没有响应onComm事件,不知道什么原因????(以前测试应该能响应,我不知道怎么回事??)
    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);       
          end;
        end
        else
          exit;
      except
        on E:Exception do
          ShowMessage('MSComm1Comm:'+#10+ E.Message);
      end;
    end;//采用消息机制,将重量数据传给fWeight单元的procedure TfrmWeight.WMWeightChanged(var Msg: TMessage);
    procedure TWeight.SetWeight(iWei:integer);
    begin
      try
        FWeight:= iWei;
        PostMessage(frmWeight.Handle,WM_WeightChanged,FWeight,0);
      except
        on E:Exception do
          ShowMessage('SetWeight:'+#10+E.Message);
      end;
    end;end.    这个程序我大约4个月前做的,当时测试了几百遍OK,但现在不知道怎么了,就是不能响应MSComm控件的onComm事件,我把MSComm控件的RThreshold参数设置为1也不行,
        请问在没有窗体的单元中能不能响应MSComm控件的onComm事件??
        我很急,请告诉指定,或者直接改下程序,谢谢!!
      

  2.   

    其实只有2个单元:
        单元fWeight有窗体,在该窗体上可以设置5个MSComm控件参数(用TComboBox),edtWeight控件用来显示重量数据
        单元uWeight无窗体,在该单元动态创建一个TWeight类,(不知道在没有窗体的单元中能不能响应MSComm控件的onComm事件??))
      

  3.   

    constructor TWeight.Create;
    begin
      try
        MSComm1 := TMSComm.Create(nil);            //动态创建MSComm1控件
        MSComm1.onComm:= MSComm1Comm;              //加上该句,这样你的MSComm1Comm才能与MSComm1的属性事件发生联系
      except
        on E:Exception do
          ShowMessage('TWeight.Create:'+#10+ E.Message );
      end;
    end;