Board index » delphi » Oh, say, can you C?

Oh, say, can you C?

I really hate doing this, but is there anyone out there willing to
translate a {*word*249}y bit of C to Pascal for me?  I know just enough C to
know that I have no idea what they were trying to do, other than
taking a line of text, and sending it out as a packet with start,
stop, bus address, and checksum bytes.  If I translate it, it
probably won't work.

/*PREVIOUS DECLARATIONS which I don't need translated, but
/*you might need to translate the function...*/

short int STX = 0x02;
short int ETX = 0x03;
short int Byt = 0x01;
short int test1=0x45;
short int test2=0x4d;
short int test3=0x20;
short int test4=0x3f;
short int addr = 0x21;
short int count= 0x0A;
short int        Port;
unsigned char
b1[2],b2[2],b3[2],b4[2],b5[2],b6[2],b7[2],b8[2],b9[2],b10[2],b11[2],b
2[2],b13[2],b14[2],b15[2],b16[
2],b17[2];
char inputstr[12];        /*11 characters max in outgoing command*/
int        speed    = 9600;
int        parity   = ODD_PARITY;
int        bits     = 7;
int        stopbits = 1;
#define FALSE           0
#define TRUE            (!FALSE)
#define NOERROR         0       /* No error               */
#define BUFOVFL         1       /* Buffer overflowed      */
#define ESC             0x1B    /* ASCII Escape character */
#define ASCII           0x007F  /* Mask ASCII characters  */
#define SBUFSIZ         0x4000  /* Serial buffer size     */
int            SError          = NOERROR;
int            portbase        = 0;
void           interrupt(*oldvects[2])();
static   char  ccbuf[SBUFSIZ];
unsigned int   startbuf        = 0;
unsigned int   endbuf          = 0;

/*THE CODE I NEED PASCALIZED FOLLOWS*/

void packetparse(char string[18])
{
int i,chksum=0;
int length,count;
unsigned t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;
length=strlen(string);
count=length+6;
sscanf(string,"%c%c%c%c%c%c%c%c%c%c%c",b5,b6,b7,b8,b9,b10,b11,b12,b13
b14,b15);

t1=(char)*b5;
t2=(char)*b6;
t3=(char)*b7;
t4=(char)*b8;
t5=(char)*b9;
t6=(char)*b10;
t7=(char)*b11;
t8=(char)*b12;
t9=(char)*b13;
t10=(char)*b14;
t11=(char)*b15;

/* calculate checksum*/
chksum=addr+Byt;

for (i=1;i < length+1;i++)
{
switch(i)
{
case 1:chksum=chksum+t1; break;
case 2:chksum=chksum+t2; break;
case 3:chksum=chksum+t3; break;
case 4:chksum=chksum+t4; break;
case 5:chksum=chksum+t5; break;
case 6:chksum=chksum+t6; break;
case 7:chksum=chksum+t7; break;
case 8:chksum=chksum+t8; break;
case 9:chksum=chksum+t9; break;
case 10:chksum=chksum+t10; break;
case 11:chksum=chksum+t11;

Quote
}
}

chksum=chksum & 0X7F;

sprintf(b1,"%c",STX);
sprintf(b2,"%c",count);
sprintf(b3,"%c",addr);
sprintf(b4,"%c",Byt);

i=length+5;
switch(i)
{
case 5:sprintf(b5,"%c",chksum);sprintf(b6,"%c",ETX); break;
case 6:sprintf(b6,"%c",chksum);sprintf(b7,"%c",ETX); break;
case 7:sprintf(b7,"%c",chksum);sprintf(b8,"%c",ETX); break;
case 8:sprintf(b8,"%c",chksum);sprintf(b9,"%c",ETX); break;
case 9:sprintf(b9,"%c",chksum);sprintf(b10,"%c",ETX); break;
case 10:sprintf(b10,"%c",chksum);sprintf(b11,"%c",ETX); break;
case 11:sprintf(b11,"%c",chksum);sprintf(b12,"%c",ETX); break;
case 12:sprintf(b12,"%c",chksum);sprintf(b13,"%c",ETX); break;
case 13:sprintf(b13,"%c",chksum);sprintf(b14,"%c",ETX); break;
case 14:sprintf(b14,"%c",chksum);sprintf(b15,"%c",ETX); break;
case 15:sprintf(b15,"%c",chksum);sprintf(b16,"%c",ETX); break;
case 16:sprintf(b16,"%c",chksum);sprintf(b17,"%c",ETX);

Quote
}

/*Display Output string*/

fprintf(stdout,"%s","\n");
fprintf(stdout,"%s","Command Sent    = ");
for (i=1;i<length+7;i++)
{
switch(i)
{
case 1:fprintf(stdout,"%X%s",*b1," "); break;
case 2:fprintf(stdout,"%X%s",*b2," "); break;
case 3:fprintf(stdout,"%X%s",*b3," "); break;
case 4:fprintf(stdout,"%X%s",*b4," "); break;
case 5:fprintf(stdout,"%X%s",*b5," "); break;
case 6:fprintf(stdout,"%X%s",*b6," "); break;
case 7:fprintf(stdout,"%X%s",*b7," "); break;
case 8:fprintf(stdout,"%X%s",*b8," "); break;
case 9:fprintf(stdout,"%X%s",*b9," "); break;
case 10:fprintf(stdout,"%X%s",*b10," "); break;
case 11:fprintf(stdout,"%X%s",*b12," "); break;
case 12:fprintf(stdout,"%X%s",*b12," "); break;
case 13:fprintf(stdout,"%X%s",*b13," "); break;
case 14:fprintf(stdout,"%X%s",*b14," "); break;
case 15:fprintf(stdout,"%X%s",*b15," "); break;
case 16:fprintf(stdout,"%X%s",*b16," "); break;
case 17:fprintf(stdout,"%X%s",*b17," ");

Quote
}
}

fprintf(stdout,"%s","\n");

/*Transmit Output Test string*/
for (i=1;i<length+7;i++)
{
switch(i)
{
case 1:SerialOut(*b1); break;
case 2:SerialOut(*b2); break;
case 3:SerialOut(*b3); break;
case 4:SerialOut(*b4); break;
case 5:SerialOut(*b5); break;
case 6:SerialOut(*b6); break;
case 7:SerialOut(*b7); break;
case 8:SerialOut(*b8); break;
case 9:SerialOut(*b9); break;
case 10:SerialOut(*b10); break;
case 11:SerialOut(*b11); break;
case 12:SerialOut(*b12); break;
case 13:SerialOut(*b13); break;
case 14:SerialOut(*b14); break;
case 15:SerialOut(*b15); break;
case 16:SerialOut(*b16); break;
case 17:SerialOut(*b17); break;

Quote
}
}
}

