How is this 'C' BXOR calculation working? How would I do this in Clarion?
Author Message
How is this 'C' BXOR calculation working? How would I do this in Clarion?

The sio array is passed 'STX4030109141652ETX'

The calculated result field CRC contains the value 39.

int  chk_crc   ( char *sio , int cmpr )
{
int  i , ix;
unsigned char ck0 , ck1 , ck2 , uc , crc;

if ( type == 'S' )
{    i = 0;
while ( sio[i] != STX )
{    i++;
}
i++;
crc = 0;
while ( sio[i] != ETX )
{    crc ^= sio[i++];
}
crc ^= sio[i++];
if ( crc < 32 )
crc += 32;
if ( diag )
printf("My crc = %2.2x\n",crc);
if ( cmpr )
{    if ( sio[i] == crc )
return(1);
else
return(0);
}
else
{    sio[i] = crc;
i++;
sio[i] = 0;
return(i);
}
}
else

TIA,
Peter Steiner

Wed, 29 Jun 2005 02:40:42 GMT
How is this 'C' BXOR calculation working? How would I do this in Clarion?
Peter,

From what you included this shows how to calculate the crc.
In your source string, 'STX4030109141652ETX', the STX and ETX are CHR(02) and CHR(03)
respectively.

CalcCRC    PROCEDURE(*STRING sio)
i           LONG,AUTO
cbSio       LONG,AUTO
STX         EQUATE('<02>')
ETX         EQUATE('<03>')
crc         BYTE,AUTO
CODE
cbSio = LEN(CLIP(sio))
!Skip everything up to and including STX
LOOP i = 1 TO cbSio
IF sio[i] = STX
i += 1
BREAK
END
END

!use the rest of the values including the ETX in the crc
crc = 0
LOOP i = i TO cbSio
crc = BXOR(crc, VAL(sio[i]))
IF sio[i] = ETX THEN BREAK.
END
IF crc < 32
crc += 32
END
RETURN crc

This returns 39h given your sample string.
HTH
Larry Sand

| The sio array is passed 'STX4030109141652ETX'
|
| The calculated result field CRC contains the value 39.
|
| int  chk_crc   ( char *sio , int cmpr )
| {
|      int  i , ix;
|      unsigned char ck0 , ck1 , ck2 , uc , crc;
|
|      if ( type == 'S' )
|      {    i = 0;
|           while ( sio[i] != STX )
|           {    i++;
|           }
|           i++;
|           crc = 0;
|           while ( sio[i] != ETX )
|           {    crc ^= sio[i++];
|           }
|           crc ^= sio[i++];
|           if ( crc < 32 )
|                crc += 32;
|           if ( diag )
|                printf("My crc = %2.2x\n",crc);
|           if ( cmpr )
|           {    if ( sio[i] == crc )
|                     return(1);
|                else
|                     return(0);
|           }
|           else
|           {    sio[i] = crc;
|                i++;
|                sio[i] = 0;
|                return(i);
|           }
|      }
|      else
|
| TIA,
| Peter Steiner

Fri, 01 Jul 2005 05:12:46 GMT
How is this 'C' BXOR calculation working? How would I do this in Clarion?
Peter,

|   cbSio = LEN(CLIP(sio))
|

Sorry about that, don't clip the string.  What you do depends on how the buffer is
declared.  I like to have the calling function pass in the length of the buffer to prevent
overruns.

Larry Sand

Fri, 01 Jul 2005 07:36:56 GMT
How is this 'C' BXOR calculation working? How would I do this in Clarion?
Larry,

Thank You :) your code was simple compared to what I had come up with.
I thought I needed to convert to hex than submit each character of the
hex value for the bxor. I see that the problem is easily handled in
decimal.

Peter Steiner

Quote:

> Peter,

> |   cbSio = LEN(CLIP(sio))
> |

> Sorry about that, don't clip the string.  What you do depends on how the buffer is
> declared.  I like to have the calling function pass in the length of the buffer to prevent
> overruns.

> Larry Sand

Sat, 02 Jul 2005 14:19:22 GMT
How is this 'C' BXOR calculation working? How would I do this in Clarion?
Peter,

| Thank You :) your code was simple compared to what I had come up with.
| I thought I needed to convert to hex than submit each character of the
| hex value for the bxor. I see that the problem is easily handled in
| decimal.

You're welcome.  It really doesn't matter if you use hex or decimal they're all the same
bits! <g>
For your information, using VAL() in a computation like this is very efficient.  It
compiles to just a few lines of ASM code.

Larry Sand

Sun, 03 Jul 2005 23:43:56 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages