Wywołanie properity z klasy

0

Panowie i Panie, poproszę o pomoc, bo moja wiedza na temat Pascala jest niezwykle uboga.

Posiadam klasę o takiej definicji

Pytanie jak mam się do niej odwołać, aby np tablicę [$01 $02] ( zapis w sterowniku zmiennej typu int) móc odczytać jako typu samllint jakiejś zmiennej
lub w drugą stronę jak przekonwertować 23765 na wartości w hexie i dostać odpowiednie [$XX $XX]
???

  TS7Helper = class
  private
    function GetInt(pval: pointer): smallint;
    procedure SetInt(pval: pointer; const Value: smallint);
    function GetWord(pval: pointer): word;
    procedure SetWord(pval: pointer; const Value: word);
    function GetDInt(pval: pointer): longint;
    procedure SetDInt(pval: pointer; const Value: longint);
    function GetDWord(pval: pointer): longword;
    procedure SetDWord(pval: pointer; const Value: longword);
    function GetDateTime(pval: pointer): TDateTime;
    procedure SetDateTime(pval: pointer; const Value: TDateTime);
    function GetReal(pval: pointer): single;
    procedure SetReal(pval: pointer; const Value: single);
    function GetBit(pval: pointer; BitIndex: integer): boolean;
    procedure SetBit(pval: pointer; BitIndex: integer; const Value: boolean);
  public
    procedure Reverse(pval : pointer; const S7Type : TS7Type);
    property ValBit[pval : pointer; BitIndex : integer] : boolean read GetBit write SetBit;
    property ValInt[pval : pointer] : smallint read GetInt write SetInt;
    property ValDInt[pval : pointer] : longint read GetDInt write SetDInt;
    property ValWord[pval : pointer] : word read GetWord write SetWord;
    property ValDWord[pval : pointer] : longword read GetDWord write SetDWord;
    property ValReal[pval : pointer] : single read GetReal write SetReal;
    property ValDateTime[pval : pointer] : TDateTime read GetDateTime write SetDateTime;
  end;        

funkcje i procedury wykonawcze

//******************************************************************************
//  S7 CLASS IMPLEMENTATION
//******************************************************************************
function TS7Helper.GetBit(pval: pointer; BitIndex: integer): boolean;
Const
  Mask : array[0..7] of byte = ($01,$02,$04,$08,$10,$20,$40,$80);
begin
  if BitIndex<0 then BitIndex:=0;
  if BitIndex>7 then BitIndex:=7;
  Result:=pbyte(pval)^ and Mask[BitIndex] <> 0;
end;
//------------------------------------------------------------------------------
function TS7Helper.GetDateTime(pval: pointer): TDateTime;
Type
  S7DT   = packed array[0..7] of byte;
Var
  Buffer : ^S7DT;
  YY,MM,DD,HH,NN,SS,MS : word;
  C,D,U : integer;

  function BCD(const B: byte): word;
  begin
    Result:=(B and $0F) + ((B shr 4) * 10);
  end;

begin
  Buffer:=pval;

  YY:= Buffer^[0];
  if YY>137 then // 137 dec = 89 BCD
    YY:=1900+BCD(YY)
  else
    YY:=2000+BCD(YY);

  MM:=BCD(Buffer^[1]);
  DD:=BCD(Buffer^[2]);
  HH:=BCD(Buffer^[3]);
  NN:=BCD(Buffer^[4]);
  SS:=BCD(Buffer^[5]);

  // Millisec
  MS:=Buffer^[6];
  MS:=(MS shl 8)+Buffer^[7];
  // Last 4 bit are Day of Week
  MS:=MS shr 4;

  // Hex to Int
  C:=((MS and $0F00) shr 8) * 100;
  D:=((MS and $00F0) shr 4) * 10;
  U:= (MS and $000F);

  MS:=C+D+U;
 // TryEncodeDateTime(YY,MM,DD,HH,NN,SS,MS,Result);
end;
//------------------------------------------------------------------------------
function TS7Helper.GetDInt(pval: pointer): longint;
Var
  DW : packed array[0..3] of byte absolute Result;
begin
  DW[0]:=pbyte(NativeInt(pval)+3)^;
  DW[1]:=pbyte(NativeInt(pval)+2)^;
  DW[2]:=pbyte(NativeInt(pval)+1)^;
  DW[3]:=pbyte(pval)^;
end;
//------------------------------------------------------------------------------
function TS7Helper.GetDWord(pval: pointer): longword;
Var
  DW : packed array[0..3] of byte absolute Result;
begin
  DW[0]:=pbyte(NativeInt(pval)+3)^;
  DW[1]:=pbyte(NativeInt(pval)+2)^;
  DW[2]:=pbyte(NativeInt(pval)+1)^;
  DW[3]:=pbyte(pval)^;
end;
//------------------------------------------------------------------------------
function TS7Helper.GetInt(pval: pointer): smallint;
Var
  BW : packed array[0..1] of byte absolute Result;
begin
  BW[0]:=pbyte(NativeInt(pval)+1)^;
  BW[1]:=pbyte(pval)^;
end;
//------------------------------------------------------------------------------
function TS7Helper.GetReal(pval: pointer): single;
Var
  DW : packed array[0..3] of byte absolute Result;
