Fórum Cálculo de CRC #335534
02/01/2007
0
Ariovaldo
Curtir tópico
+ 0Posts
03/01/2007
Ariovaldo
Gostei + 0
04/01/2007
Ariovaldo
Gostei + 0
05/01/2007
Edjlle
experimente isto:
// Função que calcula CRC
function ByteCrc(data: integer; crc: integer): integer;
var
i: integer;
begin
for i := 0 to 7 do
begin
if ((data and $01) xor (crc and $0001) <> 0) then
begin
crc := crc shr 1;
crc := crc xor $A001;
end
else crc := crc shr 1;
data := data shr 1; // Esta linha sempre será executada.
end;
ByteCrc := crc;
end;
{ Exemplo de chamada }
// Função que envia frame de um comando em ModBus
// retorna true se tiver sucesso
function EnviaFrame(Endereco, Comando, RegIn, NumRegs: Word): boolean;
var
x, crc: integer;
Str: string;
begin
Str := Chr(Endereco) + Chr(Comando);
Str := Str + Chr(RegIn shr 8);
Str := Str + Chr(RegIn and 255);
Str := Str + Chr(NumRegs shr 8);
Str := Str + Chr(NumRegs and 255);
crc := ByteCrc(Endereco, 65535);
crc := ByteCrc(Comando, crc);
crc := ByteCrc(0, crc);
crc := ByteCrc(RegIn, crc);
crc := ByteCrc((NumRegs shr 8), crc);
crc := ByteCrc((NumRegs and 255), crc);
Str := Str + chr(crc and 255);
Str := Str + chr(crc shr 8);
try
begin
for x := 1 to 8 do PortaCom.TransmitChar(Str[x]);
result := true;
end;
except
result := false;
end;
end;
Gostei + 0
05/01/2007
Edjlle
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)