Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   CRC von C in Delphi übersetze (https://www.delphipraxis.net/212736-crc-von-c-delphi-uebersetze.html)

ZOD 23. Mär 2023 14:58

CRC von C in Delphi übersetze
 
Hallo zusammen,

ich kämpfe schon seit einiger Zeit mit der Aufgabe, eine gegebene C-Funktion ins Delphi zu übersetzen und brauche hierbei leider Hilfe.

Kurz der Hintergrund:
die Kommunikation mit einem Gerät erfolgt über eine Serielle Schnittstelle.
Dabei werden Kommandos an das Gerät übertragen, dass dann entsprechend antwortet.
Grundsätzlicher Aufbau der Nachrichten ist:

<Command><CRC><cr>

Im Prinzip nichts schwieriges. Der Teufel steckt im CRC, das mit jedem Kommando gesendet werden muss.
Ist der CRC (2 Byte cuclic redundancy check code) falsch, wird der gesendete Befehl zwar empfangen - die Ausführung jedoch verweigert.

Mit der Gerätedoku habe ich eine CRC Routine in C bekommen:


Code:
INT16U cal_crc_half(INT8U far *pin, INT8U len)
{
INT16U crc;
INT8U da;
INT8U far *ptr;
INT8U bCRCHign;
INT8U bCRCLow;
INT16U crc_ta[16]=
{ /* CRC look up table */
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
};
ptr=pin;
crc=0;
len--;
while(len--!=0)
{
da=((INT8U)(crc>>8))>>4; /* CRC high four bits */
crc<<=4; /* The CRC is shifted to the right by 4 bits, which is equivalent to taking the lower 12
bits of the CRC. */
crc^=crc_ta[da^(*ptr>>4)]; /* Add the upper 4 bits of the CRC and the first half of the byte and
look up the table to calculate the
CRC, then add the remainder of the last CRC. */
da=((INT8U)(crc>>8))>>4; /* CRC high four bits */
crc<<=4; /* The CRC is shifted to the right by 4 bits, which is equivalent to taking the lower 12
bits of the CRC. */
crc^=crc_ta[da^(*ptr&0x0f)]; /* Add the upper 4 bits of the CRC and the last half of the byte and
look up the table to calculate the
CRC, then add the remainder of the last CRC. */
ptr++;
}
bCRCLow = crc;
bCRCHign= (INT8U)(crc>>8);
if(bCRCLow==0x28||bCRCLow==0x0d||bCRCLow==0x0a)
{
bCRCLow++;
}
if(bCRCHign==0x28||bCRCHign==0x0d||bCRCHign==0x0a)
{
bCRCHign++;
}
crc = ((INT16U)bCRCHign)<<8;
crc += bCRCLow;
return(crc);
}
Sinn der Funktion al_crc_half sollte sein, dass für einen übergebenen
Delphi-Quellcode:
string
ein 2-Byte CRC Wert (
Delphi-Quellcode:
word
) nach obiger Vorgabe berechnet wird.

Das ist naturgemäss für CRC viel Bitschieberei. Soweit ich erkennen konnte, scheint das ein proprietärer CRC Prozess zu sein.

Leider bin ich nahezu unerfahren, was C angeht und auch sehr ungeübt, was Bitoperationen in Delphi angeht.

Daher die Frage:
kann mir jemand das wenigstens ungefähr in Delphi übersetzen?

Anmerkung:
ich habe nicht vor auf diesem Wege Arbeit durch copy-paste programmieren zu sparen.
Leider bin ich nach einigen Stunden noch am Anfang der Aufgabe - daher die Bitte um Unterstützung.

Danke für alle Hinweise und Unterstützung.

Markus

taveuni 23. Mär 2023 15:10

AW: CRC von C in Delphi übersetze
 
Ich habe mir den Spass erlaubt den Bing Bot in Skype zu fragen. Er sagt Sure, here is the Delphi version of the code:

Delphi-Quellcode:
function cal_crc_half(pin: PByte; len: Byte): Word;
var
  crc: Word;
  da: Byte;
  ptr: PByte;
  bCRCHign: Byte;
  bCRCLow: Byte;
  crc_ta: array[0..15] of Word = ($0000, $1021, $2042, $3063, $4084, $50a5, $60c6, $70e7,
                                  $8108, $9129, $a14a, $b16b, $c18c, $d1ad, $e1ce, $f1ef);
begin
  ptr := pin;
  crc := 0;
  len := len - 1;
  while (len &lt;&gt; 0) do
  begin
    da := (crc shr 8) shr 4; // CRC high four bits
    crc := crc shl 4; // The CRC is shifted to the right by 4 bits, which is equivalent to taking the lower 12 bits of the CRC.
    crc := crc xor crc_ta[da xor (ptr^ shr 4)]; // Add the upper 4 bits of the CRC and the first half of the byte and look up the table to calculate the CRC, then add the remainder of the last CRC.
   da := (crc shr 8) shr 4; // CRC high four bits
   crc := crc shl 4; // The CRC is shifted to the right by 4 bits, which is equivalent to taking the lower 12 bits of the CRC.
   crc := crc xor crc_ta[da xor (ptr^ and $0f)]; // Add the upper 4 bits of the CRC and the last half of the byte and look up the table to calculate the CRC, then add the remainder of the last CRC.
   Inc(ptr);
   Dec(len);
  end;
  bCRCLow := crc and $FF;
  bCRCHign := crc shr 8;
  if (bCRCLow = $28) or (bCRCLow = $0d) or (bCRCLow = $0a) then
   Inc(bCRCLow);
  if (bCRCHign = $28) or (bCRCHign = $0d) or (bCRCHign = $0a) then
   Inc(bCRCHign);
  crc := (bCRCHign shl 8) + bCRCLow;
  Result := crc;
end;
Am Schluss noch: I hope this helps! Let me know if you have any other questions.

ZOD 23. Mär 2023 15:14

AW: CRC von C in Delphi übersetze
 
Das teste ich gleich!

Warum ich darauf selbst nicht gekommen bin ... Untercoffeinnebel wahrscheinlich.

Vielen DANK!

Stevie 23. Mär 2023 15:55

AW: CRC von C in Delphi übersetze
 
Putzig, dass er sogar die falschen Sourcecode Kommentare übernommen hat (<< ist left shift und kein right shift, wie im Kommentar steht)

ZOD 23. Mär 2023 16:00

AW: CRC von C in Delphi übersetze
 
Ja, faszinierend.

Vor allem, weil einen diese Kommentare beim "Selbstversuch" erst einmal in die Denkschleife schicken .... :oops:

sh17 24. Mär 2023 07:35

AW: CRC von C in Delphi übersetze
 
Also manchmal ist ChatGPT schon zu gebrauchen, finde ich. Hab letztens eine Funktion optimiert und hab der KI gesagt, sie solle sie mal umschreiben, ohne die Nutzung von PChar und Pointern. Hat er/sie/es fix erledigt. Hätte mich ein paar Minuten mehr gekostet. Kontrolle des Codes geht dann durch mich schneller.

ZOD 24. Mär 2023 11:07

AW: CRC von C in Delphi übersetze
 
eine erste Rückmeldung und Frage zur CRC Funktion:

das übergebene Funktionsargument
Delphi-Quellcode:
pin
ist nach meinem Verständnis ein Byte.
Es wird im Folgenden als Pointer auf das erste Zeichen der zu behandelnden Zeichenkette mit Länge
Delphi-Quellcode:
len
(2. Argument) verwendet.
Ich habe dies gefunden:
http://delphibasics.co.uk/RTL.php?Name=PAnsiChar

Um die CRC Funktion aufzurufen, verwende ich zum Test folgende Funktion:

Delphi-Quellcode:
function ErzeugeSolarisCRCWord(Text: string): word;
var
  tmpAnsiString: AnsiString;

  tmpPAnsiString: PByte;
  tmpPAnsiChar: PAnsiChar;

  tmpLength: Byte;

  tmpTextLength,
    i : integer;

begin
  // initialisieren
  result := 0;
  tmpTextLength := Length(Text);

  // validieren
  if ((tmpTextLength = 0) or (tmpTextLength > 256)) then
    exit;

  // Merker setzen
  tmpAnsiString := Text;
  i := 1;
  tmpPAnsiString := Addr(tmpAnsiString);
  tmpPAnsiChar := Addr(tmpAnsiString[i]);
  tmpLength := Length(tmpAnsiString);

  // CRC berechnen
  // function cal_crc_half(pin: PByte; len: Byte): Word;
  result := cal_crc_half(Addr(tmpPAnsiChar), tmpLength);

end;
Lasse ich das mit Text = 'QPIGS' laufen und setzte einen Haltepunkt vor dem Aufruf von cal_crc_half, habe ich folgende Werte:

Delphi-Quellcode:
  tmpPAnsiString = $12FBD8
  tmpPAnsiChar = 'QPIGS'
  tmpLength := 5

  Addr(tmpPAnsiChar) = $12FBD0
In der Funkton cal_crc_half ist nach der Zuweisung
Delphi-Quellcode:
ptr := pin;
der Wert von
Delphi-Quellcode:
  ptr = $12FBD0
  ptr^ = 12
Meinem bisherigen Verständnis nach sollte jedoch der derefernierte Wert von
Delphi-Quellcode:
ptr
ASCII-Wert des ersten Buchstaben von
Delphi-Quellcode:
Text
bzw. von
Delphi-Quellcode:
tmpPAnsiChar
sein - also "Q = ASCII 81".
Delphi-Quellcode:
ptr^
liefert jedoch 12.

Ich verstehe das nicht, wahrscheinlich weil ich das Thema Pointer, AnsiChar und AnsiString nicht durchblickt habe.

Kann mir jemand helfen?

sh17 24. Mär 2023 11:37

AW: CRC von C in Delphi übersetze
 
tmpPAnsiChar ist doch schon ein Pointer, Du übergibts also, denke ich, die Adresse auf die Adresse Addr(tmpPAnsiChar)

ZOD 24. Mär 2023 11:50

AW: CRC von C in Delphi übersetze
 
mhm - das ist sicher richtig. Jedoch bekomme ich ohne
Delphi-Quellcode:
Addr()
diese Fehlermeldung beim Kompilieren:

[Fehler] uCRCSolaris.pas(174): Inkompatible Typen: 'Char' und 'Byte'

Wie gesagt, ich bin beim Thema Pointer und Adressen sehr unerfahren.

ZOD 24. Mär 2023 12:19

AW: CRC von C in Delphi übersetze
 
weiter probiert und mit
Delphi-Quellcode:
PByte()
kann ich ohne Fehler kompilieren.
So sieht die Aufruffunktion jetzt aus:

Delphi-Quellcode:
function ErzeugeSolarisCRCWord(Text: string): word;
var
  tmpAnsiString: AnsiString;

  tmpPAnsiCharByte,
  tmpPAnsiString: PByte;
  tmpPAnsiChar: PAnsiChar;

  tmpLength: Byte;

  tmpTextLength,
    i : integer;

begin
  // initialisieren
  result := 0;
  tmpTextLength := Length(Text);

  // validieren
  if ((tmpTextLength = 0) or (tmpTextLength > 256)) then
    exit;

  // Merker setzen
  tmpAnsiString := Erzeuge8BitCharString(Text);
  i := 1;
  tmpPAnsiString := Addr(tmpAnsiString);
  tmpPAnsiChar := Addr(tmpAnsiString[i]);
  tmpLength := Length(tmpAnsiString);

  tmpPAnsiCharByte := PByte(tmpPAnsiChar);

  // CRC berechnen
  // function cal_crc_half(pin: PByte; len: Byte): Word;
  result := cal_crc_half(tmpPAnsiCharByte, tmpLength);

end;
Beim Test sind jetzt in
Delphi-Quellcode:
function cal_crc_half(pin: PByte; len: Byte): Word;
folgende Werte abrufbar (<STRG>+<F7>):

Delphi-Quellcode:
  ptr = $B7980C
  ptr^ = 81
  Char(ptr^) = 'Q'
Ok, soweit läuft das also.

Leider bin ich nun zurück bei meinem Ausgangsproblem - der CRC Berechnung.

Ich erhalte als CRC Wert $D2$36

Laut der Doku der CRC-C-Routine sollte jedoch der CRC für "QPIGS" den Wert $B7$A9 lauten.

Also ist entweder

A - die "Übersetzung C->Delphi" nicht korrekt
B - die Doku falsch
C - oder ich mache noch einen völlig anderen Fehler

Tipps und Hinweise sind ausdrücklich willkommen!


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:35 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz