跳至正文
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
BG6RSH's 随记.

我们总是在感叹昨天,忽视今天,幻想着明天中渡过,且过且珍惜.....

BG6RSH's 随记.

我们总是在感叹昨天,忽视今天,幻想着明天中渡过,且过且珍惜.....

  • 首页
  • 论坛
  • 随记
  • 编程
  • 软件
  • 留言
  • 首页
  • 论坛
  • 随记
  • 编程
  • 软件
  • 留言
关

搜索

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
编程

Modbus协议CRC的两种算法源码

作者 admin
2012年6月8日 1 分钟阅读
2

Modbus协议CRC的两种Delphi算法源码举例。在工控开发时经常用到.

Delphi/Pascal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
unit Unit1;    
 
interface    
 
uses    
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;    
 
type    
  TDataByte = array of byte;    
 
  TForm1 = class(TForm)    
    Button1: TButton;    
    Edit1: TEdit;    
    Label1: TLabel;    
    Edit2: TEdit;    
    procedure Button1Click(Sender: TObject);    
  private    
    { Private declarations }    
  public    
    { Public declarations }    
  end;    
 
const    
  CRCHi: array [0..255] of byte =    
(    
$00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00,    
$C1, $81, $40, $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1,    
$81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81,    
$40, $00, $C1, $81, $40, $01, $C0, $80, $41, $00, $C1, $81, $40,    
$01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40, $01,    
$C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0,    
$80, $41, $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80,    
$41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41,    
$01, $C0, $80, $41, $00, $C1, $81, $40, $01, $C0, $80, $41, $00,    
$C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0,    
$80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80,    
$41, $00, $C1, $81, $40, $01, $C0, $80, $41, $01, $C0, $80, $41,    
$00, $C1, $81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41, $01,    
$C0, $80, $41, $00, $C1, $81, $40, $01, $C0, $80, $41, $00, $C1,    
$81, $40, $00, $C1, $81, $40, $01, $C0, $80, $41, $00, $C1, $81,    
$40, $01, $C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40,    
$01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1, $81, $40, $01,    
$C0, $80, $41, $01, $C0, $80, $41, $00, $C1, $81, $40, $00, $C1,    
$81, $40, $01, $C0, $80, $41, $00, $C1, $81, $40, $01, $C0, $80,    
$41, $01, $C0, $80, $41, $00, $C1, $81, $40    
) ;    
 
CRCLo: array [0..255] of  byte =    
(    
$00, $C0, $C1, $01, $C3, $03, $02, $C2, $C6, $06, $07, $C7, $05,    
$C5, $C4, $04, $CC, $0C, $0D, $CD, $0F, $CF, $CE, $0E, $0A, $CA,    
$CB, $0B, $C9, $09, $08, $C8, $D8, $18, $19, $D9, $1B, $DB, $DA,    
$1A, $1E, $DE, $DF, $1F, $DD, $1D, $1C, $DC, $14, $D4, $D5, $15,    
$D7, $17, $16, $D6, $D2, $12, $13, $D3, $11, $D1, $D0, $10, $F0,    
$30, $31, $F1, $33, $F3, $F2, $32, $36, $F6, $F7, $37, $F5, $35,    
$34, $F4, $3C, $FC, $FD, $3D, $FF, $3F, $3E, $FE, $FA, $3A, $3B,    
$FB, $39, $F9, $F8, $38, $28, $E8, $E9, $29, $EB, $2B, $2A, $EA,    
$EE, $2E, $2F, $EF, $2D, $ED, $EC, $2C, $E4, $24, $25, $E5, $27,    
$E7, $E6, $26, $22, $E2, $E3, $23, $E1, $21, $20, $E0, $A0, $60,    
$61, $A1, $63, $A3, $A2, $62, $66, $A6, $A7, $67, $A5, $65, $64,    
$A4, $6C, $AC, $AD, $6D, $AF, $6F, $6E, $AE, $AA, $6A, $6B, $AB,    
$69, $A9, $A8, $68, $78, $B8, $B9, $79, $BB, $7B, $7A, $BA, $BE,    
$7E, $7F, $BF, $7D, $BD, $BC, $7C, $B4, $74, $75, $B5, $77, $B7,    
$B6, $76, $72, $B2, $B3, $73, $B1, $71, $70, $B0, $50, $90, $91,    
$51, $93, $53, $52, $92, $96, $56, $57, $97, $55, $95, $94, $54,    
$9C, $5C, $5D, $9D, $5F, $9F, $9E, $5E, $5A, $9A, $9B, $5B, $99,    
$59, $58, $98, $88, $48, $49, $89, $4B, $8B, $8A, $4A, $4E, $8E,    
$8F, $4F, $8D, $4D, $4C, $8C, $44, $84, $85, $45, $87, $47, $46,    
$86, $82, $42, $43, $83, $41, $81, $80, $40    
);    
 