begin
  DW[0]:=pbyte(NativeInt(pval)+3)^;
  DW[1]:=pbyte(NativeInt(pval)+2)^;
  DW[2]:=pbyte(NativeInt(pval)+1)^;
  DW[3]:=pbyte(pval)^;
end;
//------------------------------------------------------------------------------
function TS7Helper.GetWord(pval: pointer): word;
Var
  BW : packed array[0..1] of byte absolute Result;
begin
  BW[0]:=pbyte(NativeInt(pval)+1)^;
  BW[1]:=pbyte(pval)^;
end;
//------------------------------------------------------------------------------
procedure TS7Helper.Reverse(pval: pointer; const S7Type: TS7Type);
Var
  DT : TDateTime;
begin
  case S7Type of
    S7Int,
    S7Word: SetWord(pval, pword(pval)^);
    S7DInt,
    S7DWord,
    S7Real: SetDWord(pval, plongword(pval)^);
    S7DT_To_DateTime: pDateTime(pval)^:=GetDateTime(pval);
    DateTime_To_S7DT: begin
      DT:=pdateTime(pval)^;
      SetDateTime(pval, DT);
    end;
  end;
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetBit(pval: pointer; BitIndex: integer; const Value: boolean);
Const
  Mask : array[0..7] of byte = ($01,$02,$04,$08,$10,$20,$40,$80);
begin
  if BitIndex<0 then BitIndex:=0;
  if BitIndex>7 then BitIndex:=7;
  if Value then
    pbyte(pval)^ := pbyte(pval)^ or Mask[BitIndex]
  else
    pbyte(pval)^ := pbyte(pval)^ and not Mask[BitIndex];
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetDateTime(pval: pointer; const Value: TDateTime);
Type
  DATE_AND_TIME = packed array[1..8] of byte;
  pDT = ^DATE_AND_TIME;

  function BCD(Value : word) : byte;
  begin
    Result:=((Value div 10) shl 4) OR (Value mod 10);
  end;

  function BCDW(Value : word) : word;
  Var
    AppC, AppD : word;
  begin
    AppC :=(Value div 100);
    Dec(Value,AppC * 100);
    AppD :=(Value div 10);
    Dec(Value,AppD * 10);
    Result:=(AppC shl 8) + (AppD shl 4) + Value;
  end;

Var
  DT : pDT;
  MsDOW, AYear, AMonth, ADay,
  AHour, AMinute, ASecond, AMilliSecond: Word;
begin
  DT:=pval;
  //DecodeDateTime(Value,AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond);
  if AYear>1999 then
    AYear:=AYear-2000
  else
    AYear:=AYear-1900;

  DT^[1]:=BCD(AYear);
  DT^[2]:=BCD(AMonth);
  DT^[3]:=BCD(ADay);
  DT^[4]:=BCD(AHour);
  DT^[5]:=BCD(AMinute);
  DT^[6]:=BCD(ASecond);
  MsDOW:=(BCDW(AMillisecond) shl 4) + DayOfWeek(Value)+1;
  DT^[7]:=HI(MsDOW);
  DT^[8]:=LO(MsDOW);
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetDInt(pval: pointer; const Value: longint);
Var
  DW : packed array[0..3] of byte absolute value;
begin
  pbyte(pval)^:=DW[3];
  pbyte(NativeInt(pval)+1)^:=DW[2];
  pbyte(NativeInt(pval)+2)^:=DW[1];
  pbyte(NativeInt(pval)+3)^:=DW[0];
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetDWord(pval: pointer; const Value: longword);
Var
  DW : packed array[0..3] of byte absolute value;
begin
  pbyte(pval)^:=DW[3];
  pbyte(NativeInt(pval)+1)^:=DW[2];
  pbyte(NativeInt(pval)+2)^:=DW[1];
  pbyte(NativeInt(pval)+3)^:=DW[0];
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetInt(pval: pointer; const Value: smallint);
Var
  BW : packed array[0..1] of byte absolute value;
begin
  pbyte(NativeInt(pval)+1)^:=BW[0];
  pbyte(pval)^:=BW[1];
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetReal(pval: pointer; const Value: single);
Var
  DW : packed array[0..3] of byte absolute value;
begin
  pbyte(pval)^:=DW[3];
  pbyte(NativeInt(pval)+1)^:=DW[2];
  pbyte(NativeInt(pval)+2)^:=DW[1];
  pbyte(NativeInt(pval)+3)^:=DW[0];
end;
//------------------------------------------------------------------------------
procedure TS7Helper.SetWord(pval: pointer; const Value: word);
Var
  BW : packed array[0..1] of byte absolute value;
begin
  pbyte(NativeInt(pval)+1)^:=BW[0];
  pbyte(pval)^:=BW[1];
end;
0
Andrzej K-a napisał(a):

Pytanie jak mam się do niej odwołać, aby np tablicę [$01 $02] ( zapis w sterowniku zmiennej typu int) móc odczytać jako typu samllint jakiejś zmiennej
lub w drugą stronę jak przekonwertować 23765 na wartości w hexie i dostać odpowiednie [$XX $XX]

IntToHex

0

Ewentualnie Format.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0