Author Topic: RTU->Delphi->CRC  (Read 5975 times)

tobiasz0606

  • Guest
RTU->Delphi->CRC
« on: April 18, 2007, 08:16:53 AM »
I have to connect with SZH driver. I\'ve got Delphi and try to work with modbus protocol (RTU transmission system). I use Windows API and TDBC class.

My problem is:

How can I check CRC in my frame?

asokulski

  • Guest
RTU->Delphi->CRC
« Reply #1 on: April 18, 2007, 06:15:39 PM »
There is the procedure for CRC.
Frame is in Data[] table.
len is the length of the frame.


procedure ModbusRead.crc16(len:integer);
var crc: word;
n,i: integer;
b:byte;
begin
crc := $FFFF;
for i:=0 to len-1 do begin
b:=Data;
crc := crc xor b;
for n:=1 to 8 do begin
if (crc and 1)<>0
then crc:=(crc shr 1) xor $A001
else crc:=crc shr 1;
end;
end;
Data[len]:=crc and $ff;
Data [len+1]:=crc shr 8;
end;

tobiasz0606

  • Guest
RTU->Delphi->CRC
« Reply #2 on: April 19, 2007, 09:17:27 AM »
const
I'm glad to see your answer, but still need your help. Until now I tried to create my  buffor in that way:
var
  query : PChar = '050330000020';
  Buffer_O : array[0..16] of Char;

              ******************
then  a handel
              ******************

lpFileName:='Com1';

hCommDev:=CreateFile(lpFileName, GENERIC_READ or GENERIC_WRITE,0,NIL,OPEN_EXISTING,0,0);

             ******************
parameters of transmision
             ******************

dcb.DCBlength:=sizeof(dcb);
dcb.BaudRate:= CBR_9600;
dcb.Parity:= NOPARITY;
dcb.StopBits:=ONESTOPBIT;
dcb.ByteSize:=8;


SetCommState(hCommDev,dcb);

             ******************
and here I put my query into the buffor
             ******************

StrCopy(Buffer_O,query);
...
WriteFile(hCommDev, Buffer_O, nNumberOfBytesToWrite,
            NumberOfBytesWritten, NIL);


My problem is:
I don't know how I can use yours procedure. Could you help me again?

tobiasz0606

  • Guest
RTU->Delphi->CRC
« Reply #3 on: April 19, 2007, 09:18:09 AM »
There is one more problem. My driver use one bit to write down 2 numbers.

tobiasz0606

  • Guest
RTU->Delphi->CRC
« Reply #4 on: April 19, 2007, 09:18:35 AM »
:-)