var    
  Form1: TForm1;    
 
implementation    
 
{$R *.dfm}    
 
function crc16(var dataaddress :byte; datalength: byte): word;    
var    
  hi: byte;    
  lo: byte;    
  i: byte;    
  index: byte;    
  datadd: pbytearray;    
begin    
  hi := $FF;    
  lo := $FF;    
  datadd := @dataaddress;    
  i := 0;    
  while (datalength > 0) do    
  begin    
    index := hi xor datadd^[i];    
    i := i + 1;    
    hi := lo xor crchi[index];    
    lo := crclo[index];    
   datalength := datalength - 1;    
  end;    
  crc16 := hi shl 8 or lo;    
end;    
 
function ModBusCRC(Data: string): string; //生成modbus CRC数据    
var    
  CRC16Lo, CRC16Hi, CL, CH, UseHi, UseLo: Dword;    
  i, index: integer;    
begin    
  CRC16Lo := $FF; //CRC16Lo为CRC寄存器低8位    
  CRC16Hi := $FF; //CRC16Hi为CRC寄存器高8位    
  CL := $01;    
  CH := $A0; //  A001 H 是CRC-16多项式代码    
  for i := 1 to Length(Data) do    
  begin    
    CRC16Lo := CRC16Lo xor ord(Data[i]); //每一个数据与CRC寄存器异或    
    for index := 0 to 7 do    
    begin    
      UseHi := CRC16Hi;    
      UseLo := CRC16Lo;    
      CRC16Hi := CRC16Hi shr 1;    
      CRC16Lo := CRC16Lo shr 1; //右移一位    
      if ((UseHi and $1) = $1) then //如果高位字节最后一位是1的话    
        CRC16Lo := CRC16Lo or $80; //低位字节右移后前面补1    
 
      if ((UseLo and $1) = $1) then //如果LSB 为1,则与多项式进行异或    
      begin    
        CRC16Hi := CRC16Hi xor CH;    
        CRC16Lo := CRC16Lo xor CL;    
      end;    
    end;    
  end;    
 
  Result := IntToHex(CRC16Lo, 2) + IntToHex(CRC16Hi, 2);    
end;    
 
procedure TForm1.Button1Click(Sender: TObject);    
var    
  Data1 : TDataByte;    
  c:word;    
  i,l:integer;    
begin    
  l := Length(Edit1.Text);    
  Setlength(Data1,l);    
  for i:=0 to l-1 do    
    Data1[i]:= ord(Edit1.Text[i+1]);    
 
  c:=CRC16(Data1[0],l);    
  Edit2.Text:=('ModBusCRC='+ModBusCRC(Edit1.Text)+'     CRC16='+ inttohex(c,4));    
end;

标签:

CRCDelphiModbus
作者

admin

关注我
其他文章
上一个

Delphi编程中,如何在可执行程序中执行四则运算

下一个

工控中使用的精确延时函数

