Been using C for so dam long now, i cant even think straight converting this function to basic.
IF anybody has the time and idea what i may have jacked up feel free to comment.
BOOL CheckCRC( int length )
{
WORD crc=0;
BYTE data;
WORD x,total;
// get WORD value for the CRC data in response
total = (BYTE)vp2buffer[length - 2];
total = total << 8;
total |= (BYTE)vp2buffer[length - 1];
// Dont CRC the last 2 bytes of the data
length -=2;
for(x=1; x < length;x++)
{
data = vp2buffer[x];
crc = crc_table [(crc >> 8) ^ data] ^ (crc << 8);
}
if(crc == total)
{
return true;
}
return false;
}
as best i can convert it, i came up with this, but it does not work.
There are suttle differences as i am passing the data string this time to the basic function, whereas the C function the data was global
Function Check_CRC(string_data as string, length as word) as boolean
dim crc as word
dim data as byte
dim x as word
dim total as word
dim a,b as word
crc = 0
' get WORD value for the CRC data in response
' total = (BYTE)vp2buffer[length - 2];
' total = total << 8;
' total |= (BYTE)vp2buffer[length - 1];
total = asc(mid(string_data,length - 2,1))
total = total * &h80
total = total + asc(mid(string_data,length - 1,1))
' Dont CRC the last 2 bytes of the data
'length -=2;
length = length - 2
'for(x=1; x < length;x++)
for x=1 to length
'data = vp2buffer[x];
data = asc(mid(string_data,x,1))
'crc = crc_table [(crc >> 8) ^ data] ^ (crc << 8);
'(crc >> 8) ^ data
a = (crc / &h80 AND &h00ff) XOR data
'(crc << 8)
b = crc * &h80 AND &hff00
crc = crc + ( crc_table(a) XOR b)
Next x
printf("Total = " + str(total) + "\r\n")
printf("CRC = " + str(CRC) + "\r\n")
If crc = total Then
Check_CRC = true
Else
Check_CRC = false
End If
End Function