Solved

'C' to 'Delphi 5' conversion

Posted on 2001-08-02
14
237 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
  • 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
 

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 75 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
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.

 
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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

14 Experts available now in Live!

Get 1:1 Help Now