Type–length–value in Codesys

damian177
2021-12-23
2022-01-02
  • damian177 - 2021-12-23

    Someone have a library to encode TLV data to Codesys?

     

    Last edit: damian177 2021-12-23
  • hermsen

    hermsen - 2021-12-24

    Hi,

    Personally I never came across such a library. But that said, reading https://en.m.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value at first glance it seems not too difficult to implement an encoder/decoder.

    good luck and merry Christmas 🎄⛄

     
  • nothinrandom - 2022-01-02

    @damian177,

    This is pretty much the format that most, if not all, industrial control protocols (e.g. EtherNet/IP, S7, SLMP) follow. Some header that specifies command type and length of the message and is followed by the message data itself. The best way to handle this would be to create a byte array, then use sysmem.cpy and copy the bytes into such array to be sent other some medium (e.g. Ethernet).

    For example:
    1. Create a byte array called `_abOutput : ARRAY [1..4096] OF BYTE;`
    2. Create a STRUCT called `_stMyData` with some variables such as:
        1.  `uiType : UINT;`
        2. `uiLen : UINT;`
        3. `sData : STRING;`
    3. Fill out your STRUCT (e.g. uiType := 1, sData := 'hello world', uiLen := len(sData))
    4. Then use SysMemCpy to copy into your buffer via something like this `SysMemCpy(pDest:=ADR(_abOutput), pSrc:=ADR(_stMyData), udiCount:=(_stMyData.uiLen+2));`.  The `+2` accounts for the data type size of uiType.
    

    Anyway, then have TCP write client send this out. As the receive part, just do the same thing. Create a struct and copy from the receive buffer back into a STRUCT for "easy" parsing.

     

Log in to post a comment.