西門(mén)子S7-200 ModBus從站通訊程序
《西門(mén)子S7-200 ModBus從站通訊程序》由會(huì)員分享,可在線閱讀,更多相關(guān)《西門(mén)子S7-200 ModBus從站通訊程序(25頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、本文格式為Word版,下載可任意編輯 西門(mén)子S7-200 ModBus從站通訊程序 始終看到有些伴侶在問(wèn)ModBus主從站的通訊問(wèn)題,今日抽空兒給大家發(fā)幾篇。 事先申明,我是調(diào)用的主從站庫(kù),庫(kù)本身并不是我編寫(xiě)的,我沒(méi)有那么大的能耐。 另外,上位機(jī)是用VB編寫(xiě),VB本身不是很熟,假如有什么錯(cuò)誤,請(qǐng)大家海諒。 最終,這僅僅只是一個(gè)測(cè)試程序,假如各位想用到商業(yè)方面,還需要各位自己去完善。 說(shuō)明:plc從站,PC作主站,用ModBus協(xié)議進(jìn)行通訊。PC機(jī)讀取PLCVW區(qū)的數(shù)據(jù);PC機(jī)還讀取PLC內(nèi)部I或者O地址的狀態(tài)。 ////////////////////
2、/////////////////// 以下是PLC程序?qū)崿F(xiàn),便利貼出,已轉(zhuǎn)換為STL TITLE=程序解釋 Network 1 // 網(wǎng)絡(luò)標(biāo)題 // 在第一個(gè)循環(huán)周期內(nèi)初始化Modbus從站協(xié)議 LD SM0.1 CALL SBR3, 1, 12, 9600, 0, 0, 128, 32, 1000, VB0, M10.1, MB11 Network 2 // 在每個(gè)循環(huán)周期內(nèi)執(zhí)行Modbus 從站協(xié)議 LD SM0.0 CALL SBR1, M10.2, MB12 Network 3 LD SM0.0 MOVW
3、AIW0, VW100 /I +54, VW100 MOVW AIW2, VW102 /I +54, VW102 MOVW AIW4, VW104 /I +54, VW104 MOVW AIW6, VW106 /I +54, VW106 Network 4 LD SM0.0 = Q0.0 Network 5 LD I0.0 = Q0.1 Network 6 LD I0.1 = Q0.2 /////////////////////////////////// 以下是VB源碼 Opti
4、on Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Dim x1 Dim p11, p12, p13, p14, p15, p16, p17, p18 Function CRC16(data() As Byte) As String Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器 Dim CL As Byte, CH As Byte '多項(xiàng)式碼HA001 Dim SaveHi As Byte, SaveLo As
5、 Byte Dim i As Integer Dim Flag As Integer CRC16Lo = HFF CRC16Hi = HFF CL = H1 CH = HA0 For i = 0 To UBound(data) CRC16Lo = CRC16Lo Xor data(i) '每一個(gè)數(shù)據(jù)與CRC寄存器進(jìn)行異或 For Flag = 0 To 7 SaveHi = CRC16Hi SaveLo = CRC16Lo CRC16Hi = CRC16Hi \ 2 '高位右移一位 CRC16Lo = CRC16Lo
6、 \ 2 '低位右移一位 If ((SaveHi And H1) = H1) Then '假如高位字節(jié)最終一位為1 CRC16Lo = CRC16Lo Or H80 '則低位字節(jié)右移后前面補(bǔ)1 End If '否則自動(dòng)補(bǔ)0 If ((SaveLo And H1) = H1) Then '假如LSB為1,則與多項(xiàng)式碼進(jìn)行異或 CRC16Hi = CRC16Hi Xor CH CRC16Lo = CRC16Lo Xor CL End If Next Flag Next i Dim ReturnData(1) As Byte Retu
7、rnData(0) = CRC16Hi 'CRC高位 ReturnData(1) = CRC16Lo 'CRC低位 CRC16 = ReturnData End Function Private Sub About_Click() frmAbout.Show End Sub Private Sub Form_Load() '初始化 Timer1.Enabled = False '定時(shí)器1無(wú)效 Timer1.Interval = 1000 '定時(shí)器1時(shí)間為1S Timer2.Enabled = True '定時(shí)器2有效 Timer
8、2.Interval = 1000 '定時(shí)器2時(shí)間為1S Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" Text8.Text = "" Text10.Text = "" MSComm1.CommPort = 1 '設(shè)定端口號(hào) MSComm1.Settings = "9600,n,8,1" '設(shè)定通訊波特率 MSComm1.InBufferSize = 1024 '接收緩沖器大小
9、MSComm1.OutBufferSize = 1024 '輸出緩沖器大小 MSComm1.InputMode = comInputModeBinary '以二進(jìn)制傳輸 MSComm1.RThreshold = 1 ' MSComm1.SThreshold = 0 MSComm1.InputLen = 0 '讀取接收緩沖器全部字符 MSComm1.OutBufferCount = 0 '清空發(fā)送緩沖區(qū) MSComm1.InBufferCount = 0 '清空接收緩沖區(qū) If MSComm1.PortOpen = False Then Comma
10、nd3.Caption = "打開(kāi)串口" Else Command3.Caption = "關(guān)閉串口" End If End Sub Private Sub Command1_Click() Timer1.Enabled = True End Sub Private Sub Command2_Click() '退出程序,定時(shí)器1無(wú)效 Timer1.Enabled = False Cls Unload Me End Sub Private Sub Command3_Click() On Error Resume
11、Next If MSComm1.PortOpen = False Then MSComm1.PortOpen = True Else MSComm1.PortOpen = False End If If MSComm1.PortOpen Then '打開(kāi)關(guān)閉按鈕顯示文字 Command3.Caption = "關(guān)閉串口" Else Command3.Caption = "打開(kāi)串口" End If If Err Then '打開(kāi)串口失敗,則顯示出錯(cuò)信息 MsgBox Error$, 48, "錯(cuò)誤信息" Exit Su
12、b End If End Sub Private Sub Timer1_Timer() '讀V存儲(chǔ)區(qū)數(shù)據(jù) Dim CRC() As Byte Dim FGetData As String Dim aa() As Byte Dim s As String Dim str As String Dim i As Integer Dim bb, cc As String ReDim aa(5) As Byte '定義動(dòng)態(tài)數(shù)組 aa(0) = HC aa(1) = H3 aa(2) = H0 aa(3) = H3
13、2 aa(4) = H0 aa(5) = H4 CRC = CRC16(aa) str = CRC s = "" For i = 1 To LenB(str) s = s + Hex(AscB(MidB(str, i, 1))) Next i bb = Right(s, 2) cc = Mid(s, 1, 2) If Len(s) 4 Then cc = Mid(s, 1, 1) End If ReDim Preserve aa(0 To 7) As Byte aa(6) = Val("H" bb)
14、 aa(7) = Val("H" cc) MSComm1.OutBufferCount = 0 '清空輸出寄存器 MSComm1.Output = aa FGetData = ReceiveData Text5.Text = FGetData p11 = Val("H" Mid(FGetData, 7, 4)) p12 = Val("H" Mid(FGetData, 11, 4)) p13 = Val("H" Mid(FGetData, 15, 4)) p14 = Val("H" Mid(FGetData, 19, 4)) E
15、nd Sub Private Sub Command4_Click() 'I狀態(tài) Dim CRC() As Byte Dim FGetData As String Dim aa() As Byte Dim s As String Dim str As String Dim i As Integer Dim bb, cc As String ReDim aa(5) As Byte '定義動(dòng)態(tài)數(shù)組 aa(0) = HC aa(1) = H2 aa(2) = H0 aa(3) = H0 aa(4) = H0 a
16、a(5) = H1 CRC = CRC16(aa) str = CRC s = "" For i = 1 To LenB(str) s = s + Hex(AscB(MidB(str, i, 1))) Next i bb = Right(s, 2) cc = Mid(s, 1, 2) If Len(s) 4 Then cc = Mid(s, 1, 1) End If ReDim Preserve aa(0 To 7) As Byte aa(6) = Val("H" bb) aa(7) = Val("H"
17、cc) MSComm1.OutBufferCount = 0 '清空輸出寄存器 MSComm1.Output = aa FGetData = IReceiveData p17 = Val("H" Mid(FGetData, 7, 2)) p18 = Val("H" Mid(FGetData, 9, 2)) End Sub Private Sub Command5_Click() 'Q狀態(tài) Dim CRC() As Byte Dim FGetData As String Dim aa() As Byte Dim s As St
18、ring Dim str As String Dim i As Integer Dim bb, cc As String ReDim aa(5) As Byte '定義動(dòng)態(tài)數(shù)組 aa(0) = HC aa(1) = H1 aa(2) = H0 aa(3) = H0 aa(4) = H0 aa(5) = H1 CRC = CRC16(aa) str = CRC s = "" For i = 1 To LenB(str) s = s + Hex(AscB(MidB(str, i, 1))) Next i
19、 bb = Right(s, 2) cc = Mid(s, 1, 2) If Len(s) 4 Then cc = Mid(s, 1, 1) End If ReDim Preserve aa(0 To 7) As Byte aa(6) = Val("H" bb) aa(7) = Val("H" cc) MSComm1.OutBufferCount = 0 '清空輸出寄存器 MSComm1.Output = aa FGetData = QReceiveData p15 = Val("H" Mid(FGetData,
20、7, 2)) p16 = Val("H" Mid(FGetData, 9, 2)) End Sub Private Function ReceiveData() As String '返回V存儲(chǔ)器區(qū)數(shù)據(jù) Dim FGetData As String Dim t1 As Long Dim av As Variant Dim i As Integer Dim ReDataLen As Integer FGetData = "" t1 = GetTickCount() '取時(shí)間,做延時(shí)用 Do '循環(huán)等待接收數(shù)據(jù) DoEven
21、ts If MSComm1.InBufferCount 0 Then '串口有數(shù)據(jù)了 ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度 av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái) For i = 0 To ReDataLen - 1 FGetData = FGetData Right("00" Hex(av(i)), 2) Next i End If If Len(FGetData) = 6 Then If Len(FGetData) Val("H" Mid(FGetData, 5,
22、2)) * 2 + 8 Then ReceiveData = FGetData Exit Function End If End If If GetTickCount - t1 2000 Then '2秒沒(méi)收完就不收了 ReceiveData = "" Exit Function End If Loop End Function Private Function IReceiveData() As String '反回I狀態(tài) Dim FGetData As String Dim t1 As Long Dim
23、av As Variant Dim i As Integer Dim ReDataLen As Integer FGetData = "" t1 = GetTickCount() '取時(shí)間,做延時(shí)用 Do '循環(huán)等待接收數(shù)據(jù) DoEvents If MSComm1.InBufferCount 0 Then '串口有數(shù)據(jù)了 ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度 av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái) For i = 0 To ReDataLen - 1 FGetDa
24、ta = FGetData Right("00" Hex(av(i)), 2) Next i End If If Len(FGetData) = 6 Then If Len(FGetData) Val("H" Mid(FGetData, 5, 2)) * 2 + 8 Then IReceiveData = FGetData Exit Function End If End If If GetTickCount - t1 2000 Then '2秒沒(méi)收完就不收了 IReceiveData = "" Exit Functi
25、on End If Loop End Function Private Function QReceiveData() As String '反回Q狀態(tài) Dim FGetData As String Dim t1 As Long Dim av As Variant Dim i As Integer Dim ReDataLen As Integer FGetData = "" t1 = GetTickCount() '取時(shí)間,做延時(shí)用 Do '循環(huán)等待接收數(shù)據(jù) DoEvents If MSComm1.InBuff
26、erCount 0 Then '串口有數(shù)據(jù)了 ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度 av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái) For i = 0 To ReDataLen - 1 FGetData = FGetData Right("00" Hex(av(i)), 2) Next i End If If Len(FGetData) = 6 Then If Len(FGetData) Val("H" Mid(FGetData, 5, 2)) * 2 + 8 Then QRe
27、ceiveData = FGetData Exit Function End If End If If GetTickCount - t1 2000 Then '2秒沒(méi)收完就不收了 QReceiveData = "" Exit Function End If Loop End Function Private Sub Timer2_Timer() x1 = x1 + 1 Text10.Text = str(x1) Text1.Text = str(p11) Text2.Text = str(p12) T
28、ext3.Text = str(p13) Text4.Text = str(p14) Text7.Text = str(p15) If Text7.Text = 1 Then Text7.BackColor = RGB(255, 0, 255) If Text7.Text = 0 Then Text7.BackColor = RGB(0, 255, 255) 'Text6.Text = str(p16) Text9.Text = str(p17) If Text9.Text = 1 Then Text9.BackColor = RGB(255, 0, 255) If Text9.Text = 0 Then Text9.BackColor = RGB(0, 255, 255) 'Text8.Text = str(p18) End Sub 第 25 頁(yè) 共 25 頁(yè)
- 溫馨提示:
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. 裝配圖網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2022年數(shù)學(xué)九下《圓周角定理的推論與圓內(nèi)接四邊形》課件(新湘教版)
- 幼兒園眼睛課件
- 直線與橢圓的位置關(guān)系15版
- 北京垃圾分類(lèi)
- 錨索施工工藝及質(zhì)量控制培訓(xùn)doc資料課件
- 例廣東中醫(yī)藥大學(xué) 中醫(yī)學(xué)
- 水資源的合理利用課件
- 小學(xué)二年級(jí)班家長(zhǎng)會(huì)
- 四.項(xiàng)目的技術(shù)經(jīng)濟(jì)分析
- 安全文明施工圖集萬(wàn)科版本
- 旅游空間布局課件
- 北師版八下數(shù)學(xué)第五章-分式與分式方程本章專(zhuān)題整合訓(xùn)練課件
- 天然氣地球化學(xué)(與“天然氣”有關(guān)的文檔共46張)
- 北師大版四年級(jí)語(yǔ)文下冊(cè)《海上日出》精課件
- LED培訓(xùn)資料XXXX