SHARE
TWEET

Untitled

a guest Sep 10th, 2019 173 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FUNCTION_BLOCK PUBLIC ReadRegisterMir
  2. VAR_INPUT
  3.     sRegister : STRING(3);
  4. END_VAR
  5. VAR_OUTPUT
  6.     rValue : REAL;
  7.     diError : BOOL;
  8.     diDone : BOOL;
  9. END_VAR
  10. VAR
  11.     wState : WORD := 0;
  12.     URL_add : STRING(255) := '?whitelist=value';
  13.    
  14.     (* HTTP_GET *)
  15.     oHttpGet : FbHTTP_GET;
  16.     sURL : STRING(255) := '192.168.12.20/api/v2.0.0/registers/';
  17.     sURI : STRING(255);
  18.     MyAuthentification : eAuthentication := 1;
  19.     xHttpSend : BOOL;
  20.     sStatus : WagoSysErrorBase.FbResult;
  21.     aRxBuffer : ARRAY [0..19999] OF BYTE;
  22.     uiResponse : UDINT;
  23.    
  24.     (* Parsing *)
  25.     uiPos : UDINT;
  26.     sValue : STRING;
  27.    
  28.     (* Helpers *)
  29.     i : INT;
  30.     iHelp : INT;
  31.     aRxHelp : ARRAY [0..180] OF BYTE;
  32.     psHelp : POINTER TO STRING(180);
  33.     URL_ADDON: STRING(255);
  34. END_VAR
  35.  
  36.  
  37. oHttpGet(
  38.         sURI:= sURI,
  39.         sUser:= 'admin' ,
  40.         sPassword:= '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918',
  41.         sHeader:= 'Content-Type: application/json',
  42.         eAuthentication:= MyAuthentification,
  43.         pRxBuffer:= ADR(aRxBuffer),
  44.         udiRxBufferSize:= 20000,
  45.         tTimeout:= T#5S,
  46.         xTrigger:= xHttpSend,
  47.         xBusy=> ,
  48.         xError=> diError,
  49.         oStatus=> sStatus,
  50.         udiRxNBytes=> uiResponse);
  51.  
  52. CASE wState OF
  53. 0 : (* IDLE - wait for something to do *)
  54.     diDone := FALSE;
  55.     URL_ADDON := CONCAT (sRegister, URL_add);
  56.     sURI := CONCAT(sURL, URL_ADDON);
  57.     xHttpSend := TRUE;
  58.     wState := 10;
  59.    
  60. 10 : (* Wait for HTTP-GET response data *)
  61.     IF NOT xHttpSend THEN
  62.         IF  NOT diError THEN
  63.             wState := 20;
  64.         ELSE
  65.             wState := 999;     
  66.         END_IF
  67.     END_IF
  68.    
  69. 20 :
  70.     sValue := StringRef_to_PrintableString(pInString:= ADR(aRxBuffer));
  71.     uiPos := FindRight(sBuffer:= sValue, sProbe:= '"value":', udiBegin:= 0);
  72.     i := 0;
  73.     WHILE (aRxBuffer[uiPos+LEN('"value":')+i] <> 10) DO
  74.         aRxHelp[i] := aRxBuffer[uiPos+LEN('"value":')+i];
  75.         i := i+1;
  76.         IF (i+LEN('"value":')+uiPos) >= uiResponse THEN
  77.             EXIT;
  78.         END_IF
  79.     END_WHILE
  80.     aRxHelp[i] := 3;
  81.     sValue := StringRef_to_PrintableString(pInString := ADR(aRxHelp));
  82.     rValue := STRING_TO_REAL(sValue);
  83.    
  84.     wState := 21;
  85.    
  86. 21 :
  87.     diDone := TRUE;
  88.     wState := 0;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top