MediaStorm
asked on
Need Help Converting C Code to Delphi
Below is some C code I'm trying to get working in Delphi but it uses some C stuff I'm not completely clear about in the Delphi world.
Someone please help me convert this to the Delphi equivalent. I'm not concerned with the COM PORT stuff shown; a text box showing the final values is fine for my needs (I'm just looking for a working example):
//CALC CHECKSUM FOR RCVD ASCII STRING, RETURN CHECKSUM VAL 0 = GOOD
//EXAMPLE:
// as00 = 06as0066 (VALID)
// 06 = PKT LEN
// as = PKT CMD
// 00 = PKT VAL
// 66 = CHECKSUM
INT8U itAscRecBuf[82];
INT8U AscHexToBin(INT8U, INT8U *);
INT8U AsciiToHex( INT8U);
INT8U CalcCheckSum(void)
{
INT8U i,length, cc;
length = AscHexToBin(2, &itAscRecBuf[0]); //PKT LEN=POS1 & POS2
cc = AscHexToBin(2, &itAscRecBuf[length]);
for (i=0;i<length ;i++ )
{
cc += itAscRecBuf[i];
}
return(cc); //GOOD CHECKSUM = 0
}
INT8U AscHexToBin(INT8U Width, INT8U * DataPtr)
{
INT8U aVal;
aVal = AsciiToHex(*DataPtr);
DataPtr++;
if (Width == 2)
{
aVal = aVal << 4;
aVal += AsciiToHex(*DataPtr);
}
return(aVal);
}
INT8U AsciiToHex( INT8U Value )
{
switch ( Value )
{
case 'A':
return( 10 );
case 'B':
return( 11 );
case 'C':
return( 12 );
case 'D':
return( 13 );
case 'E':
return( 14 );
case 'F':
return( 15 );
default:
return( Value - 0x30 );
}
}
// SEND CHECKSUM + CR/LF
// (QUICK USAGE EXAMPLE USING COM PORT)
INT8U NibToAsc(INT8U);
void SendChecksum(INT8U CC)
{
CC = (CC ^ 0xFF) + 1;
Comm2_Put_Ch(NibToAsc(CC >> 4));
Comm2_Put_Ch(NibToAsc(CC & 0x0F));
Comm2_Put_Ch(0x0D);
Comm2_Put_Ch(0x0A);
}
INT8U NibToAsc(INT8U Nib)
{
if (Nib < 0x0A)
return( (INT8U)(Nib + 0x30));
else if (Nib <= 0x0F)
return(Nib + 0x37);
return(0x20);
}
Someone please help me convert this to the Delphi equivalent. I'm not concerned with the COM PORT stuff shown; a text box showing the final values is fine for my needs (I'm just looking for a working example):
//CALC CHECKSUM FOR RCVD ASCII STRING, RETURN CHECKSUM VAL 0 = GOOD
//EXAMPLE:
// as00 = 06as0066 (VALID)
// 06 = PKT LEN
// as = PKT CMD
// 00 = PKT VAL
// 66 = CHECKSUM
INT8U itAscRecBuf[82];
INT8U AscHexToBin(INT8U, INT8U *);
INT8U AsciiToHex( INT8U);
INT8U CalcCheckSum(void)
{
INT8U i,length, cc;
length = AscHexToBin(2, &itAscRecBuf[0]); //PKT LEN=POS1 & POS2
cc = AscHexToBin(2, &itAscRecBuf[length]);
for (i=0;i<length ;i++ )
{
cc += itAscRecBuf[i];
}
return(cc); //GOOD CHECKSUM = 0
}
INT8U AscHexToBin(INT8U Width, INT8U * DataPtr)
{
INT8U aVal;
aVal = AsciiToHex(*DataPtr);
DataPtr++;
if (Width == 2)
{
aVal = aVal << 4;
aVal += AsciiToHex(*DataPtr);
}
return(aVal);
}
INT8U AsciiToHex( INT8U Value )
{
switch ( Value )
{
case 'A':
return( 10 );
case 'B':
return( 11 );
case 'C':
return( 12 );
case 'D':
return( 13 );
case 'E':
return( 14 );
case 'F':
return( 15 );
default:
return( Value - 0x30 );
}
}
// SEND CHECKSUM + CR/LF
// (QUICK USAGE EXAMPLE USING COM PORT)
INT8U NibToAsc(INT8U);
void SendChecksum(INT8U CC)
{
CC = (CC ^ 0xFF) + 1;
Comm2_Put_Ch(NibToAsc(CC >> 4));
Comm2_Put_Ch(NibToAsc(CC & 0x0F));
Comm2_Put_Ch(0x0D);
Comm2_Put_Ch(0x0A);
}
INT8U NibToAsc(INT8U Nib)
{
if (Nib < 0x0A)
return( (INT8U)(Nib + 0x30));
else if (Nib <= 0x0F)
return(Nib + 0x37);
return(0x20);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i do not know about sense
i only converted you your functions
i only converted you your functions
ASKER
Not a problem.. I'll figure it out. Thanks for the help.
ASKER
IE - I would pass it: SendChecksum('as00') and the result would be '06as0066'.
Length = data + lengthVal
The checksum would be calc'd from '06as00' in this case and the result would be '66' conccat'd result = '06as0066'
Does that make sense? I'm happy to bump up the points for the great help and fast response time you've provided as well.