Delphi DLL 兼容

发布于 2024-10-14 19:09:58 字数 4474 浏览 5 评论 0原文

我正在 Delphi 2010 中开发一个与 Comport 通信的 DLL。

但存在一个问题,即我无法从端口获取任何值。 我正在使用 RxChar,但我认为 com 对象没有触发 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.

i am developing a DLL in Delphi 2010 that is communicating with Comport.

But there is a problem that ia dont get any value from the port.
i am using the RxChar but i think the com objet is not triggering the RxChar command.

how can i trigger the RxChar so it would work??

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-10-21 19:09:58
 ComPort1.FlowControl.FlowControl  := fcNone;

这在现代机器上没问题,尤其是波特率为 2400 的机器,但现在由您来控制握手线。您必须打开 DTR 和 RTS 信号,以便设备知道您已在线并准备好接收数据。

 ComPort1.FlowControl.FlowControl  := fcNone;

That's okay on modern machines, especially with a baudrate of 2400, but now it is up to you to control the handshake lines. You have to turn the DTR and RTS signals on so the device knows that you're online and ready to receive data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文