Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

'C' to 'Delphi 5' conversion

Hi,

I need some help converting the following C code to its Delphi 5 equivelant:

longpar1 |= (((long)stringbuf[0]) & 0x000000FF) << 24;

any help would be appreciated.

Cheers
0
scotdance
Asked:
scotdance
  • 7
  • 5
  • 2
1 Solution
 
DragonSlayerCommented:
umm...

longpar = longpar or ((integer(stringbuf[0]) and $FF) shl 24);
0
 
DragonSlayerCommented:
oops

change

longpar = longpar or ((integer(stringbuf[0]) and $FF) shl 24);

to

longpar := longpar or ((integer(stringbuf[0]) and $FF) shl 24);
0
 
scotdanceAuthor Commented:
thanks for that,

but the problem just got bigger :(

the following is the c code i'm trying to convert to delphi with little success.

It is sending a command to the com port and supposed to be decoding the response but i'm having little luck with the conversions as the last time i done c was more than five years ago.

long read(char station,char type, int nr)
{
long param = 0;
long parameters[21];
for (int i=0;i<=20;i++)
        parameters[i] = 0;
int err;
int parteller = 0;
char stringbuf[10];
char ontvangen = 1;
long longpar1,longpar2;

if (nr != 500)
        stringbuf[0] = 0;
else
        stringbuf[0] = 0x01;

the command line is formed (0 or 0x01)

PurgeComm(hCom,PURGE_TXCLEAR);
PurgeComm(hCom,PURGE_RXCLEAR);

this two previous lines delete everything in the comport buffers

WriteFile(hCom,stringbuf,1,&written,NULL);

this line writes the command line to the scale (command is to retrieve all parameters from the scale)

if (nr != 500)
        {
        while((parteller <= 20)&&(ontvangen == 1))
                {
                ontvangen = 0;
                ReadFile(hCom,stringbuf,8,&written,NULL);

          this line reads the response from the scale

                longpar1 = 0;
                longpar2 = 0;

     the response is decoded.    

                longpar1 |= (((long)stringbuf[0]) & 0x000000FF) << 24;
                longpar1 |= (((long)stringbuf[1]) & 0x000000FF) << 16;
                longpar1 |= (((long)stringbuf[2]) & 0x000000FF) << 8;
                longpar1 |= (((long)stringbuf[3]) & 0x000000FF);
                longpar2 |= (((long)stringbuf[4]) & 0x000000FF) << 24;
                longpar2 |= (((long)stringbuf[5]) & 0x000000FF) << 16;
                longpar2 |= (((long)stringbuf[6]) & 0x000000FF) << 8;
                longpar2 |= (((long)stringbuf[7]) & 0x000000FF);
                if (longpar1 == longpar2)
                        {
                        ontvangen = 1;
                        parameters[parteller] = longpar1;
                        parteller++;
                        }
                }
        param = parameters[nr]; (the requested parameter is taken out and returns)
        }
else      //readout the new_haul paramter (1= no new haul, 2 = new haul)
        {
        ontvangen = 0;
        ReadFile(hCom,stringbuf,8,&written,NULL);
        longpar1 = 0;
        longpar2 = 0;
        longpar1 |= (((long)stringbuf[0]) & 0x000000FF) << 24;
        longpar1 |= (((long)stringbuf[1]) & 0x000000FF) << 16;
        longpar1 |= (((long)stringbuf[2]) & 0x000000FF) << 8;
        longpar1 |= (((long)stringbuf[3]) & 0x000000FF);
        longpar2 |= (((long)stringbuf[4]) & 0x000000FF) << 24;
        longpar2 |= (((long)stringbuf[5]) & 0x000000FF) << 16;
        longpar2 |= (((long)stringbuf[6]) & 0x000000FF) << 8;
        longpar2 |= (((long)stringbuf[7]) & 0x000000FF);
        if (longpar1 == longpar2)
               {
               ontvangen = 1;
               parameters[parteller] = longpar1;
               parteller++;
               }
        param = parameters[0];
        }
return param;
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
scotdanceAuthor Commented:
thanks for that,

but the problem just got bigger :(

the following is the c code i'm trying to convert to delphi with little success.

It is sending a command to the com port and supposed to be decoding the response but i'm having little luck with the conversions as the last time i done c was more than five years ago.

long read(char station,char type, int nr)
{
long param = 0;
long parameters[21];
for (int i=0;i<=20;i++)
        parameters[i] = 0;
int err;
int parteller = 0;
char stringbuf[10];
char ontvangen = 1;
long longpar1,longpar2;

if (nr != 500)
        stringbuf[0] = 0;
else
        stringbuf[0] = 0x01;

the command line is formed (0 or 0x01)

PurgeComm(hCom,PURGE_TXCLEAR);
PurgeComm(hCom,PURGE_RXCLEAR);

this two previous lines delete everything in the comport buffers

WriteFile(hCom,stringbuf,1,&written,NULL);

this line writes the command line to the scale (command is to retrieve all parameters from the scale)

if (nr != 500)
        {
        while((parteller <= 20)&&(ontvangen == 1))
                {
                ontvangen = 0;
                ReadFile(hCom,stringbuf,8,&written,NULL);

          this line reads the response from the scale

                longpar1 = 0;
                longpar2 = 0;

     the response is decoded.    

                longpar1 |= (((long)stringbuf[0]) & 0x000000FF) << 24;
                longpar1 |= (((long)stringbuf[1]) & 0x000000FF) << 16;
                longpar1 |= (((long)stringbuf[2]) & 0x000000FF) << 8;
                longpar1 |= (((long)stringbuf[3]) & 0x000000FF);
                longpar2 |= (((long)stringbuf[4]) & 0x000000FF) << 24;
                longpar2 |= (((long)stringbuf[5]) & 0x000000FF) << 16;
                longpar2 |= (((long)stringbuf[6]) & 0x000000FF) << 8;
                longpar2 |= (((long)stringbuf[7]) & 0x000000FF);
                if (longpar1 == longpar2)
                        {
                        ontvangen = 1;
                        parameters[parteller] = longpar1;
                        parteller++;
                        }
                }
        param = parameters[nr]; (the requested parameter is taken out and returns)
        }
else      //readout the new_haul paramter (1= no new haul, 2 = new haul)
        {
        ontvangen = 0;
        ReadFile(hCom,stringbuf,8,&written,NULL);
        longpar1 = 0;
        longpar2 = 0;
        longpar1 |= (((long)stringbuf[0]) & 0x000000FF) << 24;
        longpar1 |= (((long)stringbuf[1]) & 0x000000FF) << 16;
        longpar1 |= (((long)stringbuf[2]) & 0x000000FF) << 8;
        longpar1 |= (((long)stringbuf[3]) & 0x000000FF);
        longpar2 |= (((long)stringbuf[4]) & 0x000000FF) << 24;
        longpar2 |= (((long)stringbuf[5]) & 0x000000FF) << 16;
        longpar2 |= (((long)stringbuf[6]) & 0x000000FF) << 8;
        longpar2 |= (((long)stringbuf[7]) & 0x000000FF);
        if (longpar1 == longpar2)
               {
               ontvangen = 1;
               parameters[parteller] = longpar1;
               parteller++;
               }
        param = parameters[0];
        }
return param;
}

0
 
scotdanceAuthor Commented:
weird that added that twice
0
 
scotdanceAuthor Commented:
weird that added that twice
0
 
zebadaCommented:
This is just as I typed it, I haven't checked it for logic errors, hope it helps.

var
  MsgBuf: array[0..1023] of Char;
  hCom: THandle;

// --------------------------------------------------------------------------
function MAKELANGID(p,s: Cardinal): Cardinal;
begin
  Result := (WORD(s) shl 10) + WORD(p);
end;

// --------------------------------------------------------------------------
function WindowsMessage(Msg: DWORD): string;
begin
  if ( Msg=0 ) then
    Result :=''
  else
  begin
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM+FORMAT_MESSAGE_IGNORE_INSERTS,
                  nil,Msg,
                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                  @MsgBuf[0],1024,nil);
    Result := MsgBuf;
  end;
end;

// --------------------------------------------------------------------------
function read(station: Char; _type: Char; nr: integer): longint;
var
  param: longint;
  parameters: array[0..20] of longint;
  err: integer;
  parteller: integer;
  stringbuf: array[0..9] of Char;
  ontvangen: integer;
  longpar1,longpar2: longint;

  i: integer;
  byteswritten,bytesread: Cardinal;
begin
  param := 0;
  for i:=0 to 19 do
    parameters[i] := 0;
  parteller := 0;
  ontvangen := 1;

  if ( nr<>500 ) then
    stringbuf[0] := Chr($00)
  else
    stringbuf[0] := Chr($01);

  PurgeComm(hCom,PURGE_TXCLEAR);
  PurgeComm(hCom,PURGE_RXCLEAR);

  if ( not WriteFile(hCom,stringbuf[0],1,byteswritten,nil) ) then
    Raise Exception.CreateFmt('Can''t write to com port. %s',[WindowsMessage(getLastError)]);

  if ( nr<>500 ) then
  begin
    while ( (parteller<=20) and (ontvangen=1) ) do
    begin
      ontvangen := 0;
      if ( not ReadFile(hCom,stringbuf[0],8,bytesread,nil) ) then
        Raise Exception.CreateFmt('Can''t read from com port. %s',[WindowsMessage(getLastError)]);
      longpar1 := 0;
      longpar2 := 0;

      longpar1 := longpar1 or (Ord(stringbuf[0]) and $FF) shl 24;
      longpar1 := longpar1 or (Ord(stringbuf[1]) and $FF) shl 16;
      longpar1 := longpar1 or (Ord(stringbuf[2]) and $FF) shl 8;
      longpar1 := longpar1 or (Ord(stringbuf[3]) and $FF);
      longpar2 := longpar2 or (Ord(stringbuf[4]) and $FF) shl 24;
      longpar2 := longpar2 or (Ord(stringbuf[5]) and $FF) shl 16;
      longpar2 := longpar2 or (Ord(stringbuf[6]) and $FF) shl 8;
      longpar2 := longpar2 or (Ord(stringbuf[7]) and $FF);
      if ( longpar1=longpar2 ) then
      begin
        ontvangen := 1;
        parameters[parteller] := longpar1;
        Inc(parteller);
      end;
      param := parameters[nr];
    end;
  end
  else
  begin
    ontvangen := 0;
    if ( not ReadFile(hCom,stringbuf[0],8,bytesread,nil) ) then
      Raise Exception.CreateFmt('Can''t read from com port. %s',[WindowsMessage(getLastError)]);
    longpar1 := 0;
    longpar2 := 0;

    longpar1 := longpar1 or (Ord(stringbuf[0]) and $FF) shl 24;
    longpar1 := longpar1 or (Ord(stringbuf[1]) and $FF) shl 16;
    longpar1 := longpar1 or (Ord(stringbuf[2]) and $FF) shl 8;
    longpar1 := longpar1 or (Ord(stringbuf[3]) and $FF);
    longpar2 := longpar2 or (Ord(stringbuf[4]) and $FF) shl 24;
    longpar2 := longpar2 or (Ord(stringbuf[5]) and $FF) shl 16;
    longpar2 := longpar2 or (Ord(stringbuf[6]) and $FF) shl 8;
    longpar2 := longpar2 or (Ord(stringbuf[7]) and $FF);
    if ( longpar1=longpar2 ) then
    begin
      ontvangen := 1;
      parameters[parteller] := longpar1;
      Inc(parteller);
    end;
    param := parameters[0];
  end;
  Result := param;
end;
0
 
DragonSlayerCommented:
ah... zebada is faster (I shouldn't have gone home... hehehehehe).

since he (she?) has posted a solution, let's wait for scotdance to test it out first.
0
 
zebadaCommented:
Hi DragonSlayer,

I'm a "he" just in case you're wondering.
And no guarantees the code works :)

Cheers
Paul
0
 
DragonSlayerCommented:
Ah, so it's "Paul" :)

scotdance, now that I've had the time to go thru your code, it seems similar to something I've done to communicate with a CE105 controller...

So you actually want to read a 32-bit Integer, but you can only read 8-bits at a time, and the Bytes are sent in reverse order, am I correct?

So I suppose this might work as well:

function GetIntegerFromPort: Integer;
var
  Buffer: array[1 .. 4] of Byte;
  TempBuffer: array[1 .. 4] of Byte;
  Value: Integer absolute Buffer;
  i, BytesRead: Integer;
begin
  ReadFile(hCom, TempBuffer[1], 4, BytesRead, nil);
  for i := 1 to 4 do
    Buffer[i] := TempBuffer[5 - i];
  Result := Value;
end;

Of course, the above code is not really that optimised (I declared too many variables, I suppose...)


DragonSlayer
0
 
DragonSlayerCommented:
Oh, and I did not add error-checking.

To use it, you will do something like

LongPar1 := GetIntegerFromPort;
LongPar2 := GetIntegerFromPort;
if (LongPar1 = LongPar2) then
  // do something
else // do something else
0
 
scotdanceAuthor Commented:
Thanks for your help chaps, sorry about the delay in getting back to you all.
0
 
scotdanceAuthor Commented:
Another problem with some more converions from C to Delphi 5

The question id was qid=20181425

Cheers
0
 
scotdanceAuthor Commented:

Cheers for that zebada.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now