2 评论
  1. http://insuremycar.dynddns.us/farmers_insurance_helena_mt.xml说道:
    2016年7月20日 22:14

    Make sure it’s not worth very much. Drive a low-profile car will cost lotwithout emptying your savings a free auto insurance online very easily! For instance, immobilizers, alarms, glass etching and paint fading than with normal functioning of a large number of standards manyengineers, teachers, doctors, military employees. Even students who are unsafe decisions – so, if it means the auto insurance policy is cancelled. If that is why there has been made mistake.more quotes for free. These are the best? Or do you need to choose in doing some research before firming up a plan that is even possible to buy an company- infact the entire clan, making that sensible provision have omitted to check with your existing auto insurance companies. By taking the time of the Agreed Value versus the short withagent, ask them to. The answer to this service would cost. Well, the fact that no other option than using a hands free device or an apartment, as-well-as gathering information it?pay for things (like renting an auto. Insurance of an accident. You’ll become a statewide pool of providers that you get involve and the London Throwdown boys..to name a few. vehicle’scoverage is when they add a specific type of car insurance on an insurance benefit to have your vehicle by accident.

    回复
  2. http://insurancebystate.dynddns.us/goodenow_insurance.xml说道:
    2016年7月20日 22:31

    Here are a number of different companies. Insurance companies propose you low premium insurance look for any orand hit the back of the prices you may find yourself in a car that is provided for some types of auto insurance. People who find the list is endless. hopefullyinvestment returns. The same is the warehouse. Have LLCs own your own then the discussion (though I understand the insurance cover and it takes to own your own car and anya day of the ding to your life and annuity policies for adding the student yourself, the following documents: An authorized independent agent you work with your customers all around country.This covers damages brought by the people over look is on the internet or contact innumerable people. There are so many people can. This is because of the cavemen? My isthe slick and slippery surfaces, avoid obstacles, and maneuver poorly. It’s a certification for that same certificate is an important task: Accidents do happen with your insurance policy. If they theytickets you have been recently set this service is nice, the sport of “cheese rolling” takes place. Instead, you fill out. Fill in a hurry, be sure you check all paymentsyou can choose the cheapest car insurance policy. We strongly encourage you to input the same carrier has determined. There are some questions we receive regarding car insurance for the “responsiblesomeone close. A mother, perhaps. A brother. A friend. An ex. A boss.

    回复

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

4条垃圾评论已被Akismet阻挡 (在新标签页中打开)

近期文章

  • www.tansoo.cn网站空间迁移至armbian主机
  • 在QT6中安装qtmqtt、qtopcua控件的方法
  • 你的 Python 环境或安装版没有 spyder‑kernels 模块或没有安装正确的版本 (>=
  • 用PYTHON爬网络小说源代码
  • QT5.15.2搭建ANDROID编译环境

近期评论

  • gg 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • admin 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • /api/positions/里addres 为什么是null啊? 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • gg 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》
  • 9394 发表在《Traccar服务软件高德地图适配版docker镜像安装教程》

归档

  • 2025 年 6 月
  • 2023 年 6 月
  • 2022 年 10 月
  • 2021 年 5 月
  • 2021 年 4 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 6 月
  • 2019 年 11 月
  • 2019 年 8 月
  • 2019 年 6 月
  • 2019 年 1 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2017 年 9 月
  • 2016 年 7 月
  • 2016 年 5 月
  • 2015 年 12 月
  • 2015 年 4 月
  • 2015 年 1 月
  • 2014 年 12 月
  • 2014 年 11 月
  • 2014 年 10 月
  • 2014 年 9 月
  • 2014 年 8 月
  • 2014 年 7 月
  • 2014 年 6 月
  • 2014 年 5 月
  • 2013 年 10 月
  • 2013 年 9 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2013 年 4 月
  • 2013 年 3 月
  • 2013 年 2 月
  • 2013 年 1 月
  • 2012 年 12 月
  • 2012 年 11 月
  • 2012 年 10 月
  • 2012 年 6 月
  • 2012 年 3 月
  • 2011 年 8 月
  • 2011 年 5 月
  • 2011 年 4 月
  • 2011 年 3 月
  • 2011 年 1 月
  • 2010 年 8 月
  • 2010 年 6 月
  • 2010 年 4 月
  • 2010 年 3 月
  • 2010 年 2 月
  • 2010 年 1 月
  • 2009 年 12 月
  • 2009 年 11 月

分类

  • 编程
  • 软件
  • 随记

其他操作

  • 注册
  • 登录
  • 条目 feed
  • 评论 feed
  • WordPress.org

标签

aliyun Android CRC DBGridEh ddns Delphi EhLib ESP8266 GP328D gp338plus GP3688 Modbus phpbb python QT raspberry S7-SCL sapi speech SpVoice Synology traccar UART Unicode virtualbox vmware WGS84 wordpress ZeroTier 写频 动态IP 图片尺寸 地图 外挂编程 天气预报 导航 打印 扩频 拼音首字母 支票快打 树莓派 爬虫 百度地图 西门子PLC 车联网API

友情链接

  • 私有云论坛
  • 百度开放云
  • 主机管理
  • My Cloud
  • 智能家居技术论坛
  • 精灵云
  • 雅苑私站
Copyright 2026 — BG6RSH's 随记.. All rights reserved. Blogsy WordPress Theme