I have a vague idea of what they are doing, but it looks to me like
they did it the hard way...which may only point out my naivete
vis-a-vis C.

Any help will be most appreciated.

--
BCNU
Daniel J. Wojcik
http://147.35.241.5/mindless/mindless.htm
--
Eenie, meenie, miney, wiggle...it's Mr Wiggles the Worm!

 

Re:Oh, say, can you C?


Quote
woj...@satcom.kaiserslautern.army.mil wrote:

> I really hate doing this, but is there anyone out there willing to
> translate a {*word*249}y bit of C to Pascal for me?  I know just enough C to
> know that I have no idea what they were trying to do, other than
> taking a line of text, and sending it out as a packet with start,
> stop, bus address, and checksum bytes.  If I translate it, it
> probably won't work.

> [... C code cut out ...]

> I have a vague idea of what they are doing, but it looks to me like
> they did it the hard way...which may only point out my naivete
> vis-a-vis C.

> Any help will be most appreciated.

> --
> BCNU
> Daniel J. Wojcik
> http://147.35.241.5/mindless/mindless.htm
> --
> Eenie, meenie, miney, wiggle...it's Mr Wiggles the Worm!

You are right about what the code is doing: reading a string from the user and
constructing a packet of it, consisting of a header, the string itself, a
checksum and a tail. But it is *very* bad programming. A simple pascal version
might be the following:

VAR
  input_str : STRING[11];                                       { input string }
  len       : BYTE ABSOLUTE input_string;                       { string length

Quote
}

  input_num : ARRAY[0..11] OF BYTE ABSOLUTE input_string;       { see string as
bytes }
  packet    : ARRAY[1..16] OF BYTE;                             { packet buffer
Quote
}

  j         : BYTE;                                             { loop counter }
  chksum    : BYTE;                                             { check sum }

BEGIN
  readln( input_str );
  chksum:=addr+byt;                                     { initialize check sum }
  FOR j:=1 TO len DO INC( chksum, input_num[j] );       { build check sum }
  chksum:=chksum AND $7F;                               { clear high order bit }
  packet[1]:=STX;
  packet[2]:=count;
  packet[3]:=addr;
  packet[4]:=byt;                                       { packet header
constructed }
  FOR j:=1 to len DO packet[4+j]:=input_num[j];         { move string to packet

Quote
}

  packet[4+len+1]:=chksum;                              { append check sum }
  packet[4+len+2]:=ETX;                                 { append packet tail }
  WRITELN;
  WRITE('Command sent = ');
  FOR j:=1 TO len+6 DO WRITE( hex(packet[j]), ' ' );    { display packet }
  WRITELN;
  FOR j:=1 TO len+6 DO SerialOut( packet[j] );          { transmit packet }
END.

That's all! The C code is really poor and ineffective code, mainly because it
simulates(!!!!!!) array indexing through a combination of FOR and SWITCH (as we
say CASE) to address 16 different variables, which just ought to be elements of
an array.

Of course, to make it work you will need a HEX function to convert a number to a
hexadecimal string (e.g. HEX(46)='2E') and a SERIALOUT procedure, to send a byte
through a serial port.  

I also have a remark. Shouldn't the COUNT byte at the packet header contain the
actual length of the packet? I don't know, I just feel that since the packet
size is variable, its actual length sould be included somewhere. Maybe you can
check the documentation.

I hope I have helped.

Votis

------------------------------------------

Votis Kokavessis
Mathematics teacher & pascal programmer
Thessaloniki, Greece
E-mail address: paratiritis.the.forthnet...@popper.forthnet.gr (votis)

Other Threads