Delphi DLL 兼容
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在现代机器上没问题,尤其是波特率为 2400 的机器,但现在由您来控制握手线。您必须打开 DTR 和 RTS 信号,以便设备知道您已在线并准备好接收数据。
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.