CRC16 Modbus Problem
Cameron Davidson
Posts: 6
I'am having problems with the checksum of this piece of kit any help well much appreciated.
It's been a bit of a *****.
I have tried this.
DEFINE_FUNCTION CalculateCRC (CHAR cData[255])
{
STACK_VAR LONG nCRC_Hi LONG nCRC_Lo INTEGER nIndex
nCRC_Hi = ($FF)
nCRC_Lo = ($FF)
SEND_STRING 0:0:70,"'CRC Hi, ',ITOA(nCRC_Hi),' CRC Lo, ',ITOA(nCRC_Lo)"
FOR(nIndex = 1 ; nIndex < LENGTH_STRING(cData) ; nIndex ++)
{
nCRC_Hi = nCRC_Lo ^ cCRCHiArray[cData[nIndex]]
nCRC_Lo = cCRCLoArray[cData[nIndex]]
SEND_STRING 0:0:70,"'For Loop ',ITOA(nIndex),' CRC Hi, ',ITOA(nCRC_Hi)"
SEND_STRING 0:0:70,"'For Loop ',ITOA(nIndex),' CRC Lo, ',ITOA(nCRC_Lo)"
}
nCRC_Data_Lo = nCRC_Lo
nCRC_Data_Hi = (nCRC_Hi << 8)
SEND_STRING 0:0:70,"'End CRC Lo, ',ITOA(nCRC_Data_Lo)"
SEND_STRING 0:0:70,"'End CRC Hi, ',ITOA(nCRC_Data_Hi)"
}
cScaler_Command_Buffer = "$BE,$EF,$1A,$0C,$00,$00,$00,$3F,$00,$00,$00,$02,$00,$00,$00,$00,$00,$00,$00"
CalculateCRC (cScaler_Command_Buffer)
I Should get $19,$d7 back.
Any Help Please.
Cameron.
It's been a bit of a *****.
I have tried this.
DEFINE_FUNCTION CalculateCRC (CHAR cData[255])
{
STACK_VAR LONG nCRC_Hi LONG nCRC_Lo INTEGER nIndex
nCRC_Hi = ($FF)
nCRC_Lo = ($FF)
SEND_STRING 0:0:70,"'CRC Hi, ',ITOA(nCRC_Hi),' CRC Lo, ',ITOA(nCRC_Lo)"
FOR(nIndex = 1 ; nIndex < LENGTH_STRING(cData) ; nIndex ++)
{
nCRC_Hi = nCRC_Lo ^ cCRCHiArray[cData[nIndex]]
nCRC_Lo = cCRCLoArray[cData[nIndex]]
SEND_STRING 0:0:70,"'For Loop ',ITOA(nIndex),' CRC Hi, ',ITOA(nCRC_Hi)"
SEND_STRING 0:0:70,"'For Loop ',ITOA(nIndex),' CRC Lo, ',ITOA(nCRC_Lo)"
}
nCRC_Data_Lo = nCRC_Lo
nCRC_Data_Hi = (nCRC_Hi << 8)
SEND_STRING 0:0:70,"'End CRC Lo, ',ITOA(nCRC_Data_Lo)"
SEND_STRING 0:0:70,"'End CRC Hi, ',ITOA(nCRC_Data_Hi)"
}
cScaler_Command_Buffer = "$BE,$EF,$1A,$0C,$00,$00,$00,$3F,$00,$00,$00,$02,$00,$00,$00,$00,$00,$00,$00"
CalculateCRC (cScaler_Command_Buffer)
I Should get $19,$d7 back.
Any Help Please.
Cameron.
0
Comments
This function returns the CRC bytes all ready to be concatenated into your send packet like:
Apparently this is one weirdo protocol trying to CRC it's own header and CRC byte positions.
Thanks Greg
That is fantastic i will try this on Monday.
Its a very strange way indeed. thats why i was a bit confussed.
Thanks Again
Cameron