Announcement

Collapse
No announcement yet.

CRC Generation issues

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CRC Generation issues

    I am currently using a uLCD70DT-A[v0.8, 13/10/23] to develop an HMI for a small PLC based application. The communication uses Modbus RTU protocol and I have already written the basic Query/Response functions. In order to generate/check the modbus message's CRC I tried the library function crc_MODBUS(buf,count) with no success. As soon as the function is called the program freezes and the message: "ADDRESS TRAP" shows at the top of the display.

    In a effort to explore the issue a little bit deeper I wrote a short 4d program (see below) that calls each one of the four CRC generation library functions, one at a time. All of them end up the same way, i.e. displaying the address trap message, so I'm wondering what I am doing wrong. The rest of the code in my project works perfectly with the exact behavior indicated in the documentation, so I am quite puzzled by this.

    By the way, it is nice to find out that pointers work perfectly in 4d (although not mentioned in the 4DGL Programmers Reference Manual) and, together with private variables allow to create some nice reusable code. Being used to program high end industrial systems, finding so much power in such a small and inexpensive product is a wonder. Kudos for that and, please, keep the good work going.

    Right now I am using my own CRC generation function, so my project keeps going on; only that I would like to have an answer to my initial troubles, if possible.

    Regards
    Domingo


    P.D.
    I guess some other users have already suggested this but.....
    What about some additional variable types, such as Boolean and Char?

    =========================================================================================
    Code: CRC_Test.4dg

    #platform "uLCD-70DT"
    #inherit "4DGL_16bitColours.fnc"

    func main()
    var msg := "123456789";
    var msgLen;
    var crc;

    gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change

    print("\nModbus CRC Test\n") ;
    msgLen := strlen(msg);
    print("msglen: ",msgLen);
    print(" msg:",[STR]msg);

    //Uncomment only one of the four lines below to test each crc function
    crc := crc_MODBUS(str_Ptr(msg),msgLen);
    //crc := crc_CSUM_8(str_Ptr(msg),msgLen);
    //crc := crc_CCITT(str_Ptr(msg),msgLen, 0);
    //crc := crc_16(str_Ptr(msg),msgLen);

    print("\nCRC :",[HEX2]crc);

    repeat
    forever
    endfunc

    =========================================================================================




  • #2
    Err, I think the main issue with your code is you are trying to generate a CRC of constant storage. I suspect you can actually do that without the str_Ptr(), but since it would appear to be a useless exercise I would worry about trying that much if I were you.

    Try using

    var msg[20] ;

    and this to initialise the data:-

    to(msg)
    print("123456789\0") ;
    Mark

    Comment


    • #3
      Hello Mark

      Thank you for your quick response but your suggestion didn't work either.

      In the real application the buffer is a variable and its content is always dynamic. First the buffer is filled with the Modbus request, then the CRC calculated and appended to the buffer and, finally, the CRC calculation is applied again to check that the CRC of the whole message is zero. I would believe that the CRC generation should work both on a constant or variable array because there is no modification of the original content involved. At least, it works this way in my hand-coded version.

      Could you please send me a short example to test?

      Thanks again.

      Comment


      • #4
        I thought I gave you the information needed to modify the code you already presented.

        Anyway, here it is modified

        Code:
        #platform "uLCD-70DT"
        #inherit "4DGL_16bitColours.fnc"
        
        func main()
            var msg[20] ;
            var msgLen;
            var crc;
        
            gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change
        
            to(msg) ;
            print("123456789") ;
        
            print("\nModbus CRC Test\n") ;
            msgLen := strlen(msg);
            print("msglen: ",msgLen);
            print(" msg:",[STR]msg);
        
            //Uncomment only one of the four lines below to test each crc function
            crc := crc_MODBUS(str_Ptr(msg),msgLen);
            //crc := crc_CSUM_8(str_Ptr(msg),msgLen);
            //crc := crc_CCITT(str_Ptr(msg),msgLen, 0);
            //crc := crc_16(str_Ptr(msg),msgLen);
        
            print("\nCRC :",[HEX2]crc);
        
            // example of how to use a constant to calculate a CRC, not really practical
            var msgconst := "123456789";
            print("\n\nModbus Constant CRC Test\n") ;
            msgLen := strlen(msgconst);
            print("msglen: ",msgLen);
            print(" msg:", [STR] msgconst);
        
            //Uncomment only one of the four lines below to test each crc function
            crc := crc_MODBUS(msgconst,msgLen);
            print("\nCRC :",[HEX2]crc);
        
            repeat
            forever
        endfunc
        Mark

        Comment


        • #5
          Mark
          Thank you for your patience but your example didn't work either.

          I pasted the program in the Workshop, and tried to run it commenting out either the string or the constant section. In both instances the result is that the print statement after calling crc_MODBUS never executes.


          Code:
          <script type="text/javascript">
          #platform "uLCD-70DT"
          #inherit "4DGL_16bitColours.fnc"
          
          func main()
              var msg[20] ;
              var msgLen;
              var crc;
          
              gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change
          
          
          /*
              // String
              to(msg) ;
              print("123456789") ;
              print("\n\nModbus string variable CRC Test\n") ;
              msgLen := strlen(msg);
              print("msglen: ",msgLen);
              print(" msg:",[STR]msg);
              crc := crc_MODBUS(msg,msgLen);
              print("\nCRC :",[HEX2]crc);
          */
          
              // example of how to use a constant to calculate a CRC, not really practical
              var msgconst := "123456789";
              print("\n\nModbus Constant CRC Test\n") ;
              msgLen := strlen(msgconst);
              print("msglen: ",msgLen);
              print(" msg:", [STR] msgconst);
              crc := crc_MODBUS(msgconst,msgLen);
              print("\nCRC :",[HEX2]crc);
          
              repeat
              forever
          endfunc

          Comment


          • #6
            This a picture of the screen after running the code in my previous post.
            Could you reproduce this in another unit? I'm still wondering what's wrong with this.
            Thanks again.

            Attached Files
            Last edited by TUTAN; 11 June 2014, 05:53 AM.

            Comment


            • #7
              It works fine for me. Are you using an old PmmC?
              Mark

              Comment


              • #8
                Yes, and that was the issue. After updating to the latest version (R12) it worked fine. Than you for your help.

                Comment

                Working...
                X