Lasse Hedeby

Sharing my passion for automation, programming, IT and other geeky stuff

JSON is a very powerful and simple way of arranging data and exchange it between systems.

https://www.json.org/
https://www.w3schools.com/js/js_json_intro.asp

JSON parser comes for almost every PC based programming language, but not PLC’s, so I started my own.

This parser can read and write JSON in Siemens SIMATIC S7-12xx/S7-15xx PLC’s.
There are some limitations though;

Only strings, numbers and booleans are supported.
No spaces between characters.

 
// Test string 
#tempJSON := '{"name":"Lasse","age":35,"sex":"male","birthDay":"1984-03-02"}'; 
// Convert string to Char array 
Strg_TO_Chars(Strg:=#tempJSON,              
              pChars:=0,               
              Cnt=>#tempCnt,               
              Chars:=#tempChar); 
// Read JSON
"gFBJSONRead"(json:=#tempChar); 

JSON is very useful when systems has to exchange data in e.g. TCP/IP socket communication. Traditionally data exchange has to be documented and agreed on between systems, to have the same data structure for the byte stream. With JSON, this is not needed anymore, and the data and values can be arranged differently but, still understood on all systems.

Attachments:

April 15th, 2019

Posted In: Automation, S7-1200/1500, SIMATIC, Structured Text (SCL/ST)

Tags: , , , ,

I’ve made a random key generator for a S7-1200 PLC. It uses a random integer generator from Siemens to select the character position in a string of charactors and then add this particular character to a new string.┬áThis new string is the unique key.

The random integer generator reads the realtime clock in the CPU and uses the nanosecond data type for random integer generator.

This is not at truly random, unique safe generator that can be used in cryptography or other ways to secure anything. It can add an extra level of security but it can also be predicted.


// Reset in every cycle
#tempError  := False;
#tempStatus := #NO_JOB;

// Rising edge on execute
#statExecuteTrig   := #execute AND NOT #statExecuteMemory;
#statExecuteMemory := #execute;

// Generate key on rising edge
IF #statExecuteTrig THEN
  // Define list of charactors
  #tempCharactors := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA9876543210-';
  #tempCharLength := LEN(#tempCharactors);

  // Charactor loop
  #tempString := '';
  #statRandomInt := 0;
  FOR #tempIndex := 0 TO (#length - 1) DO
    #tempRepeat := 0; // Prevent cycle time overflow
    REPEAT
      #tempRepeat := #tempRepeat + 1;
      // Generate a random number for charactor index
      #tempRandomInt := "LGF_RandomInt"(minValue := 1, maxValue := #tempCharLength);
    UNTIL (#statRandomInt <> #tempRandomInt) OR (#tempRepeat > 4) // Prevent dublicates next to each other
    END_REPEAT;
    // Add to string
    #tempString := CONCAT(IN1 := #tempString, IN2 := MID(IN := #tempCharactors, P := #tempRandomInt, L := 1));
    // Save last random int
    #statRandomInt := #tempRandomInt;
  END_FOR;

  // Write key and length
  #uniqueKey  := #tempString;
  #keyLength  := LEN(#tempString);
  #tempStatus := #JOB_COMPLETE;
END_IF;

// Reset if not execute
IF NOT #execute THEN
  #uniqueKey := '';
  #keyLength := 0;
END_IF;

// Update outputs
#error  := #tempError;
#status := #tempStatus;

Attachements:

 

February 20th, 2016

Posted In: Coding, S7-1200/1500, SIMATIC, Structured Text (SCL/ST)

Tags: , , , ,