VB編寫的Modbus RTU協(xié)議通訊源程序_第1頁(yè)
VB編寫的Modbus RTU協(xié)議通訊源程序_第2頁(yè)
VB編寫的Modbus RTU協(xié)議通訊源程序_第3頁(yè)
VB編寫的Modbus RTU協(xié)議通訊源程序_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、VB編寫的Modbus RTU協(xié)議通訊源程序 sunwu 建議刪除該貼! | 收藏 | 回復(fù) | 修改 | 2005-12-09 12:37:09 樓主 modbus rtu協(xié)議可以算是一種事實(shí)上的工業(yè)標(biāo)準(zhǔn)協(xié)議,為許多儀表、PLC等所支持。以前有幾個(gè)用戶問(wèn)如何使用VB編程來(lái)與我們的KND-K3系列PLC通訊,于是整了一個(gè)demo程序。這次把這個(gè)demo共享,希望能給大家一點(diǎn)幫助。 1) 模塊文件:modCRC,其中包含了CRC校驗(yàn)的函數(shù)。 'data 待校驗(yàn)的數(shù)組名稱 'no 數(shù)組中元素個(gè)數(shù) 'btLoCRC 算出的CRC高字節(jié) 'btHiCRC 算出的CRC低

2、字節(jié) Public Function CalCRC16Fast(data() As Byte, no As Integer, btLoCRC As Byte, btHiCRC As Byte) As String Dim CL As Byte, CH As Byte '多項(xiàng)式碼&HA001 Dim SaveHi As Byte, SaveLo As Byte Dim i As Integer Dim Flag As Integer btHiCRC = &HFF btLoCRC = &HFF CL = &H1 CH = &HA0 For i = 0

3、 To (no - 1) btHiCRC = btHiCRC Xor data(i) '每一個(gè)數(shù)據(jù)與CRC寄存器進(jìn)行異或 For Flag = 0 To 7 SaveHi = btLoCRC SaveLo = btHiCRC btLoCRC = btLoCRC 2 '高位右移一位 btHiCRC = btHiCRC 2 '低位右移一位 If (SaveHi And &H1) = &H1) Then '如果高位字節(jié)最后一位為1 btHiCRC = btHiCRC Or &H80 '則低位字節(jié)右移后前面補(bǔ)1 End If '否

4、則自動(dòng)補(bǔ)0 If (SaveLo And &H1) = &H1) Then '如果LSB為1,則與多項(xiàng)式碼進(jìn)行異或 btLoCRC = btLoCRC Xor CH btHiCRC = btHiCRC Xor CL End If Next Flag Next i Dim ReturnData(1) As Byte ReturnData(0) = btHiCRC 'CRC高位 ReturnData(1) = btLoCRC 'CRC低位 CalCRC16Fast = ReturnData End Function Public Function CalCR

5、C16Tbl(data() As Byte, no As Integer, btLoCRC As Byte, btHiCRC As Byte) As String Dim btLoCRC As Byte Dim btHiCRC As Byte btLoCRC = &HFF btHiCRC = &HFF Dim i As Integer Dim iIndex As Long For i = 0 To (no - 1) iIndex = btHiCRC Xor data(i) btHiCRC = btLoCRC Xor GetCRCLo(iIndex) '低位處理 btLo

6、CRC = GetCRCHi(iIndex) '高位處理 Next i Dim ReturnData(1) As Byte ReturnData(0) = btHiCRC 'CRC高位 ReturnData(1) = btLoCRC 'CRC低位 CalCRC16Tbl = ReturnData End Function 'CRC低位字節(jié)值表 Function GetCRCLo(Ind As Long) As Byte GetCRCLo = Choose(Ind + 1, _ &H0, &HC1, &H81, &H40, &

7、;H1, &HC0, &H80, &H41, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _ &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, _ &H80, &H41, &H1, &HC0, &H

8、80, &H41, &H0, &HC1, &H81, &H40, _ &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, _ &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, _ &H0, &HC1, &H81, &H40, &H1

9、, &HC0, &H80, &H41, &H0, &HC1, _ &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _ &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81,

10、 &H40, &H0, &HC1, &H81, &H40, _ &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, _ &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _ &H0, &HC1, &H81, &H40, &H1, &

11、amp;HC0, &H80, &H41, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, _ &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, _ &H80, &H41, &H1, &HC0, &H80, &a

12、mp;H41, &H0, &HC1, &H81, &H40, _ &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _ &H0, &HC1, &H81, &H40, &H0, &

13、;HC1, &H81, &H40, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _ &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &

14、H40, &H0, &HC1, &H81, &H40, _ &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, _ &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0,

15、 &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40) End Function 'CRC高位字節(jié)值表 Function GetCRCHi(Ind As Long) As Byte GetCRCHi = Choose(Ind + 1, _ &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &

16、amp;HC7, &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &

17、amp;HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &

18、amp;H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB, &H2B, &

19、amp;H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _ &H61, &HA1,

20、 &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8,

21、 &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF, &H7D, &HBD, &HBC, &H7 C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0

22、, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59

23、, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83

24、, &H41, &H81, &H80, &H40) End Function 2)窗體:FORM1,上面放置的控件如下: Begin VB.Form frmCom Caption = "Form1" ClientHeight = 8235 ClientLeft = 3885 ClientTop = 2250 ClientWidth = 6810 LinkTopic = "Form1" ScaleHeight = 8235 ScaleWidth = 6810 Begin VB.TextBox txtReceive 注:放置接

25、收上來(lái)的IB0數(shù)據(jù) Height = 495 Left = 1200 TabIndex = 2 Top = 2280 Width = 1335 End Begin VB.CommandButton Command1 Caption = "讀取IB0" Height = 495 Left = 2760 TabIndex = 1 Top = 2280 Width = 1695 End Begin VB.CommandButton cmdSDO Caption = "置位Q1.1" Height = 495 Left = 2160 TabIndex = 0 T

26、op = 3720 Width = 1575 End Begin MSCommLib.MSComm ComK3 Left = 480 Top = 1080 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = -1 'True End End Form_Load事件,在此主要是實(shí)現(xiàn)了打開(kāi)并初始化串口 Private Sub Form_Load() With ComK3 .CommPort = 1 .Settings = "19200,N,8,1" .InputMode = comInputMode

27、Binary '二進(jìn)制收發(fā) .InBufferSize = 512 .OutBufferSize = 512 If (Not .PortOpen) Then .PortOpen = True End With End Sub Form_UnLoad事件,在此主要是關(guān)閉串口 Private Sub Form_Unload(Cancel As Integer) If (ComK3.PortOpen) Then ComK3.PortOpen = False End If End Sub “置位Q1.1”按鈕單擊事件 '設(shè)置Q1.1為1 Private Sub cmdSDO_Click() Dim btSend(8) As Byte btSend(0) = &H1 '目標(biāo)站號(hào) btSend(1) = &H5 '功能碼 btSend(2) = &H0 'Q1.1地址(0009)高字節(jié) btSend(3) = &H9 'Q1.1地址(0009)低字節(jié) btSend(4) = &HFF '強(qiáng)制值高字節(jié) btSend(5) = &H0 '強(qiáng)制值低字節(jié) Dim crc

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論