Split IEC 61131-3 DINT into two INT variables (PLC structured text)
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;
matlab modbus plc st
|
show 1 more comment
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;
matlab modbus plc st
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it usingread(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.
– Quirzo
Nov 14 '18 at 6:25
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Could you try to use DINT at PLC and then write to it withwrite(m,'holdingregs',RegAddr,Values,'int32');
or something similar?
– Quirzo
Nov 19 '18 at 6:25
|
show 1 more comment
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;
matlab modbus plc st
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;
matlab modbus plc st
matlab modbus plc st
edited Nov 16 '18 at 15:24
Biomusli
asked Nov 12 '18 at 11:57
BiomusliBiomusli
85
85
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it usingread(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.
– Quirzo
Nov 14 '18 at 6:25
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Could you try to use DINT at PLC and then write to it withwrite(m,'holdingregs',RegAddr,Values,'int32');
or something similar?
– Quirzo
Nov 19 '18 at 6:25
|
show 1 more comment
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it usingread(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.
– Quirzo
Nov 14 '18 at 6:25
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Could you try to use DINT at PLC and then write to it withwrite(m,'holdingregs',RegAddr,Values,'int32');
or something similar?
– Quirzo
Nov 19 '18 at 6:25
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it using
read(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.– Quirzo
Nov 14 '18 at 6:25
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it using
read(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.– Quirzo
Nov 14 '18 at 6:25
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Could you try to use DINT at PLC and then write to it with
write(m,'holdingregs',RegAddr,Values,'int32');
or something similar?– Quirzo
Nov 19 '18 at 6:25
Could you try to use DINT at PLC and then write to it with
write(m,'holdingregs',RegAddr,Values,'int32');
or something similar?– Quirzo
Nov 19 '18 at 6:25
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261693%2fsplit-iec-61131-3-dint-into-two-int-variables-plc-structured-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 7:36
Sergey RomanovSergey Romanov
1,69921628
1,69921628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261693%2fsplit-iec-61131-3-dint-into-two-int-variables-plc-structured-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By quickly looking, Matlab supports 32 bit variables for modbus. Please check this link under "Specifying Server ID and Precision". So basically you just read the DINT from PLC by giving the starting address of the variable and then read it using
read(m,'holdingregs',1,2,'uint32');
or similar. If you need to divide the DINT to two INTs, it is possible with MEMCPY for example. Please provide more information if you need help.– Quirzo
Nov 14 '18 at 6:25
Thanks, yes, Matlab supports 32 bit variables, but (i guess) with another representation. When reading a variable, the only combination of variables that gives the same result is Matlab 'int16' and IEC 'INT'.
– Biomusli
Nov 15 '18 at 8:27
Could you provide some example how you are actually doing the reading? I haven't used the matlab modbus myself, just normal modbus.
– Quirzo
Nov 15 '18 at 10:55
Yes, I added the matlab function code to the original question.
– Biomusli
Nov 16 '18 at 15:26
Could you try to use DINT at PLC and then write to it with
write(m,'holdingregs',RegAddr,Values,'int32');
or something similar?– Quirzo
Nov 19 '18 at 6:25