Solved

Need Help Converting C Code to Delphi

Posted on 2004-10-18
4
306 Views
Last Modified: 2010-04-05
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);
}      
0
Comment
Question by:MediaStorm
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
vadim_ti earned 500 total points
ID: 12341668
unit Unit1;

interface

implementation


//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




// SEND CHECKSUM + CR/LF
// (QUICK USAGE EXAMPLE USING COM PORT)
var
itAscRecBuf: array [0..100] of Byte;

procedure Comm2_Put_Ch(ch:Byte);
begin
end;

function AsciiToHex(Value: Byte ):Byte;
begin
  if chr(value) in ['A'..'F'] then
    result := value - ord('A') + 10
  else
    result := value - $30;
end;

function AscHexToBin(Width:Byte; Ptr: Pointer):Byte;
var
     aVal, ch: Byte;
     DataPtr: PChar;
begin

     DataPtr := ptr;
     ch := ord(DataPtr^);
     aVal := AsciiToHex(ch);
     Inc(DataPtr);
     if (Width = 2) then begin
          aVal := aVal shl 4;
          ch := ord(DataPtr^);
          Inc(aVal , AsciiToHex(ch));
     end;
     result := aVal;
end;


function CalcCheckSum: Byte;
var
     i,length, cc: Byte;
begin

     length := AscHexToBin(2, @itAscRecBuf[0]);     //PKT LEN=POS1 & POS2
     cc := AscHexToBin(2, @itAscRecBuf[length]);
     for i:=0 to length-1 do
          INC(cc, itAscRecBuf[i]);
     result := cc;     //GOOD CHECKSUM = 0
end;

function NibToAsc(Nib:Byte):Byte ;
begin
     if Nib < $A then
       result := Nib + $30
     else if Nib <= $F  then
       result := Nib + $37
     else
       result := $20
end;

procedure SendChecksum(CC:Byte);
begin
     CC := (CC xor $FF) + 1;
     Comm2_Put_Ch(NibToAsc(CC shr 4));
     Comm2_Put_Ch(NibToAsc(CC and $F));
     Comm2_Put_Ch($D);
     Comm2_Put_Ch($A);
end;

end.
0
 
LVL 1

Author Comment

by:MediaStorm
ID: 12342183
That's really, really close. What I need to do is be able to send a string to the SendChecksum procedure and return the formatted string including the length, data and checksum.

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.
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12342614
i do not know about  sense
i only converted you your functions

0
 
LVL 1

Author Comment

by:MediaStorm
ID: 12343850
Not a problem.. I'll figure it out. Thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now