[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

'C' to 'Delphi 5' conversion

Posted on 2001-08-02
14
Medium Priority
?
248 Views
Last Modified: 2010-04-06
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
Comment
Question by:scotdance
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6345180
umm...

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

Expert Comment

by:DragonSlayer
ID: 6345188
oops

change

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

to

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

Author Comment

by:scotdance
ID: 6345491
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
Technology Partners: 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!

 

Author Comment

by:scotdance
ID: 6345492
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
 

Author Comment

by:scotdance
ID: 6345498
weird that added that twice
0
 

Author Comment

by:scotdance
ID: 6345507
weird that added that twice
0
 
LVL 6

Accepted Solution

by:
zebada earned 300 total points
ID: 6346896
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6346965
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
 
LVL 6

Expert Comment

by:zebada
ID: 6348275
Hi DragonSlayer,

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

Cheers
Paul
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6348468
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6348489
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
 

Author Comment

by:scotdance
ID: 6473783
Thanks for your help chaps, sorry about the delay in getting back to you all.
0
 

Author Comment

by:scotdance
ID: 6473800
Another problem with some more converions from C to Delphi 5

The question id was qid=20181425

Cheers
0
 

Author Comment

by:scotdance
ID: 6473802

Cheers for that zebada.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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