0% found this document useful (0 votes)
117 views5 pages

056-167 CODESYS DataConversion

This document describes methods for converting data in CODESYS, primarily for CAN or MODBUS communications. It discusses endianness in CAN and MODBUS, and provides examples of converting raw bytes of data to other data types using either the DSE_UTILS library functions or custom code. The DSE_UTILS library contains useful functions for converting between bytes, words, double words, and long words to facilitate interpreting CAN and MODBUS messages.

Uploaded by

Bubu Rexha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views5 pages

056-167 CODESYS DataConversion

This document describes methods for converting data in CODESYS, primarily for CAN or MODBUS communications. It discusses endianness in CAN and MODBUS, and provides examples of converting raw bytes of data to other data types using either the DSE_UTILS library functions or custom code. The DSE_UTILS library contains useful functions for converting between bytes, words, double words, and long words to facilitate interpreting CAN and MODBUS messages.

Uploaded by

Bubu Rexha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Conversion in CODESYS

1 INTRODUCTION

NOTE: This document assumes prior knowledge of CAN, MODBUS, CODESYS, Data Types and more.

This document describes methods for converting data in CODESYS, primarily for use in CAN or MODBUS.

1.1 ENDIANNESS
Endiannes describeds the way data is handled and stored when the value is larger than 8 bytes.

CAN MODBUS
CAN J1939 is Little Endian, meaning the lowest MODBUS is Big Endian, meaning the highest significant
significant byte is first. byte is first.

Historically, due to the internal architecture of the respective processors, Little Endian was referred to as Intel byte
order, Big Endian was referred to as Motorola byte order.
When referring to communications protocols, the terms Network Order is often used, describing the order that the
bytes are sent on the communications network.

056-167 CODESYS Data Conversion ISS 1


2 RAW DATA

NOTE: Within the J1939 document, the 8 BYTES in the CAN message are labelled from 1 to 8. Other
documents vary so be sure to understand how the data is documented.

Example:

Let’s assume that we have received the CAN message EEC1 and want to know the Engine Speed, this is defined as
follows within J1939:

Byte Size SPN Resolution Offset


4,5 16 190 0.125 rpm / bit 0

First we need to take the two bytes of data (from bytes 4 and 5) and put them to a 16 bit variable. CAN J1939 is Little
Endian, meaning the lowest significant byte is first. In this example, byte 4 is the lowest significant byte, byte 5 is the
most significant byte.

The options for converting this data are shown in the following sections.

After conversion we must multiply the value by 0.125 to obtain the actual rpm (resolution 0.125 rpm).
The Offset is zero, meaning that there is no further conversion required.

Where CAN data is transmitted that may be a negative value (eg ambient temperature), the Offset is used to set the
zero point of the value. (eg Ambient temperature has an offset of 40 deg C meaning that the message value 0
contains the ambient temperature -40).

056-167 CODESYS Data Conversion ISS 1


2.1 DSE_UTILS
This is the recommended method, taking advantage of the DSE_UTILS compiled library.
DSE_UTILS contains a number of conversion functions for BYTES into other variable types.
In this case we use BYTES_TO_WORD to put the two BYTES into a 16-bit WORD.

2.1.1 DECLARATION
VAR
RawData : WORD;
EngineSpeed : REAL;
END_VAR

2.1.2 STRUCTURED TEXT


DSE_UTILS.BYTES_TO_WORD(B0:=Data[5], B1:=Data[4], W=>RawData);
EngineSpeed:=(WORD_TO_REAL(RawData) * 0.125);

2.1.3 FUNCTION BLOCK DIAGRAM

056-167 CODESYS Data Conversion ISS 1


2.2 CUSTOM CODE
Should you wish to program this yourself, this example shows how to achieve it without using the DSE_UTILS
compiled library.
We need to shift the most significant byte into the top 16-bits of the WORD and then bitwise OR the least significant
byte to give us the full 16-bit value.

2.2.1 DECLARATION
VAR
RawData : WORD;
EngineSpeed : REAL;
END_VAR

2.2.2 STRUCTURED TEXT


RawData:=SHL(BYTE_TO_WORD(Data[5]),8) OR BYTE_TO_WORD(Data[4]);
EngineSpeed:=(WORD_TO_REAL(RawData) * 0.125);

2.2.3 FUNCTION BLOCK DIAGRAM

056-167 CODESYS Data Conversion ISS 1


3 ADDITIONAL CONVERSION FUNCTIONS FOR BYTES WITHIN DSE_UTILS
LIBRARY

NOTE: Within DSE_UTILS many functions are included. See the CODESYS online help within Library
Manager for full details. The below table show functions useful for CAN and MODBUS.

The following functions within DSE_UTILS are useful for BYTE manipulation associated with CAN and MODBUS
among other uses.

Function Usage
BYTES_TO_WORD Converting two BYTES to a 16 bit value
BYTES_TO_DWORD Converting four BYTES to a 32 bit value
BYTES_TO_LWORD Converting eight BYTES to a 64 bit value
WORD_TO_BYTES Converting a 16 bit value to two BYTES
DWORD_TO_BYTES Converting a 32 bit value to four BYTES
LWORD_TO_BYTES Converting a 64 bit value to eight BYTES

056-167 CODESYS Data Conversion ISS 1

You might also like