You are not logged in.

#1 04 Dec 2007 4:19 am

NaugthyGal
Member
Registered: Dec 2007
Posts: 5

CRC16-CCITT

Hi,

I'm testing a machine that communicate with messages that uses CCRC16-CCITT. Trying very hard to find out how CCIT CRC for this machine has been calculated. I tried to use the codes posted in this forum by Matt-Hatter but i face a problem to get the value for E1h (CCIT CRC), D7h (CCIT CRC) & 4Eh (Byte CRC). Need Help!

I have this message that looks like this:-
02h (STX)
44h
00h
00h
00h
1B
2Eh
00h
42h
00h
1B
21h
1B
21h
FFh
00h
1B
21h
00h
E1h (CCIT CRC)
D7h (CCIT CRC)
4Eh (Byte CRC)
04H (EOT)


'------Below are codes that invokes the class Crc16Ccitt:-

Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click
        Dim str As String = Me.SendDataReq
        Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(str)
        Dim crcVal As UInt16 = New Crc16Ccitt(InitialCrcValue.NonZero1).ComputeChecksum(bytes)
        Dim crc As Byte() = New Crc16Ccitt(InitialCrcValue.NonZero1).ComputeChecksumBytes(bytes)
        Me.txtTstText.Text = crcVal
End Sub

'-----This are codes that covert Hex values into decimal
Public Function SendDataReq() As String
        Dim i As Integer
        Dim str As String = "440000001B2E0042001B211B21FF001B2100"
        Dim sReadyData As String
        Dim sCVData As String
        Dim s As String
        i = 1

        Do Until i > Len(str)
            s = Mid(str, i, 2)
            If s = "00" Then
                sCVData = Chr(0)
            Else
                sCVData = Chr(CInt(CLng("&H" & s)))
            End If
            sReadyData = sReadyData & sCVData
            i = i + 2
        Loop

        Return sReadyData
    End Function

Offline

 

#2 04 Dec 2007 10:02 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: CRC16-CCITT

can you explain the message payload a bit? 

44h 00h 00h 00h 1B 2Eh 00h 42h 00h 1B 21h 1B 21h FFh 00h 1B 21h 00h

looks a series of bytes delimited by this 1B byte.  what significance does that character have?  adding it all up doesn't produce e1 d7 with crc16 ccitt, so perhaps there are smaller messages in there that are delimited (but not used to generate the checksum) by '1B'.

thats just a guess...

also, you're using UTF8 to decode the string, but it doesn't look like you're using UTF8 to encode it from a byte array...

infact I'd probably just use something along the lines of:

Code:

Private Shared Function GetRequestBytes(ByVal hexString As String) As Byte()
    Dim count As Integer = (hexString.Length / 2)
    Dim bytes As Byte() = New Byte(count  - 1) {}
    Dim i As Integer = 0
    Dim j As Integer
    For j = 0 To count - 1
        bytes(j) = Byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber)
        i = (i + 2)
    Next j
    Return bytes
End Function

to go from "440000001B2E0042001B211B21FF001B2100" to the byte array you need to pass into the crc function.  I'm not convinced that the 1B portions are part of the message to be checked though.

Last edited by MadHatter (04 Dec 2007 10:22 pm)

Offline

 

#3 04 Dec 2007 10:24 pm

NaugthyGal
Member
Registered: Dec 2007
Posts: 5

Re: CRC16-CCITT

how about if you ignore the 1B, coz this 1Bhex is the rules that is for encoding and decoding the message. If you leave the 1Bhex and minus the following byte with 20 you will get the following message:-

44 00 00 00 0E 00 42h 00 01 01 FF 00 01 00

The above message are in Hex. This is the original message but it still carry the same CRC CITT which is E1h (CCIT CRC), D7h (CCIT CRC), 4Eh (Byte CRC). I have tried, still can't  get it for either way of the messages, regardless with the 1Bhex or not.

You can ask me more questions if you need further details, thanks!

Offline

 

#4 04 Dec 2007 10:39 pm

NaugthyGal
Member
Registered: Dec 2007
Posts: 5

Re: CRC16-CCITT

The reason why I use UTF8 is becoz ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. The message itself is in hex value, UTF8 will support all the values in the message, otherwise if use ASCII encoding some of the bytes in the message will become a question mark(?) instead of its original value.

Offline

 

#5 04 Dec 2007 10:55 pm

NaugthyGal
Member
Registered: Dec 2007
Posts: 5

Re: CRC16-CCITT

I hope i didn't confuse you tongue, here is some details of what is going on to the machine

The message from the machine will look like this:-

D    B   N

if you convert them to hex they will look like this:-

02 44 00 00 00 0e 00 42 00 01 01 ff 00 01 00 e1 d7 4e 04

if i wan to know how is the e1, d7 and 4e come from, i need to convert this series of hex back to its origin(where it come from the machine)that's where it comes to this function below, i use Chr(CInt(CLng("&H" & s))) to convert it back to its origin just like what i received from the machine :-

Public Function SendDataReq() As String
        Dim i As Integer
        Dim str As String = "440000000E0042000101FF000100"
        Dim sReadyData As String
        Dim sCVData As String
        Dim s As String
        i = 1

        Do Until i > Len(str)
            s = Mid(str, i, 2)
            If s = "00" Then
                sCVData = Chr(0)
            Else
                sCVData = Chr(CInt(CLng("&H" & s)))
            End If
            sReadyData = sReadyData & sCVData
            i = i + 2
        Loop

        Return sReadyData
    End Function

Offline

 

#6 05 Dec 2007 3:12 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: CRC16-CCITT

unfortunately I'm snowed under here at work.  I haven't done much rs232 type programming so I don't know right off hand what it may be, but I'll plug away at it in my spare time and see what I see.

Offline

 



© 2003 - 2019 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License