这是部分代码,删除掉前面某些行偶尔可以恢复正常,有时候又不能。
unit unitPPI;
interface
uses
Windows, SysUtils, Variants, Classes, Controls, Forms,
StdCtrls, OoMisc, AdPort, MMsystem;
const
StationNo = 2;
Hostnummer = 0 ;
type
TfrmPPI = class(TForm)
PPICom: TApdComPort;
function OpenCOM(const No: Byte; const Settings:String) : Boolean;
function CloseCOM : Boolean;
procedure PPIComTrigger(CP: TObject; Msg, TriggerHandle, Data: Word);
function ReadVB(const Station :byte; const Addr : Word; var value
Word): boolean;
function writeVB(const Station :byte; const Addr : Word; const Value : Dword): Boolean;
private
{ Private declarations }
portOpened:boolean;
expectCharNo,realCharNo:word;
waitData:boolean;
timerHandle:word;
retStr:string;
Bytebuffer : Array[0..512] of Byte;
protected
{ Protected declarations }
public
{ Public declarations }
end;
TbyteArray =Array[0..222] of byte;
var
frmPPI: TfrmPPI;
S7Quit : Array[0..5] of Byte = ($10, StationNo, $00, $5C, $5E, $16);
S7GetVW : Array[0..32] of Byte =($68, $1B, $1B, $68, StationNo, $00,
$7C, $32, $01, $00, $00, $00, $00, $00, $0E, $00, $00, $04, $01, $12, $0A, $10,
$02, $00, $02, $00, $01, $84, $00, $03, $20, $9C, $16);
resetByte: Array [0..26] of Byte =($68,$15,$15,$68,StationNo,$00,$6c,$32,$01,$00,
$00,$cc,$c1,$00,$08,$00,$00,$f0,$00,$00,$01,$00,$01,$03,$c0,$eb,$16);
implementation
{$R *.dfm}
procedure Delay_MS(const mst : Cardinal);
var
CDelay: Cardinal;
CTime: LongInt;
LTime: LongInt;
begin
LTime := timeGetTime;
CDelay := 0;
while CDelay < mst do begin
Application.processMessages;
CTime := timeGetTime;
Inc(CDelay, CTime - LTime);
LTime := CTime;
end;
end;
function TfrmPPI.OpenCOM (const No: Byte; const Settings:String) : Boolean;
begin
try
PPICom.ComNumber := No;
PPICom.Baud :=strtoint(settings);
ppiCom.Parity :=pEven;
if not PPICom.Open then PPICom.Open :=true;
result:=true;
portOpened:=true;
//s7GetVL[4]:=Station;
//s7SetVW[4]:=Station;
//s7SetVD[4]:=Station;
except
portOpened:=false;
result:=false;
end;
end;
function TfrmPPI.CloseCOM ():boolean;
begin
if PPICom.Open then PPICom.Open :=false;
portOpened:=false;
result:=true;
end;
procedure TfrmPPI.PPIComTrigger(CP: TObject; Msg, TriggerHandle, Data: Word);
var
I : Word;
c :char;
begin
case Msg of
APW_TRIGGERDATA :
begin
//overTimeFlag:=false;
end;
APW_TRIGGERAVAIL :
begin
for I := 1 to Data do begin
c:= PPICom.GetChar;
retStr := retStr+c;
byteBuffer[realCharNo]:=ord(c);
realCharNo:=realCharNo+1;
end;
if expectCharNo=realCharNo then begin
ppiCom.RemoveTrigger(timerHandle);
waitData:=false;
end;
end;
APW_TRIGGERTIMER :
begin
waitData:=false;
PPICom.RemoveTrigger(timerHandle);
end;
end;
end;
function TfrmPPI.ReadVB(const Station :byte; const Addr : Word; var value
Word): boolean;
var
I,K : Byte;
sStr,qStr: string;
begin
Result := false;
if (not portOpened) or waitData then exit;
K:=0;
s7GetVW[4]:=station;
S7GetVW[22]:=2;
S7GetVW[24]:=1; //读取字节数
S7GetVW[26]:=1;
S7GetVW[27]:=$84;
S7GetVW[29]:=Hi(Addr*8);
S7GetVW[30]:=Lo(Addr*8);
for I:=4 to 30 do
K:=K + S7GetVW[I];
S7GetVW[31]:=K;
sStr:='';
for i:=0 to sizeof(s7GetVW)-1 do sStr:=sStr+char(S7GetVW[I]);
PPICom.PutString(sStr);
expectCharNo:=1;
realCharNo:=0;
retStr:='';
timerHandle := PPICom.AddTimerTrigger;
PPICom.SetTimerTrigger(timerHandle,182,true);
end;
function TfrmPPI.writeVB (const Station :byte; const Addr : Word; const Value : Dword): Boolean;
var
I,K : Byte;
sStr,qStr: string;
begin
Result := false;
if (not portOpened) or waitData then exit;
end;
end.