056-167 CODESYS DataConversion
056-167 CODESYS DataConversion
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.
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:
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).
2.1.1 DECLARATION
VAR
RawData : WORD;
EngineSpeed : REAL;
END_VAR
2.2.1 DECLARATION
VAR
RawData : WORD;
EngineSpeed : REAL;
END_VAR
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