Delphi DLL comport
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.
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.
精彩评论