Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Sorry really long shot but I have a manual in German which is baffling me! It shows how to calculate a CRC value for a serial device I'm trying to write code in VB6 for. Not only do I not understand the German but the example isn't in a language I recognize (falling at every hurdle!). Ultimately I need to turn whatever this reveals into VB6 code but even knowing what language the example is would help in my Google searches.

Here it comes....

4.6 Berechnung des CRC-Zeichens
Folgender Programmabschnitt erklärt die Berechnung des CRC-Zeichens. Der Type U8 ist eine 8bit-Variable ohne Vorzeichen (0 bis 255)
Die Funktion put_tx1_buffer( U8 c ) sendet ein Zeichen über die serielle Schnittstelle.


#define POLYNOM 0xB1  // 28+27+25+24+20+1
#define INIT_TX_CRC {tx_crc=0xA5;} U8 tx_crc ;
void build_tx_crc8( U8 a )
{
U8 i=8 ;
do
{
if (( a & 0x01 ) != ( tx_crc & 0x01 ))
{
tx_crc >>= 1 ;
tx_crc ^= POLYNOM ;
}
else
{
tx_crc >>= 1 ;
}
a >>= 1 ;
}
while  (--i!=0) ;
}

void set_lamp( U8 keyboardnumber, U8 lampnumber, U8 command )
{
INIT_TX_CRC ;
put_tx1_buffer( STX+0x80               ) ; build_tx_crc8( STX+0x80             ) ; put_tx1_buffer( command                  ) ; build_tx_crc8( command                  ) ; put_tx1_buffer( keyboardnumber      ) ; build_tx_crc8( keyboardnumber       ) ; put_tx1_buffer( lampnumber             ) ; build_tx_crc8( lampnumber              ) ; put_tx1_buffer( ETX+0x80               ) ; build_tx_crc8( ETX+0x80               ) ; put_tx1_buffer( tx_crc                       ) ;
}


What I have tried:

Google translate, C# but didn't seem to be that.
Posted
Updated 10-Aug-17 4:33am

Looks like C code but not complete. Part of the code is missing.
Correctly indented code is easier to read.
C++
#define POLYNOM 0xB1  // 28+27+25+24+20+1
#define INIT_TX_CRC {tx_crc=0xA5;} U8 tx_crc ;
void build_tx_crc8( U8 a )
{
    U8 i=8 ;
    do
    {
        if (( a & 0x01 ) != ( tx_crc & 0x01 ))
        {
            tx_crc >>= 1 ;
            tx_crc ^= POLYNOM ;
        }
        else
        {
            tx_crc >>= 1 ;
        }
        a >>= 1 ;
    }
    while  (--i!=0) ;
}

void set_lamp( U8 keyboardnumber, U8 lampnumber, U8 command )
{
    INIT_TX_CRC ;
    put_tx1_buffer( STX+0x80               ) ;
    build_tx_crc8( STX+0x80             ) ;
    put_tx1_buffer( command                  ) ;
    build_tx_crc8( command                  ) ;
    put_tx1_buffer( keyboardnumber      ) ;
    build_tx_crc8( keyboardnumber       ) ;
    put_tx1_buffer( lampnumber             ) ;
    build_tx_crc8( lampnumber              ) ;
    put_tx1_buffer( ETX+0x80               ) ;
    build_tx_crc8( ETX+0x80               ) ;
    put_tx1_buffer( tx_crc                       ) ;
}

It looks like some 8 bits CRC calculation.
Cyclic redundancy check - Wikipedia[^]
 
Share this answer
 
It is a C implementation of a CRC calculation written probably for some kind of microcontroller.

Even if you would know C and understand the algorithm it would not make much sense to convert that to VB6 (while possible). You probably want to calculate the CRC for a string or a sequence of binary data. You should be able to find examples in the net.

If you are going to write code for the same device as the C code, you have use the same start value (hex A5) and the same polynomial (hex B1). If not, these must be specified in the documentation for your serial device.

Then implement the CRC algorithm in VB using examples from the net.

Or do it yourself. It is quite simple:

  • You are working with byte values (which may be also single characters)
  • Create a variable to hold the CRC and initialse it with the start value
  • Apply the CRC for each byte / character of the input within a loop handling the 8 bits:
    • If the LSB (the lowest bit) of input byte and CRC are identical shift both right by one
    • Otherwise shift also both right by one and XOR the CRC value with the polynomial
  • The CRC variable contains now the calculated CRC
 
Share this answer
 
You don't need to translate from German. But you do need to understand both C and VB6 programming languages. You might also build a DLL using the C source and consume it in your VB6 application.
 
Share this answer
 
v2
4.6 Berechnung des CRC-Zeichens
Folgender Programmabschnitt erklärt die Berechnung des CRC-Zeichens. Der Type U8 ist eine 8bit-Variable ohne Vorzeichen (0 bis 255)
Die Funktion put_tx1_buffer( U8 c ) sendet ein Zeichen über die serielle Schnittstelle.


4.6 Calculation of CRC-Char
Folowing Codepart describes the calculation of the CRC-Char. The Type U8 is a 8bit unsigned var (0 to 255)
The function put_tx1_buffer(U8 c) sends a Char over the serielport
 
Share this answer
 
Thanks everyone I will start looking into this. The reason I'm trying to find a VB6 solution is because this will be implemented into a much larger project written in C. Thank you for the formatting ppolymorphe. Its made it much clearer. Will post what i come up with.
 
Share this answer
 
Here is my resulting VB6 to calculate the CRC for anyone who finds it useful.

VB
Private tx_crc As Integer
Private Const Polynom As Integer = &HB1    ' 28+27+25+24+20+1

Public Sub InitTxCrc()
    tx_crc = &HA5
End Sub


Public Sub BuildTxCrc(ByVal a As Integer)
    a = a And &HFF    ' Make it byte
    Dim i As Integer
    i = 8

    Do

        If ((a And 1) <> (tx_crc And 1)) Then
            tx_crc = tx_crc \ 2
            tx_crc = tx_crc Xor Polynom
        Else
            tx_crc = tx_crc \ 2
        End If
        i = i - 1
        a = a \ 2
    Loop Until i = 0

End Sub

Public Sub BuildTxCrcHex(ByVal ah As String)
    BuildTxCrc (Val("&H" & ah))
End Sub

Public Function GetTxCrc() As Integer
    GetTxCrc = tx_crc
End Function


Thanks for your help everyone
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900