Split IEC 61131-3 DINT into two INT variables (PLC structured text)

2k views Asked by At

I want to publish a DINT variable (dintTest) over MODBUS on a PLC to read it with Matlab Instrument Control Toolbox. Turns out, Matlab can read Modbus variables but only INT16. So i want to split the DINT variable into two INT variables in IEC. I found this solution, but this only allows values from +- 0 ... 32767^2:

dintTest := -2;

b := dintTest MOD 32767;
a := dintTest / 32767;
result := 32767 * a + b;

c := DINT_TO_INT(b); // publish over modbus
d := DINT_TO_INT(a); // publish over modbus

What would be the solution for the whole range of DINT? Thanks!

edit: I read with a matlab function block in simulink (requires Instrument Control Toolbox):

function Check = MBWriteHoldingRegs(Values,RegAddr)
coder.extrinsic('modbus');
m = modbus('tcpip', '192.169.237.17');
coder.extrinsic('write');
write(m,'holdingregs',RegAddr,double(Values),'int16');
Check = Values;
1

There are 1 answers

0
Sergey Romanov On

I would better split DINT to 2 WORD

VAR
    diInt: DINT := -2;
    dwTemp: DWORD;
    w1: WORD;
    w2: WORD;
END_VAR


dwTemp := DINT_TO_DWORD(diInt);

w1 := DWORD_TO_WORD(dwTemp);
w2 := DWORD_TO_WORD(SHR(dwTemp, 16));

And then I could build it back in matlab.

The point here is not using mathematic but bit masks.