x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 258

# '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
• 7
• 5
• 2
1 Solution

Commented:
umm...

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

Commented:
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 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;

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

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;
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 Commented:
0

Author Commented:
0

Commented:
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;
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;
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;
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

Commented:
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

Commented:
Hi DragonSlayer,

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

Cheers
Paul
0

Commented:
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;
begin
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

Commented:
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 Commented:
Thanks for your help chaps, sorry about the delay in getting back to you all.
0

Author Commented:
Another problem with some more converions from C to Delphi 5

The question id was qid=20181425

Cheers
0

Author Commented:

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.