Delphi DLL Comport - PullRequest
       21

Delphi DLL Comport

0 голосов
/ 29 января 2011

Я разрабатываю DLL в Delphi 2010, которая взаимодействует с Comport.

Но есть проблема в том, что я не получаю никакого значения от порта. Я использую 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.

1 Ответ

2 голосов
/ 29 января 2011
 ComPort1.FlowControl.FlowControl  := fcNone;

Это нормально на современных машинах, особенно с скоростью передачи 2400, но теперь вы можете контролировать линии рукопожатия. Вы должны включить сигналы DTR и RTS, чтобы устройство знало, что вы подключены к сети и готовы получать данные.

...