CRC checksum algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ValK

    #1

    CRC checksum algorithm

    Hello,
    I’m working with handheld device that communicates with windows service thru
    the serial port.
    Transaction between device and my application looks like this:

    Handshaking:
    Device sends ENQ
    Service responds DLE 0
    Device sends DLESOH416884000 0SR01L01DLEETX4 á

    As you see at the end of the string, after DLE ETX (Data Link escape and end
    of text) control codes, device sends two more characters 4á.
    If I’m not mistaking, this would be the calculation of CRC checksum of this
    string.

    After each transaction, I need to calculate CRC and compare to the one that
    device sends. Just to make sure that we didn’t miss any data.
    I tried many different ways, but can’t come up with the correct algorithm to
    get 4á on this string.
    Is anybody can give me a hand on this one?

    Thanks

  • Dick Grier

    #2
    Re: CRC checksum algorithm

    Hi,

    This might be a CRC or, perhaps, a simpler checksum. Since it is only a
    single byte, my guess is that it is not a CRC (though I have CRC-8 code in
    my book, its use is quite uncommon).

    You are printing the data as ASCII/ANSI (text), so the font used can confuse
    things. If you print out the actual binary data received, I may be able to
    run a quick check to see what algorithm has been used.

    Dick

    --
    Richard Grier (Microsoft Visual Basic MVP)

    See www.hardandsoftware.net for contact information.

    Author of Visual Basic Programmer's Guide to Serial Communications, 4th
    Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
    www.mabry.com/vbpgser4 to order.


    Comment

    • Charles Law

      #3
      Re: CRC checksum algorithm

      This looks similar to an Allen-Bradley protocol, so you might want to look
      this up. As the last character is only 8-bit it is more likely to be a BCC
      (binary check character) rather than a CRC, which is usually at least
      16-bit.

      The normal way to calculate a BCC is to sum the data MOD 256. Bear in mind,
      though, that _data_ does not mean the entire binary string. BCC is usually
      calculated on only a part of the string. In the case of an Allen-Bradley
      protocol this would be all data between (but not including) the DLE SOH and
      DLE ETX. Also, A-B actually takes the two's complement of the resulting BCC.

      Ultimately, the only way to know for certain is to get some reference for
      the equipment you are working with from the manufacturer.

      HTH

      Charles


      "ValK" <ValK@discussio ns.microsoft.co m> wrote in message
      news:376C5F72-536F-4469-94A6-75047A07C1F6@mi crosoft.com...[color=blue]
      > Hello,
      > I'm working with handheld device that communicates with windows service
      > thru
      > the serial port.
      > Transaction between device and my application looks like this:
      >
      > Handshaking:
      > Device sends ENQ
      > Service responds DLE 0
      > Device sends DLESOH416884000 0SR01L01DLEETX4 á
      >
      > As you see at the end of the string, after DLE ETX (Data Link escape and
      > end
      > of text) control codes, device sends two more characters 4á.
      > If I'm not mistaking, this would be the calculation of CRC checksum of
      > this
      > string.
      >
      > After each transaction, I need to calculate CRC and compare to the one
      > that
      > device sends. Just to make sure that we didn't miss any data.
      > I tried many different ways, but can't come up with the correct algorithm
      > to
      > get 4á on this string.
      > Is anybody can give me a hand on this one?
      >
      > Thanks
      >[/color]


      Comment

      • ValK

        #4
        Re: CRC checksum algorithm

        Dick,
        Here is the actual binary data of this string:

        ?bRead()
        {Length=23}
        (0): 16
        (1): 1
        (2): 52
        (3): 49
        (4): 54
        (5): 56
        (6): 56
        (7): 52
        (8): 48
        (9): 48
        (10): 48
        (11): 48
        (12): 83
        (13): 82
        (14): 48
        (15): 49
        (16): 76
        (17): 48
        (18): 49
        (19): 16
        (20): 3
        (21): 52
        (22): 225


        "Dick Grier" wrote:
        [color=blue]
        > Hi,
        >
        > This might be a CRC or, perhaps, a simpler checksum. Since it is only a
        > single byte, my guess is that it is not a CRC (though I have CRC-8 code in
        > my book, its use is quite uncommon).
        >
        > You are printing the data as ASCII/ANSI (text), so the font used can confuse
        > things. If you print out the actual binary data received, I may be able to
        > run a quick check to see what algorithm has been used.
        >
        > Dick
        >
        > --
        > Richard Grier (Microsoft Visual Basic MVP)
        >
        > See www.hardandsoftware.net for contact information.
        >
        > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
        > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
        > www.mabry.com/vbpgser4 to order.
        >
        >
        >[/color]

        Comment

        • wg

          #5
          Re: CRC checksum algorithm

          I am in the process of looking for code for a CRC 16H checksum algorithim.
          If anyone has this or a reference to where to find one it would be greatly
          appriciated. I am writing an interface to old old PLC (Programmable Logic
          Controller).

          Thanks

          wg
          "ValK" <ValK@discussio ns.microsoft.co m> wrote in message
          news:376C5F72-536F-4469-94A6-75047A07C1F6@mi crosoft.com...[color=blue]
          > Hello,
          > I'm working with handheld device that communicates with windows service
          > thru
          > the serial port.
          > Transaction between device and my application looks like this:
          >
          > Handshaking:
          > Device sends ENQ
          > Service responds DLE 0
          > Device sends DLESOH416884000 0SR01L01DLEETX4 á
          >
          > As you see at the end of the string, after DLE ETX (Data Link escape and
          > end
          > of text) control codes, device sends two more characters 4á.
          > If I'm not mistaking, this would be the calculation of CRC checksum of
          > this
          > string.
          >
          > After each transaction, I need to calculate CRC and compare to the one
          > that
          > device sends. Just to make sure that we didn't miss any data.
          > I tried many different ways, but can't come up with the correct algorithm
          > to
          > get 4á on this string.
          > Is anybody can give me a hand on this one?
          >
          > Thanks
          >[/color]


          Comment

          • wg

            #6
            Re: CRC checksum algorithm

            Dick,

            I took a look at you book on your web site. As mentioned in a post I am
            working on an older PLC interafce to OPC. I have written OPC servers before
            so this is not a problem. I have never writtin a serial communications in
            VB.NET until now. I have gotten an example rs232 class that seems to work
            fine. I will have to write a CRC algoritham and since this interface is
            attached to 10 PLC's on 10 serial ports I was thinking of multi-threading
            the it. What books would you recommend I look into? I would really rather
            not use the MSCOMM.OCX from VB6.

            Thanks


            wg
            "Dick Grier" <dick_grierNOSP AM@msn.com> wrote in message
            news:%23sfOktLf FHA.1044@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Hi,
            >
            > This might be a CRC or, perhaps, a simpler checksum. Since it is only a
            > single byte, my guess is that it is not a CRC (though I have CRC-8 code in
            > my book, its use is quite uncommon).
            >
            > You are printing the data as ASCII/ANSI (text), so the font used can
            > confuse things. If you print out the actual binary data received, I may
            > be able to run a quick check to see what algorithm has been used.
            >
            > Dick
            >
            > --
            > Richard Grier (Microsoft Visual Basic MVP)
            >
            > See www.hardandsoftware.net for contact information.
            >
            > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
            > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
            > www.mabry.com/vbpgser4 to order.
            >
            >[/color]


            Comment

            • Richard L Rosenheim

              #7
              Re: CRC checksum algorithm

              I don't know if you are able to use a beta product or not, but just to let
              you know, Visual Studio 2005 includes a serial communication control.

              Richard Rosenheim


              "wg" <wg@hotmail.com > wrote in message
              news:xrHwe.3840 $qm.3014@bignew s5.bellsouth.ne t...[color=blue]
              > Dick,
              >
              > I took a look at you book on your web site. As mentioned in a post I am
              > working on an older PLC interafce to OPC. I have written OPC servers[/color]
              before[color=blue]
              > so this is not a problem. I have never writtin a serial communications in
              > VB.NET until now. I have gotten an example rs232 class that seems to work
              > fine. I will have to write a CRC algoritham and since this interface is
              > attached to 10 PLC's on 10 serial ports I was thinking of multi-threading
              > the it. What books would you recommend I look into? I would really rather
              > not use the MSCOMM.OCX from VB6.
              >
              > Thanks
              >
              >
              > wg
              > "Dick Grier" <dick_grierNOSP AM@msn.com> wrote in message
              > news:%23sfOktLf FHA.1044@tk2msf tngp13.phx.gbl. ..[color=green]
              > > Hi,
              > >
              > > This might be a CRC or, perhaps, a simpler checksum. Since it is only a
              > > single byte, my guess is that it is not a CRC (though I have CRC-8 code[/color][/color]
              in[color=blue][color=green]
              > > my book, its use is quite uncommon).
              > >
              > > You are printing the data as ASCII/ANSI (text), so the font used can
              > > confuse things. If you print out the actual binary data received, I may
              > > be able to run a quick check to see what algorithm has been used.
              > >
              > > Dick
              > >
              > > --
              > > Richard Grier (Microsoft Visual Basic MVP)
              > >
              > > See www.hardandsoftware.net for contact information.
              > >
              > > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
              > > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
              > > www.mabry.com/vbpgser4 to order.
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • wg

                #8
                Re: CRC checksum algorithm

                Richard,

                I have not downloaded it yet. I am worried about it being a beta version.
                Will this affect anything?


                wg


                "Richard L Rosenheim" <richard@rlr.co m> wrote in message
                news:uKsogsTfFH A.2840@tk2msftn gp13.phx.gbl...[color=blue]
                >I don't know if you are able to use a beta product or not, but just to let
                > you know, Visual Studio 2005 includes a serial communication control.
                >
                > Richard Rosenheim
                >
                >
                > "wg" <wg@hotmail.com > wrote in message
                > news:xrHwe.3840 $qm.3014@bignew s5.bellsouth.ne t...[color=green]
                >> Dick,
                >>
                >> I took a look at you book on your web site. As mentioned in a post I am
                >> working on an older PLC interafce to OPC. I have written OPC servers[/color]
                > before[color=green]
                >> so this is not a problem. I have never writtin a serial communications in
                >> VB.NET until now. I have gotten an example rs232 class that seems to work
                >> fine. I will have to write a CRC algoritham and since this interface is
                >> attached to 10 PLC's on 10 serial ports I was thinking of multi-threading
                >> the it. What books would you recommend I look into? I would really rather
                >> not use the MSCOMM.OCX from VB6.
                >>
                >> Thanks
                >>
                >>
                >> wg
                >> "Dick Grier" <dick_grierNOSP AM@msn.com> wrote in message
                >> news:%23sfOktLf FHA.1044@tk2msf tngp13.phx.gbl. ..[color=darkred]
                >> > Hi,
                >> >
                >> > This might be a CRC or, perhaps, a simpler checksum. Since it is only
                >> > a
                >> > single byte, my guess is that it is not a CRC (though I have CRC-8 code[/color][/color]
                > in[color=green][color=darkred]
                >> > my book, its use is quite uncommon).
                >> >
                >> > You are printing the data as ASCII/ANSI (text), so the font used can
                >> > confuse things. If you print out the actual binary data received, I
                >> > may
                >> > be able to run a quick check to see what algorithm has been used.
                >> >
                >> > Dick
                >> >
                >> > --
                >> > Richard Grier (Microsoft Visual Basic MVP)
                >> >
                >> > See www.hardandsoftware.net for contact information.
                >> >
                >> > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                >> > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                >> > www.mabry.com/vbpgser4 to order.
                >> >
                >> >[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                • ValK

                  #9
                  Re: CRC checksum algorithm

                  wg,
                  You have a few options:
                  You can get free Visual Basic Resource Kit from Microsoft. I has in it the
                  Sax Communications Community Edition .NET Serial control. Something like
                  MSComm control.

                  Also if you look at Dick Grier web site www.hardandsoftware.net , you can
                  download NETComm.ocx control.

                  Val


                  "wg" wrote:
                  [color=blue]
                  > Richard,
                  >
                  > I have not downloaded it yet. I am worried about it being a beta version.
                  > Will this affect anything?
                  >
                  >
                  > wg
                  >
                  >
                  > "Richard L Rosenheim" <richard@rlr.co m> wrote in message
                  > news:uKsogsTfFH A.2840@tk2msftn gp13.phx.gbl...[color=green]
                  > >I don't know if you are able to use a beta product or not, but just to let
                  > > you know, Visual Studio 2005 includes a serial communication control.
                  > >
                  > > Richard Rosenheim
                  > >
                  > >
                  > > "wg" <wg@hotmail.com > wrote in message
                  > > news:xrHwe.3840 $qm.3014@bignew s5.bellsouth.ne t...[color=darkred]
                  > >> Dick,
                  > >>
                  > >> I took a look at you book on your web site. As mentioned in a post I am
                  > >> working on an older PLC interafce to OPC. I have written OPC servers[/color]
                  > > before[color=darkred]
                  > >> so this is not a problem. I have never writtin a serial communications in
                  > >> VB.NET until now. I have gotten an example rs232 class that seems to work
                  > >> fine. I will have to write a CRC algoritham and since this interface is
                  > >> attached to 10 PLC's on 10 serial ports I was thinking of multi-threading
                  > >> the it. What books would you recommend I look into? I would really rather
                  > >> not use the MSCOMM.OCX from VB6.
                  > >>
                  > >> Thanks
                  > >>
                  > >>
                  > >> wg
                  > >> "Dick Grier" <dick_grierNOSP AM@msn.com> wrote in message
                  > >> news:%23sfOktLf FHA.1044@tk2msf tngp13.phx.gbl. ..
                  > >> > Hi,
                  > >> >
                  > >> > This might be a CRC or, perhaps, a simpler checksum. Since it is only
                  > >> > a
                  > >> > single byte, my guess is that it is not a CRC (though I have CRC-8 code[/color]
                  > > in[color=darkred]
                  > >> > my book, its use is quite uncommon).
                  > >> >
                  > >> > You are printing the data as ASCII/ANSI (text), so the font used can
                  > >> > confuse things. If you print out the actual binary data received, I
                  > >> > may
                  > >> > be able to run a quick check to see what algorithm has been used.
                  > >> >
                  > >> > Dick
                  > >> >
                  > >> > --
                  > >> > Richard Grier (Microsoft Visual Basic MVP)
                  > >> >
                  > >> > See www.hardandsoftware.net for contact information.
                  > >> >
                  > >> > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                  > >> > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                  > >> > www.mabry.com/vbpgser4 to order.
                  > >> >
                  > >> >
                  > >>
                  > >>[/color]
                  > >
                  > >[/color]
                  >
                  >
                  >[/color]

                  Comment

                  • Dick Grier

                    #10
                    Re: CRC checksum algorithm

                    Hi,
                    [color=blue][color=green]
                    >>[/color][/color]
                    I will have to write a CRC algoritham and since this interface is
                    attached to 10 PLC's on 10 serial ports I was thinking of multi-threading
                    the it. What books would you recommend I look into? I would really rather
                    not use the MSCOMM.OCX from VB6.
                    <<

                    Threading is OK.

                    Easiest my be to download DesktopSerialIO dll from my homepage. It already
                    implements threaded receive, so you don't really need anything more. It is
                    practical to simply use the OnComm events, which execute in the object's
                    worker thread context.

                    I have CRC examples in my book, and I suspect that the CRC-16 (CCITT) is the
                    one that you need.

                    Dick

                    --
                    Richard Grier (Microsoft Visual Basic MVP)

                    See www.hardandsoftware.net for contact information.

                    Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                    Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                    www.mabry.com/vbpgser4 to order.


                    Comment

                    • Dick Grier

                      #11
                      Re: CRC checksum algorithm

                      Actually, the DesktopSerialIO dll that I have placed on my site is better
                      (IMO) than NETComm.ocx for .NET applications, unless you are porting from
                      VB6 -- where it is just about a wash.

                      Dick

                      --
                      Richard Grier (Microsoft Visual Basic MVP)

                      See www.hardandsoftware.net for contact information.

                      Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                      Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                      www.mabry.com/vbpgser4 to order.


                      Comment

                      • Dick Grier

                        #12
                        Re: CRC checksum algorithm

                        Hi,

                        This looks like:
                        DLE-STX[data]DLE-ETX[2-byteCS]

                        Is that your understanding? If so, then this probably is a 16-bit CRC, not
                        a simple checksum. The trick is to determine what CRC (or, perhaps some
                        other form of 2-byte checksum). Do you have any description from the vendor
                        on what it is, or how it is calculated?

                        I've tried your data with a standard CRC-16, and the result is D066, not
                        34FF (hex format), so my guess it that this CS is different.

                        Can you get any more information on what or how this CS is used?

                        Dick

                        --
                        Richard Grier (Microsoft Visual Basic MVP)

                        See www.hardandsoftware.net for contact information.

                        Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                        Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                        www.mabry.com/vbpgser4 to order.


                        Comment

                        • Dick Grier

                          #13
                          Re: CRC checksum algorithm

                          Hi,

                          I have two different CRC-16 algorithms in my book. You also can download
                          from my homepage. However, there are lots of possible algorithms, so you
                          need to know for sure which one is being used. Probably the CCITT CRC-16,
                          but...

                          Dick

                          --
                          Richard Grier (Microsoft Visual Basic MVP)

                          See www.hardandsoftware.net for contact information.

                          Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                          Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                          www.mabry.com/vbpgser4 to order.


                          Comment

                          • wg

                            #14
                            Re: CRC checksum algorithm

                            Dick,

                            I just ordered you book to help out. I also downloaded the DestopIO dll to
                            ty. The algorithm is CRC-16H or CCITTA.

                            Thanks,


                            wg


                            "Dick Grier" <dick_grierNOSP AM@msn.com> wrote in message
                            news:O03v2gZfFH A.2152@TK2MSFTN GP14.phx.gbl...[color=blue]
                            > Hi,
                            >
                            > I have two different CRC-16 algorithms in my book. You also can download
                            > from my homepage. However, there are lots of possible algorithms, so you
                            > need to know for sure which one is being used. Probably the CCITT CRC-16,
                            > but...
                            >
                            > Dick
                            >
                            > --
                            > Richard Grier (Microsoft Visual Basic MVP)
                            >
                            > See www.hardandsoftware.net for contact information.
                            >
                            > Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                            > Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                            > www.mabry.com/vbpgser4 to order.
                            >
                            >[/color]


                            Comment

                            • Dick Grier

                              #15
                              Re: CRC checksum algorithm

                              Hi,

                              What protocol? Some protocols calculate the CRC over only the data (not the
                              packet header and trailer), and some calculate the CRC over the entire
                              packet.

                              Dick

                              --
                              Richard Grier (Microsoft Visual Basic MVP)

                              See www.hardandsoftware.net for contact information.

                              Author of Visual Basic Programmer's Guide to Serial Communications, 4th
                              Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
                              www.mabry.com/vbpgser4 to order.


                              Comment

                              Working...