modbus crc16算法(delphi) 点击:2333 | 回复:0



舞天涯

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:3帖 | 24回
  • 年度积分:0
  • 历史总积分:58
  • 注册:2002年6月05日
发表于:2008-06-24 10:10:13
楼主

unit unCRC16_Modbus;

interface

function CalCRC16(AData:array of Byte; AStart,AEnd:Integer): Word;

implementation

function CalCRC16(AData:array of Byte; AStart,AEnd:Integer): Word;
const
  GENP = $A001;

var
  crc:Word;
  i:Integer;
  tmp:Byte;

procedure CalOneByte(AByte:Byte);
var
  j:Integer;
begin
  crc:=crc xor AByte;
  for j := 0 to 7 do
  begin
    tmp:=crc and 1;
    crc:=crc shr 1;
    crc:= crc and $7FFF;
    if tmp = 1 then
    crc:= crc xor GENP;
    crc:=crc and $FFFF;
  end;
end;

begin
  crc:=$FFFF;
  for i := AStart to AEnd do
  CalOneByte(AData【i】);
  Result:=crc;
end;

end.



热门招聘
相关主题

官方公众号

智造工程师