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: , , , ,

Simple CRC32 function for SIMATIC controllers.


FUNCTION "FCCRC32Calc" : DWord
  { S7_Optimized_Access := 'TRUE' }
  VERSION : 0.1
  VAR_INPUT
    data : Array[0..4095] of Byte;
    length : Int;
  END_VAR

  VAR_TEMP
    rem : DWord;
    i : Int;
    j : Int;
  END_VAR

  VAR CONSTANT
    POLYNOMIAL : DWord := 16#ABCDEF01;  // Chose your polynomial here
  END_VAR

  BEGIN
  #rem := 16#ffffffff;
  FOR #i := 0 TO (#length-1) DO
    #rem := #rem XOR #data[#i];
    FOR #j := 0 TO 7 DO
      IF #rem.%X31 THEN // if leftmost (most significant) bit is set
        #rem := SHR(IN := #rem, N := 1) XOR #POLYNOMIAL;
      ELSE
        #rem := SHR(IN := #rem, N := 1);
      END_IF;
    END_FOR;
  END_FOR;
  #FCCRC32Calc := #rem XOR 16#ffffffff;
END_FUNCTION

If the function does not work as expected, it’s often because the endianess is not correct between sender and receiver. Change the #rem.%X31 bit to #rem.%X0 to see if it helps.

March 28th, 2018

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

Tags: , , ,

Although SIMOTION does not support Modbus out-of-the-box, it does support TCP/IP socket communication. Siemens has not made a library for Modbus TCP/IP, so when I needed one, I had to make it myself.

What is Modbus and how does it works? I started reading the Protocol Specification and then the TCP/IP Implementation Guide from Modbus.org

Protocol Specification

This document describes the protocol and how it works, disregard of communication form. The Modbus protocol  is a simpel protocol data unit called PDU (nice right?) that consists of a function code and some data.

PDU

The additional fields is added when mapping the protocol on different networks or busses. In this case we use TCP/IP.

TCP/IP Implementation Guide

This document describes how to map the modbus protocol onto a TCP socket communication. When doing so an additional field is added called MBAP Header.

modbus_tcp_adu

The MBAP Header contains;

mbap_header

MODBUS TCP/IP Client for SIMOTION

The function block I made for SIMOTION only implements the functions codes 0x03 (Read Holding Registers) and 0x10 (Write Multiple Registers). Others I don’t need. I used the MB_CLIENT for S7-1200/1500 from Siemens as templates, hence the naming is similar and not following the programing style guide from PLCOpen.

Because we use TCP the function also requires the LCom Library for SIMOTION from Siemens. I could use the system functions for TCP but, it is just easier to use LCom since Siemens are maintaining it, and it works. It can be downloaded here:

https://support.industry.siemens.com/cs/ww/en/view/48955385

Also the help content provided in TIA Portal for the MB_CLIENT block can be used, as the parameters behave the same.

Attachments:

 

May 11th, 2016

Posted In: Automation, SIMOTION, Structured Text (SCL/ST)

Tags: , , , , ,

Next Page »