Advertisement
Guest User

random crc32 1200 siemens tia portal tiaportal scl st

a guest
Aug 20th, 2018
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. FUNCTION "fc_Calc_CRC32_V15.0.4" : DWord
  2. { S7_Optimized_Access := 'TRUE' }
  3. VERSION : 0.1
  4. VAR_INPUT
  5. Buffer : Variant;
  6. END_VAR
  7.  
  8. VAR_OUTPUT
  9. ob_Error : Bool; // Error with Varriant Input
  10. END_VAR
  11.  
  12. VAR_TEMP
  13. tempBuffer : Array[0..#MaxArray] of Byte;
  14. tempByte : Byte;
  15. tempMaxIndex : UDInt;
  16. tempRetVal : Int;
  17. tempRem : DWord;
  18. tempI : Int;
  19. tempJ : Int;
  20. END_VAR
  21.  
  22. VAR CONSTANT
  23. POLYNOMIAL : DWord := 16#ABCD_EF01; // oder besser 16#EDB8_8320 ?
  24. MaxArray : Int := 64;
  25. END_VAR
  26.  
  27.  
  28. BEGIN
  29. // Ist Varriant passend?
  30. IF IS_ARRAY(#Buffer) AND TypeOfElements(#Buffer) = Byte AND
  31. CountOfElements(#Buffer) < #MaxArray THEN
  32. #tempMaxIndex := CountOfElements(#Buffer);
  33. // Erzeuge eine Byte Array für den CRC32 Code
  34. #tempRetVal := MOVE_BLK_VARIANT(SRC := #Buffer, COUNT := #tempMaxIndex,
  35. SRC_INDEX := 0, DEST_INDEX := 0, DEST => #tempBuffer);
  36. // http://www.hedeby.net/2018/03/28/crc32-function-for-simatic/
  37. // If the function does not work as expected, it’s often because the endianess
  38. // is not correct between sender and receiver.
  39. // Change the #rem.%X0 bit to #rem.%X31 to see if it helps.
  40. #tempRem := 16#FFFF_FFFF;
  41. FOR #tempI := 0 TO UDINT_TO_INT(#tempMaxIndex) - 1 DO
  42. #tempRem := #tempRem XOR #tempBuffer[#tempI];
  43. FOR #tempJ := 0 TO 7 DO
  44. IF #tempRem.%X0 THEN // if leftmost (most significant) bit is set
  45. #tempRem := SHR(IN := #tempRem, N := 1) XOR #POLYNOMIAL;
  46. ELSE
  47. #tempRem := SHR(IN := #tempRem, N := 1);
  48. END_IF;
  49. END_FOR;
  50. END_FOR;
  51. #"fc_Calc_CRC32_V15.0.4" := #tempRem XOR 16#FFFF_FFFF;
  52. ELSE
  53. // NULL zurück wenn Varriant nicht passt
  54. #"fc_Calc_CRC32_V15.0.4" := 0;
  55. #ob_Error := true;
  56. END_IF;
  57. END_FUNCTION
  58.  
  59. FUNCTION "fc_Zufall_DWord_V15.0.4" : DWord
  60. { S7_Optimized_Access := 'TRUE' }
  61. VERSION : 0.1
  62. VAR_INPUT
  63. idw_seed : DWord;
  64. END_VAR
  65.  
  66. VAR_OUTPUT
  67. ob_Error : Bool; // Error with Varriant Input
  68. END_VAR
  69.  
  70. VAR_TEMP
  71. temp_array : Array[0..15] of Byte;
  72. temp_dtl {InstructionName := 'DTL'; LibVersion := '1.0'} : DTL;
  73. temp_dint : DInt;
  74. temp_int : Int;
  75. END_VAR
  76.  
  77.  
  78. BEGIN
  79. // Zeit lesen
  80. #temp_int := RD_SYS_T(#temp_dtl);
  81. #temp_dint := 0;
  82. // Zeit in Byte Array kopieren
  83. #temp_int := Serialize(SRC_VARIABLE := #temp_dtl, DEST_ARRAY => #temp_array, POS := #temp_dint);
  84. IF #temp_int = 0 THEN
  85. // Seed initialisieren
  86. #temp_array[12] := #idw_seed.%B0;
  87. #temp_array[13] := #idw_seed.%B2;
  88. #temp_array[14] := #idw_seed.%B1;
  89. #temp_array[15] := #idw_seed.%B3;
  90. // Berechne CRC32 aus Byte Array
  91. #"fc_Zufall_DWord_V15.0.4" := "fc_Calc_CRC32_V15.0.4"(Buffer := #temp_array, ob_Error => #ob_Error);
  92.  
  93. ELSE
  94. #ob_Error := true;
  95. #"fc_Zufall_DWord_V15.0.4" := 0;
  96. END_IF;
  97. END_FUNCTION
  98.  
  99. FUNCTION "fc_Zufall_Real_V15.0.4" : Real
  100. { S7_Optimized_Access := 'TRUE' }
  101. VERSION : 0.1
  102. VAR_INPUT
  103. minValue : Real; // minimal Value random can reach
  104. maxValue : Real; // maximal Value random can reach
  105. seed : Real;
  106. END_VAR
  107.  
  108. VAR_OUTPUT
  109. ob_Error : Bool; // Error with Varriant Input
  110. END_VAR
  111.  
  112. VAR_TEMP
  113. tempDWord : DWord;
  114. tempReal : Real;
  115. END_VAR
  116.  
  117.  
  118. BEGIN
  119. #tempReal := NORM_X(MIN := #minValue, VALUE := #seed, MAX := #maxValue);
  120. #tempDWord := "fc_Zufall_DWord_V15.0.4"(idw_seed:=SCALE_X(MIN := 0, VALUE := #tempReal, MAX := 16#FFFF_FFFF), ob_Error=>#ob_Error);
  121. #tempReal := NORM_X(MIN := 0, VALUE := #tempDWord, MAX := 16#FFFF_FFFF);
  122. #"fc_Zufall_Real_V15.0.4" := SCALE_X(MIN := #minValue, VALUE := #tempReal, MAX := #maxValue);
  123. END_FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement