Delphi DLL comport

我正在开发Delphi 2010中与Comport通信的DLL。 但是有一个问题是我没有从端口获得任何价值。 我正在使用RxChar,但我认为com objet没有触发RxChar命令。 我怎么能触发RxChar所以它会工作?
unit unitfxvogir;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, AxCtrls, Classes,
  ridFXVogir_TLB, StdVcl, CPort, CPortCtl, ExtCtrls;

type
  Tfxvogir = class(TAutoObject, IConnectionPointContainer, Ifxvogir)
  private
    { Private declarations }
    FConnectionPoints: TConnectionPoints;
    FConnectionPoint: TConnectionPoint;
    FEvents: IfxvogirEvents;
    { note: FEvents maintains a *single* event sink. For access to more
      than one event sink, use FConnectionPoint.SinkList, and iterate
      through the list of sinks. }
    ComPort1: TComPort;
  public
    procedure Initialize; override;
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    procedure ComPort1RxChar(Sender: TObject; Count: Integer); safecall;
  protected
    function OpnaVog(const ComPort: WideString): WordBool; safecall;
    function LokaVog: WordBool; safecall;
    function Vigt: WideString; safecall;
    { Protected declarations }
    function SendaSkipun(const Inntak: WideString): WordBool; safecall;
    property ConnectionPoints: TConnectionPoints read FConnectionPoints
      implements IConnectionPointContainer;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
  end;

implementation

uses ComServ, unitAdgerdir;

procedure Tfxvogir.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IfxvogirEvents;
end;

procedure Tfxvogir.Initialize;
begin
  inherited Initialize;
  FConnectionPoints := TConnectionPoints.Create(Self);
  if AutoFactory.EventTypeInfo <> nil then
    FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
      AutoFactory.EventIID, ckSingle, EventConnect)
  else FConnectionPoint := nil;
end;

procedure Tfxvogir.AfterConstruction;
begin
  inherited;
  ComPort1 := TComPort.Create(ComPort1);
  //tmrtimer := TTimer.Create(ComPort1);
end;

procedure Tfxvogir.BeforeDestruction;
begin
  inherited;
  ComPort1.Free;
  //tmrtimer.Free;
end;

function Tfxvogir.OpnaVog(const ComPort: WideString): WordBool;
begin
 try
  //tmrtimer.Enabled := false;
  //tmrtimer.Interval := 100;

  ComPort1.Port                     := ComPort;
  ComPort1.BaudRate                 := br2400;
  ComPort1.DataBits                 := dbSeven;
  ComPort1.StopBits                 := sbOneStopBit;
  ComPort1.Parity.Bits              := prEven;
  ComPort1.FlowControl.FlowControl  := fcNone;

  if not ComPort1.Connected then
    ComPort1.Open;

  if ComPort1.Connected then
    Result := True;

  except
    Result := False;
  end;
end;

function Tfxvogir.LokaVog: WordBool;
begin
  try
    if ComPort1.Connected then
      ComPort1.Close;

    Result := True;
  except
    Result := False;
  end;
end;

function Tfxvogir.Vigt: WideString;
begin
  Result := g_rVigtun.VigtunGr;
end;

procedure Tfxvogir.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  Str: string;
  str1 : ansichar;
    i : Integer;
  cStafur : AnsiChar;
begin
  ComPort1.Readstr(Str, Count);
  try
    for i := 1 to count do begin
      cStafur := AnsiChar(str[i]);
      LesaSvar(cStafur);
    end;

  except
    g_rVigtun.SvarTexti := 'Villa í tengingu';
  end;
end;

function Tfxvogir.SendaSkipun(const Inntak: WideString): WordBool;
var
  //I : Integer;
  BCC : Integer;
  sCommand : AnsiString;
begin
  //ATH höndla ef slökkt er á vog = ekkert svar berst
  Result := False;
  g_rVigtun := FrumstillaVigtun;

  if Length(Inntak) < 1 then begin
    g_rVigtun.SvarTexti := 'VILLA: Engin skipun til að senda.';
    exit(false);
  end;

  //Er þetta lögleg skipun    if (rSending.Kaupsamn <> '') and (rSending.Kaupsamn[1] in ['K', 'V']) then begin
  if not (Inntak[1] in ['C','G','M','O','R','T','W','Z']) then begin
    g_rVigtun.SvarTexti := 'VILLA: ['+Inntak+'] er óþekkt skipun.';
    exit(false);
  end;

  g_rVigtun.Skipun := ansichar(Inntak[1]);
  SamskiptiByrja;
  g_StoduVel := svNyttSvar;

  BCC := ReiknaBCC(Inntak);
  //Skipun er alltaf á forminu
  //<STX><[SKIPUN][aukatexti]><ETX><BCC>
  sCommand := STX + Inntak + ETX + Chr(BCC);

  //ATH ætti að hreinsa inntaks buffer hér ?
  try
    ComPort1.WriteStr(sCommand);
  except

  end;
  Result := True;
end;

initialization
  TAutoObjectFactory.Create(ComServer, Tfxvogir, Class_fxvogir,
    ciMultiInstance, tmApartment);
end.
    
已邀请:
 ComPort1.FlowControl.FlowControl  := fcNone;
这在现代机器上是可以的,特别是波特率为2400,但现在由你来控制握手线。您必须打开DTR和RTS信号,以便设备知道您已在线并准备好接收数据。     

要回复问题请先登录注册