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












0














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;









share|improve this question
























  • 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
















0














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;









share|improve this question
























  • 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














0












0








0







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;









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:24

























asked Nov 12 '18 at 11:57









Biomusli

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 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


















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer


























      0














      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.






      share|improve this answer
























        0












        0








        0






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 7:36









        Sergey Romanov

        1,68921628




        1,68921628






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini