2022年用VB编写modbus源代码的过程终版 .pdf

上传人:Q****o 文档编号:27868961 上传时间:2022-07-26 格式:PDF 页数:13 大小:72.13KB
返回 下载 相关 举报
2022年用VB编写modbus源代码的过程终版 .pdf_第1页
第1页 / 共13页
2022年用VB编写modbus源代码的过程终版 .pdf_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《2022年用VB编写modbus源代码的过程终版 .pdf》由会员分享,可在线阅读,更多相关《2022年用VB编写modbus源代码的过程终版 .pdf(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、用 VB编写 modbus源代码的过程今天是周六, 现在刚从单位自愿加班回来。 为什么自愿加班呢?因为自己和老板夸下海口说:一个礼拜把modbus上位机程序写好(也就是这个礼拜拉)。现在终于把这个程序写好拉。感想很多啊! 该如何说起呢?我语文不好, 把自己想表达的意思说出来就行拉。首先:还是那句话:一切得靠自己!上周一时,老板说让我做个modbus界面,我想:这个简单啊。不就是串口收发么?于是我计划安排上写了1 个礼拜搞定。然后开始安装 DELPHI (我在单位一直搞C编程,机子没有装这些),完后就打开我那曾经熟悉的界面。恩?怎么看似熟悉的界面怎么这么陌生呢/ 于是上网找资料。这个 3 天的期

2、间我感慨好深啊:因为我大四学习的DELPHI ,所以我感觉这个还熟悉点, 可是当时做课程设计的时候也没有用过串口,这个串口编程咋得和 MCU 串口不一样呢?它就是不一样!晕晕。上网找SPCOMM。,学习了一天也不会用。 于是徒劳了 3 天也没有效果。 转眼就到了周四, 我心里那个急呀。唉!还是忍痛抛弃delphi吧!为什么呢?它没有自带串口控件,下载并安装了还是出错啊。 VB好象是我大二学的,具体时间我也忘了,反正就记得做课设的时候用它连个 ACCESS 实现了插入删除查询工作。以后知道这个周四我也没有用过,甚至没有见到过它的界面。 我选择它一个因为我选择, 我喜欢。更重要的是它安装了自带串口

3、控件,不用下载什么的了,用起来实打实的心里有底。大家现在知道我的底子了吧?周四我借公司里的一本VB书看了一天的串口,然后心理有底了,周五动手一天,收获很大,今天早晨8 点起床,心理还琢磨着程序的事情, 由于家里没有实验仪器, 于是打算去公司, 一直做到现在, 终于搞定拉。这个礼拜上网找源代码的时候看着好多像我一样找源码的人发帖子要源程序,但是大多只是网页写了一大堆代码。没有用!为什么呢?你怎么知道人家用什么控件,什么功能呢?还是老老实实的自己写吧。没什么的,很简单的啊。说实话,这个程序只要你把浮躁的心放下来写就可以了。我自从毕业一直没有做过 WINDOWS编程,都是直接对单片机的, 上学那会也

4、是天天乐呼呼的,没学到什名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 13 页 - - - - - - - - - 么东西,就这样我还不是搞定了吗? (当然这个建立在看网上那堆源程序的基础上)。现在我就把这个程序发在网页上,这个程序已经调通了, 写在这里的目的是让大家做个程序参考, 你也不知道我界面上的控件, 所以没有必要复制粘贴。(不是我小气,是因为这个工程涉及到公司机密,这个产品还没有送检,所以不给。发布啊,望谅解)欢迎大家和我讨论,谢谢!Global Data D

5、efinitions Dim MyHandle As Long Handle to Connection Dim MyStatus As Integer Status returned from mbMasterV7 Control Dim Slave As Integer Slave, Cmd, Address, & Length Dim Cmd As Integer Dim Address As Long Dim Length As Integer Dim LoopbackMsg(20) As Byte Public Sub show_status(ErrCode As Integer)

6、If (ErrCode = 0) Then STATUS.Text = 正常通行 ing ElseIf (ErrCode 0 Then Connection was successful (This example only allows a single connection) Disable All Connection Buttons Enable the Read, Write & Disconnect Buttons ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False

7、 Disconnect.Enabled = True LoopBackTst.Enabled = True STATUS.Text = 正在连接 ing READMODBUS.Enabled = True WRITEMODBUS.Enabled = True Else Connection Attempt Failed (Another application must have control of the COM Port) STATUS.Text = 串口忙,请稍候 End If End Sub Handler for the CONNECT TAPI Button Private Su

8、b ConnectTAPI_Click() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 13 页 - - - - - - - - - Dim nTAPIDevices As Long Dim TAPIDevice As String Go through the motions of getting the TAPI Device List nTAPIDevices = MbMasterV71.NumberOfTAPIDevices() TAPIDevice = Mb

9、MasterV71.GetTAPIDeviceName(0) Setup the phone number to dial MbMasterV71.PhoneNumber = 645-5966 Dial the call MyHandle = MbMasterV71.DialTAPIDevice(0) If MyHandle 0 Then Call should be in progress now Dont enable the Read & Write Buttons until we get the CallEstablished Event STATUS.Text = 正在连接 ing

10、 ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = False LoopBackTst.Enabled = False READMODBUS.Enabled = False WRITEMODBUS.Enabled = False Else STATUS.Text = 没有连接 End If End Sub Handler for the CONNECT TCP Button Private Sub ConnectTCP_Click()

11、 Select the Device to connec to In this case use the IP Loopback address to 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 13 页 - - - - - - - - - connect to the local machine MbMasterV71.TCPDevice = 127.0.0.1 MyHandle = MbMasterV71.ConnectModbusTCP(502) If MyHa

12、ndle 0 Then Connection should be in progress now Dont enable the Read & Write Buttons until we get the CallEstablished Event STATUS.Text = 正在连接 ing ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = False LoopBackTst.Enabled = False READMODBUS.E

13、nabled = False WRITEMODBUS.Enabled = False Else STATUS.Text = 没有连接 End If End Sub ConnectionEstablished Event Handler Initiated from either ConnectModbusTCP() or DialTAPIDevice() Private Sub MbMasterV71_ConnectionEstablished(ByVal hConnect As Long) Enable the Disconnect Button Enable Read & Write Bu

14、ttons ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 13 页 - - - - - - - - - Disconnect.Enabled = True LoopBackTst.Enabled = True STATUS.Text = 正常通信 ing READMODBUS.Enabled = Tru

15、e WRITEMODBUS.Enabled = True End Sub ConnectionDropped Event Handler Private Sub MbMasterV71_ConnectionDropped(ByVal hConnect As Long) Either the TCP or TAPI connection attempt failed or something has happened to abort the connection after it has been established for a while. In either case were now

16、 disconnected so enable the buttons accordingly MyHandle = -1 ConnectSerial.Enabled = True ConnectTAPI.Enabled = True ConnectTCP.Enabled = True Disconnect.Enabled = False LoopBackTst.Enabled = False STATUS.Text = 没有连接 READMODBUS.Enabled = False WRITEMODBUS.Enabled = False End Sub Handler for the DIS

17、CONNECT Button Private Sub Disconnect_Click() Tell the control to Disconnect MyStatus = MbMasterV71.Disconnect(MyHandle) MyHandle = -1 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 13 页 - - - - - - - - - ReEnable the Connect Buttons for new connection attempt

18、ConnectSerial.Enabled = True ConnectTAPI.Enabled = True ConnectTCP.Enabled = True Disconnect.Enabled = False LoopBackTst.Enabled = False STATUS.Text = 没有连接 READMODBUS.Enabled = False WRITEMODBUS.Enabled = False End Sub Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single) End Sub

19、Handler for the READ Button Private Sub READMODBUS_Click() Get the Slave Node Address, Modbus Command, Address & Length from the appropriate Edit controls Slave = NODEADDRESS.Text Cmd = POINTTYPE.Text Address = READADDRESS.Text Length = READLENGTH.Text We must remember these parameters so we can use

20、 them in the ReadResponse method to make sure we get what we ask for Initiate the Read Request MyStatus = MbMasterV71.PollModbus(MyHandle, Slave, Cmd, Address, Length) Check the status to make sure the request went out. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第

21、8 页,共 13 页 - - - - - - - - - If MyStatus = 0 Then STATUS.Text = 串口忙 , 请稍侯 Else show_status (MyStatus) End If End Sub Process the Slave Read Response Message Modbus_Master SlaveReadResponse Event Handler Private Sub MbMasterV71_SlaveReadResponse(ByVal hConnect As Long) Dim MyData As Long Dim i As Int

22、eger Read the data returned from the slave and update the text controls For i = 0 To Length - 1 MyStatus = MbMasterV71.ReadResults(hConnect, Slave, Cmd, Address + i, MyData) show_status (MyStatus) If MyStatus = 0 Then Text1(i).Text = MyData If i = 0 Or i = 8 Or i = 16 Then Text1(i).Text = MyData * 系

23、数 End If If i = 2 Or i = 10 Or i = 18 Then Text1(i).Text = MyData * 系数 End If If i = 4 Or i = 12 Or i = 20 Or i = 6 Or i = 14 Or i = 22 Then Text1(i).Text = MyData * 系数 End If If i = 28 Or i = 30 Then 3P,3Q 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 13 页 -

24、- - - - - - - - Text1(i).Text = MyData * 系数 End If If i = 29 Then cosq Text1(i).Text = MyData * 系数 End If If i = 27 Then F Text1(i).Text = MyData * 系数 End If If i = 1 Or i = 5 Or i = 7 Or i = 9 Or i = 13 Or i = 15 Or i = 17 Or i = 21 Or i = 23 Or i = 25 Or i = 23 Or i = 26 Or i = 31 Then Text1(i).Te

25、xt = 0 没有测量的量默认为0 End If Else Text1(i).Text = Error End If Next i End Sub Private Sub WRITEMODBUS_Click() Dim IsRegister As Boolean Dim i As Integer Dim junk As Integer Slave = NODEADDRESS.Text Address = WRITEADDRESS.Text 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - -

26、第 10 页,共 13 页 - - - - - - - - - Length = WRITELENGTH.Text If Length 200 Then Length = 200 End If For i = 0 To Length - 1 junk = MbMasterV71.FillWriteBuffer(MyHandle, i, PATTERN.Text) Next i If POINTTYPE.Text 0 Then For i = 0 To NumberOfBytes - 1 MyStatus = MbMasterV71.ReadUserMsgResponse(hConnect, i, temp) LoopbackMsg(i) = temp Next i STATUS.Text = LoopbackMsg End If End Sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 13 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