![温控器Modbus协议通讯设计样例更新_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-3/3/10c10160-76e1-456c-9f24-57c1fbc19de6/10c10160-76e1-456c-9f24-57c1fbc19de61.gif)
![温控器Modbus协议通讯设计样例更新_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-3/3/10c10160-76e1-456c-9f24-57c1fbc19de6/10c10160-76e1-456c-9f24-57c1fbc19de62.gif)
![温控器Modbus协议通讯设计样例更新_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-3/3/10c10160-76e1-456c-9f24-57c1fbc19de6/10c10160-76e1-456c-9f24-57c1fbc19de63.gif)
![温控器Modbus协议通讯设计样例更新_第4页](http://file3.renrendoc.com/fileroot_temp3/2022-3/3/10c10160-76e1-456c-9f24-57c1fbc19de6/10c10160-76e1-456c-9f24-57c1fbc19de64.gif)
![温控器Modbus协议通讯设计样例更新_第5页](http://file3.renrendoc.com/fileroot_temp3/2022-3/3/10c10160-76e1-456c-9f24-57c1fbc19de6/10c10160-76e1-456c-9f24-57c1fbc19de65.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、DELPHI SPCOMM温控器 Modbus 协议通讯设计 QQ:64782489陈建光 unit Unit1;-interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, SPComm, ExtCtrls,StrUtils;typeTForm1 = class(TFormMemo1: TMemo;Button1: TButton;Button2: TButton;Button3: TButton;ComboBox1: TComboBox;L
2、abel2: TLabel;GroupBox1: TGroupBox;Label3: TLabel;ComboBox2: TComboBox;Memo2: TMemo;GroupBox2: TGroupBox;Button4: TButton;Button5: TButton;Label5: TLabel;Button6: TButton;Label1: TLabel;Label6: TLabel;Label4: TLabel;Label7: TLabel;ComboBox3: TComboBox;ComboBox4: TComboBox;ComboBox5: TComboBox;Comm1:
3、 TComm;GroupBox3: TGroupBox;Panel1: TPanel;Panel2: TPanel;Label8: TLabel;Label9: TLabel;Label10: TLabel;Label11: TLabel;Label12: TLabel;Label13: TLabel;Label15: TLabel;Panel3: TPanel;Panel4: TPanel;Button7: TButton;Button9: TButton;Button10: TButton;Timer1: TTimer;Label14: TLabel;Panel5: TPanel;Labe
4、l16: TLabel;Edit1: TEdit;Label17: TLabel;Edit2: TEdit;Label18: TLabel;Edit3: TEdit;Label19: TLabel;Edit4: TEdit;Edit5: TEdit;Label20: TLabel;Label21: TLabel;Edit6: TEdit;Button8: TButton;Label22: TLabel;Edit7: TEdit;Button12: TButton;Edit8: TEdit;Label23: TLabel;Edit9: TEdit;Label25: TLabel;Panel6:
5、TPanel;Edit10: TEdit;Label24: TLabel;Edit11: TEdit;Label26: TLabel;Label27: TLabel;Button11: TButton;Button13: TButton;Timer2: TTimer;procedure SetComPort(;-procedure Button1Click(Sender: TObject;procedure Button2Click(Sender: TObject;procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; Buff
6、erLength: Word;procedure Button3Click(Sender: TObject;procedure FormCreate(Sender: TObject;procedure Button4Click(Sender: TObject;procedure Button5Click(Sender: TObject;procedure Button6Click(Sender: TObject;-procedure Timer1Timer(Sender: TObject;procedure Button7Click(Sender: TObject;procedure Butt
7、on8Click(Sender: TObject;procedure Button9Click(Sender: TObject;procedure Button10Click(Sender: TObject;procedure Button11Click(Sender: TObject;procedure Button13Click(Sender: TObject;procedure Timer2Timer(Sender: TObject;/function CalCRC16(AData:array of Byte;AStart,AEnd:Integer:Word;/function retu
8、rnCRC16(vStr1:String:String;private Private declarations public Public declarations end;-varForm1: TForm1;viewstring:string; /定义全局字符串rbuf,sbuf:array of byte; /定义全局可变字节数组Cur_Qry_Str: array1.32 of string; /当前查询字符串Btn_Qry_Str,Tmr_Qry_Str:array1.32 of string; /按钮、定时器返回字符串数组Btn_Rtn_Str,Tmr_Rtn_Str:array1
9、.32 of string; /按钮、定时器返回字符串数组cur_wd,Cur_wd_p,set_wd,Cur_Mode,Open_sta:array1.32 of integer; /设置当前温度、设置温度、开关状态数 组implementation$R *.dfm/=此 处 用 于 自 动 获 取 系 统 的 串 口 数 目 以 及 名 称 , 调 用 api 函 数 实 现 相 关 功 能 =procedure EnumComPorts(Ports: TStrings;varKeyHandle: HKEY;ErrCode, Index: Integer;ValueName, Data:
10、string;ValueLen, DataLen, ValueType: DWORD;TmpPorts: TStringList;beginErrCode := RegOpenKeyEx(HKEY_LOCAL_MACHINE,'HARDW AREDEVICEMAPSERIALCOMM',0,KEY_READ,KeyHandle;if ErrCode <> ERROR_SUCCESS thenExit; / raise EComPort.Create(CError_RegError, ErrCode; TmpPorts := TStringList.Create;tr
11、yIndex := 0;repeatValueLen := 256;DataLen := 256;SetLength(ValueName, ValueLen;SetLength(Data, DataLen;ErrCode := RegEnumValue(KeyHandle,Index,PChar(ValueName,Cardinal(ValueLen,nil,ValueType,PByte(PChar(Data,DataLen;if ErrCode = ERROR_SUCCESS thenbeginSetLength(Data, DataLen;TmpPorts.Add(Data;Inc(In
12、dex;endelseif ErrCode <> ERROR_NO_MORE_ITEMS thenexit; /raise EComPort.Create(CError_RegError, ErrCode; until (ErrCode <> ERROR_SUCCESS ;TmpPorts.Sort;Ports.Assign(TmpPorts;finallyRegCloseKey(KeyHandle;TmpPorts.Free;end;end;/=替换字符串中的空格函数 = function Trimplace(str: string: string;vartmp: s
13、tring;p : Integer;begintmp := Trim(str;while Pos(' ',tmp>0 dobeginp := Pos(' ',tmp;tmp := Copy(tmp,1,p-1+Copy(tmp,p+1,Length(tmp-p;end;Result := tmp;end;/字符串转成 16进制代码function strToHexStr(str:string:string;varc:char;ss:string;i:integer;beginwhile str<>'' do beginc:=st
14、r1;ss:=ss+format('%0x',ord(c;delete(str,1,1;end;strtohexStr:= ss;end;/16进制字符串转换成字符串function HexStrToStr(const S:string:string;vart:Integer;ts:string;M,Code:Integer;begint:=1;Result:=''while t<=Length(S dowhile (t<=Length(S and (not (St in '0'.'9','A'.
15、9;F','a'.'f' doinc(t;if (t+1>Length(Sor(not (St+1 in '0'.'9','A'.'F','a'.'f' thents:='$'+Stelsets:='$'+St+St+1;V al(ts,M,Code;if Code=0 thenResult:=Result+Chr(M;inc(t,2;end;end;/替换字符串函数function replacing(s,source,
16、target:string:string;var site,StrLen:integer;beginsource在 S 中出现的位置 site:=pos(source,s;source的长度 StrLen:=length(source;删除 source 字符串 delete(s,site,StrLen;插入 target 字符串到 S 中 insert(target,s,site;返回新串 replacing:=s;end;/××××××××××××××
17、××××××××××××/ CalCRC16用于计算 Modbus RTU的 CRC16/ 多项式公式为 X16+X15+X2+1/××××××××××××××××××××××××××function CalCRC16(
18、AData:array of Byte;AStart,AEnd:Integer:Word; constGENP=$A001; /多项式公式 X16+X15+X2+1(1100 0000 0000 0101 varcrc:Word;i:Integer;tmp:Byte;procedure CalOneByte(AByte:Byte; /计算 1个字节的校验码varj:Integer;begincrc:=crc xor AByte; /将数据与 CRC 寄存器的低 8位进行异或 for j:=0 to 7 do /对每一位进行校验begintmp:=crc and 1; /取出最低位crc:=cr
19、c shr 1; /寄存器向右移一位crc:=crc and $7FFF; /将最高位置 0if tmp=1 then /检测移出的位,如果为 1,那么与多项式异或crc:=crc xor GENP;crc:=crc and $FFFF;end;end;begincrc:=$FFFF; /将余数设定为 FFFFfor i:=AStart to AEnd do /对每一个字节进行校验CalOneByte(ADatai;Result:=crc;end;function returnCRC16(vStr1:String:String;varData:array0.255 of Byte;i,j,Co
20、unt:Integer;Res:Word;ResStr:string;szData:string;beginszData:=Trimplace(vStr1; /读入欲校验的字符串Count:=round(Length(Trimplace(vStr1/2; /读入需要计算的字符串长度i:=1;j:=0;for j:=0 to Count-1 dobeginif (i mod 2=0 then /每 2个字符放入一个字节中i:=i+1;if i>=Length(szData thenexit;Dataj:=StrToInt('$'+copy(szData,i,2; /取出字符
21、并转换为 16进制数i:=i+1;end;/showmessage(inttostr(Count-1;Res:=CalCRC16(Data,Low(Data,Count-1;ResStr:=IntToHex(Res,4;Result:=RightStr(ResStr,2+' '+LeftStr(ResStr,2; /两个字节对调 *end;/=设置串口 = procedure TForm1.SetComPort(;begin/获取计算机中的串口列表,并设置当前设备的串口号EnumComPorts(ComboBox1.Items;begintryComm1.CommName:=C
22、omboBox1.Text;Comm1.BaudRate:=StrtoInt(ComboBox2.Text; /设置串口的当前波特率Comm1.StopComm; /打开之前先关闭串口 Comm1.StartComm; /Label1.Caption:='串口状态:'+ComboBox1.Text+'串口已打开 'exceptLabel1.Caption := '状态:'+ComboBox1.Text+'串口打开失败 'end;endelsebeginLabel1.Caption:='串口状态:'+ComboBox
23、1.Text+'串口已关闭 'end;end;/=打开串口 = procedure TForm1.Button1Click(Sender: TObject;begintryComm1.BaudRate:=StrtoInt(ComboBox2.Text; /设置串口的当前波特率Comm1.CommName:=ComboBox1.Text;if ComboBox3.Text='Even(偶 ' then /设置奇偶校验Comm1.Parity:=Even;if ComboBox3.Text='Mark(标记 ' thenComm1.Parity:=M
24、ark;if ComboBox3.Text='None(无 ' thenComm1.Parity:=None;if ComboBox3.Text='Odd(奇 ' thenComm1.Parity:=Odd;if ComboBox3.Text='Space(空格 ' thenComm1.Parity:=Space;if ComboBox4.Text='5' then /设置数据位Comm1.ByteSize:=_5; /设置数据位if ComboBox4.Text='6' thenComm1.ByteSize:=_
25、6; /设置数据位if ComboBox4.Text='7' thenComm1.ByteSize:=_7; /设置数据位if ComboBox4.Text='8' thenComm1.ByteSize:=_8; /设置数据位if ComboBox5.text='1' then /设置停止位Comm1.StopBits:=_1;if ComboBox5.text='1.5' thenComm1.StopBits:=_1_5;if ComboBox5.text='2' thenComm1.StopBits:=_2;c
26、omm1.StopComm;comm1.StartComm;Label1.Caption:='状态:'+ComboBox1.Text+'串口已开始捕获 'exceptLabel1.Caption := '状态:'+ComboBox1.Text+'串口打开失败 'end;/Label1.Caption:=inttoStr(Comm1.CommPort;/Label1.Caption:=InttoStr(Comm1.BaudRate;end;/关闭串口procedure TForm1.Button2Click(Sender: TObj
27、ect;begincomm1.StopComm;Label1.Caption := '状态:'+ComboBox1.Text+'串口已停止捕获 'end;/=设置串口号 = procedure TForm1.FormCreate(Sender: TObject;beginSetComPort(;end;/=自定义发送数据过程 = procedure senddata;vari:integer;commflg:boolean;beginviewstring:=''commflg:=true;/showmessage(inttostr(high(sb
28、uf;for i:=1 to high(sbuf dobeginbegincommflg:=false;break;end;/发送时字节间的延时sleep(1;viewstring:=viewstring+inttohex(sbufi,2+' 'if not commflg then messagedlg('发送失败 !',mterror,mbyes,0;end;viewstring:='发送 '+viewstring;end;/ =串口发送数据 = procedure sentcustom(SqrStr:string;var str11:str
29、ing ;i,j,k:integer;tmpstr:string;beginif SqrStr<>'' thenbeginstr11:=Trimplace(SqrStr; /替换字符串中的所有空格i:=round(length(str11/2; /获得字符串长度 , 除 2取整后加 1/showmessage(inttostr(i;setlength(sbuf,i+1; /重新设定发送数组范围/showmessage(inttostr(high(sbuf;for j:=1 to i dobegintmpstr:=copy(str11,j*2-1,2;/showmes
30、sage(tmpstr;if tmpstr='' thentmpstr:='00'sbufj:=byte(strtoint('$'+tmpstr; /将变量转换为 byte 数组end;endelsebeginsetlength(sbuf,9;sbuf1:=byte($01;sbuf2:=byte($03;sbuf3:=byte($00;sbuf4:=byte($00;sbuf5:=byte($00;sbuf6:=byte($05;sbuf7:=byte($85;sbuf8:=byte($C9;end;senddata;end;/=接收数据 =
31、procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word;typess=array1.8192of char ;varstr1:ss;s:array0.8192 of char ;n:integer;string1:string;beginstr1:=Buffer;/s=('0','1','2','3'.'E'for i:=0 to 9 dosi:=chr(48+i;for i:=10 to 15 do
32、si:=chr(55+i;string1:=''for i:=1 to bufferlength dobeginn:=ord(str1i;string1:=string1+sn div 16+sn mod 16+ ' '/ 除取整 除取余end;/转换字符串完毕后,清空 bufferZeroMemory(buffer,0;/根据过滤状态,调用 replacing 函数替换字符串if label5.Caption='状态:已停止过滤 ' thenbeginstring1 :=string1;endelsebeginbeginstring1:=rep
33、lacing(string1,'82 71 14 01 08 ',''string1:=replacing(string1,'81 33 00 00 09 80 33 00 00 08 ','' end;if trim(string1 <>'' thenbeginTmr_Rtn_Str1:= string1end;if trim(string1 <>'' thenfor i:=1 to 16 dobeginif Tmr_Qry_Stri='01 03 00 00 00
34、 05' thenbeginTmr_Rtn_Stri:= string1;Tmr_Qry_Stri:=''endelseTmr_Qry_Stri:=''end;end;end;/=清除 MEMO 文本框中的数据 = procedure TForm1.Button3Click(Sender: TObject;beginmemo1.Clear;end;procedure TForm1.Button4Click(Sender: TObject;beginlabel5.Caption:='状态:已开始过滤 'end;procedure TForm
35、1.Button5Click(Sender: TObject;beginlabel5.Caption:='状态:已停止过滤 'end;procedure TForm1.Button6Click(Sender: TObject;beginmemo2.Clear;end;/查询状态函数function Return_Sta_Sting(SqrStr:string:String;vari:integer;Cur_CRC16Str,End_Sqr_Str:string;beginfor i:=1 to 32 do /循环地址码 132,将查询返回值保存至数组beginif strtoi
36、nt(copy(Trimplace(SqrStr,1,2=i thenbeginCur_CRC16Str:=returnCRC16(SqrStr; /计算 CRC16校验码End_Sqr_Str:=SqrStr +' '+Cur_CRC16Str; /最终查询字符串sentcustom(End_Sqr_Str; /发送查询指令Result:=End_Sqr_Strendend;end;/定时返回状态值procedure TForm1.Timer1Timer(Sender: TObject;vari,Ad_id:integer; /循环用整数变量Tmr_Qry_Str1:stri
37、ng; /定时器状态查询字符串变量Tmr_Rtn_Str1:string; /定时器状态返回字符串变量beginTmr_Qry_Str1:='01 03 00 00 00 05' /查询字符串Ad_id:=StrToInt('$'+Edit1.Text; /查询字符串地址码tryFor i:=1 to 16 dobeginif i=Ad_id thenbeginTmr_Qry_Stri:=Tmr_Qry_Str1; /为状态查询字符串数组赋值Return_Sta_Sting(Tmr_Qry_Str1; /执行状态查询Tmr_Rtn_Str1:=Trimplace
38、(Tmr_Rtn_Stri; /状态返回值/label28.caption:=Tmr_Rtn_Str1;end;if (Tmr_Rtn_Str1<>'' and (copy(Tmr_Rtn_Str1,5,2='0A' and (round(length(Tmr_Rtn_Str1/2=15 thenbeginEdit7.text:=Tmr_Rtn_Str1;Label8.Caption:=IntToStr(trunc(StrToInt('$'+copy(Tmr_Rtn_Str1,7,4/10; /当前温度 Edit8.Text:=Lab
39、el8.Caption;Label9.Caption:='.'+IntToStr(StrToInt('$'+copy(Tmr_Rtn_Str1,7,4-(trunc(StrToInt('$'+copy(Tmr_Rtn_Str1,7,4/10* 10;Label13.Caption:=IntToStr(trunc(StrToInt('$'+copy(Tmr_Rtn_Str1,11,4/10; /设置温度 Edit9.Text:=Label13.Caption;if StrToInt('$'+copy(Tmr_Rtn_
40、Str1,15,4=1 then /当前 模式beginLabel14.Visible:=false;Label27.Visible:=true;Edit11.Text:='01'endelsebeginLabel14.Visible:=true;Label27.Visible:=false;Edit11.Text:='00'end;if StrToInt('$'+copy(Trimplace(Tmr_Rtn_Str1,23,4=1 then /当前状态 beginPanel6.Visible:=false;Edit10.Text:='0
41、1'endelsebeginPanel6.Visible:=true;Edit10.Text:='00'end;end;end;exceptend;end;/=开关按钮 = procedure TForm1.Button7Click(Sender: TObject;vari,Ad_id:integer;Btn_Qry_Str1:string;beginBtn_Qry_Str1:='01 06 00 04 00 00' /关闭Ad_id:=StrToInt('$'+Edit1.Text; /查询字符串地址码for i:=1 to 16 do
42、beginif i=Ad_id thenbeginif Edit10.Text='01' thenbeginBtn_Qry_Str1:='01 06 00 04 00 00' /关闭Btn_Qry_Stri:=Btn_Qry_Str1;Edit6.Text:=Return_Sta_Sting(Btn_Qry_Str1; /打开Edit10.Text:='00'Edit1.Text:='01' /地址码Edit2.Text:='06' /功能码Edit3.Text:='0004' /寄存器地址Edit
43、4.Text:='0000' /数据 (关闭 Edit5.Text:=Btn_Qry_Str1;endelsebeginBtn_Qry_Str1:='01 06 00 04 00 01' /关闭Btn_Qry_Stri:=Btn_Qry_Str1;Edit6.Text:=Return_Sta_Sting(Btn_Qry_Str1; /打开Edit10.Text:='01'Edit1.Text:='01' /地址码Edit2.Text:='06' /功能码Edit3.Text:='0004' /寄存器地址Edit4.Text:='0001' /数据 (关闭 Edit5.Text:=Btn_Qry_Str1;end;end;end;end;/=模式按钮 = procedure TForm1.Button8Click(Sender: TObject;varBtn_Qry_Str1:string;beginif Edit11.Text='01' thenbeginBtn_Qry_Str1:='01 06 00 02 00 00' /制热模式Edit6.Text:=Return_Sta_Sting(Bt
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 设计类合同协议书
- 软件产品开发与生命周期管理作业指导书
- 2025年聊城道路货运驾驶员从业资格证考试
- 2025年咸宁道路货运驾驶员从业资格证考试题库
- 2024-2025学年高中政治课时作业12博大精深的中华文化含解析新人教版必修3
- 2024-2025学年度九年级物理全册15.3串联和并联教学设计3新版新人教版
- 2024-2025学年高中英语Unit2LanguageSectionⅦWriting-调查报告教案含解析牛津译林版必修3
- 2024年春八年级物理下册第十章浮力章末小结与提升分层精炼新版新人教版
- 2024年新教材高中生物课时素养评价十八6.3.2隔离在物种形成中的作用含解析新人教版必修2
- 苏科版数学八年级上册听评课记录《1-3探索三角形全等的条件(1)》
- 国库集中支付培训班资料-国库集中支付制度及业务操作教学课件
- 诗词写作入门课件
- 2023年上海青浦区区管企业统一招考聘用笔试题库含答案解析
- 2023版押品考试题库必考点含答案
- 植物之歌观后感
- 空气能热泵安装示意图
- 建筑工程施工质量验收规范检验批填写全套表格示范填写与说明
- 2020年中秋国庆假日文化旅游市场安全生产检查表
- 昆明天大矿业有限公司寻甸县金源磷矿老厂箐-小凹子矿段(拟设)采矿权出让收益评估报告
- 心有榜样行有力量 -从冬奥冠军徐梦桃身上感受青春奋斗初中主题班会
- 七年级英语下册阅读理解10篇
评论
0/150
提交评论