能给我发个VB与三菱PLC通讯的源代码实例吗,非常感谢啊6
发布网友
发布时间:2023-10-21 18:01
我来回答
共3个回答
热心网友
时间:2024-12-03 22:23
得说明是什么型号的PLC啊,串口还是网口,VB6还是VB.NET?
算了,写段代码,VB.NET与Q系列以太网通讯的:
Imports System.Net
Imports System.Runtime.InteropServices
Public Class Form1
Dim Handle1 As Int32
Dim EntLink1 As Boolean
Dim ScanCount1 As Long
Dim PLC As New EntQsPlc_Asc.PlcClient'EntQsPlc_ASC.DLL是Q系列以太网通讯组件
Public Declare Function timeGetTime Lib "winmm.dll" () As UInt32
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
cmbReadMry.Items.Clear()
cmbReadMry.Items.Add("X")
cmbReadMry.Items.Add("Y")
cmbReadMry.Items.Add("M")
cmbReadMry.Items.Add("D")
cmbReadMry.Items.Add("R")
cmbReadMry.Items.Add("Z")
'
cmbWriteMry.Items.Clear()
cmbWriteMry.Items.Add("X")
cmbWriteMry.Items.Add("Y")
cmbWriteMry.Items.Add("M")
cmbWriteMry.Items.Add("D")
cmbWriteMry.Items.Add("R")
cmbWriteMry.Items.Add("Z")
'
cmbBitMry.Items.Clear()
cmbBitMry.Items.Add("X")
cmbBitMry.Items.Add("Y")
cmbBitMry.Items.Add("M")
'
cmbReadType.Items.Clear()
cmbReadType.Items.Add("INT16")
cmbReadType.Items.Add("UINT16")
cmbReadType.Items.Add("DINT32")
cmbReadType.Items.Add("HEX32")
cmbReadType.Items.Add("REAL32")
cmbReadType.Items.Add("BIN16")
'
cmbWriteType.Items.Clear()
cmbWriteType.Items.Add("INT16")
cmbWriteType.Items.Add("UINT16")
cmbWriteType.Items.Add("DINT32")
cmbWriteType.Items.Add("HEX32")
cmbWriteType.Items.Add("REAL32")
cmbWriteType.Items.Add("BIN16")
cmbReadMry.SelectedIndex = 3
cmbWriteMry.SelectedIndex = 3
cmbBitMry.SelectedIndex = 2
cmbReadType.SelectedIndex = 0
cmbWriteType.SelectedIndex = 0
lstRead.Items.Clear()
txtWrite.Text = ""
'
cmbCmdType.SelectedIndex = 0
End Sub
Private Sub butLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLink.Click
Dim re As Short
Dim restr As String = ""
re = PLC.EntLink(Trim(txtLocalIP.Text), Val(txtLocalPort.Text), Trim(txtRemoteIP.Text), Val(txtRemotePort.Text), "DEMO", Handle1, 1000, CBool(cmbCmdType.SelectedIndex))
txtReLink.Text = re.ToString
If re = 0 Then
EntLink1 = True
MsgBox("PLC联接成功! ")
Else
EntLink1 = False
MsgBox("PLC联接失败: " & restr)
End If
End Sub
Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClose.Click
Dim re As Short
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
re = PLC.DeLink(Handle1)
txtReClose.Text = re.ToString
End Sub
Private Sub butRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRead.Click
Dim re As Short
Dim i As Short
Dim RD() As Object
ReDim RD(Val(txtReadCnt.Text - 1))
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
' Exit Sub
End If
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbReadMry.SelectedIndex + 1
Dim typ As EntQsPlc_Asc.PlcClient.DataType = cmbReadType.SelectedIndex + 1
re = PLC.CmdRead(Handle1, mry, typ, CInt(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
txtReRead.Text = re.ToString
lstRead.Items.Clear()
For i = 0 To UBound(RD) Step 1
If Not IsNothing(RD(i)) Then lstRead.Items.Add(RD(i))
Next i
If re <> 0 Then
Timer1.Enabled = False
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub butWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butWrite.Click
Dim re As Short
Dim i As Short
Dim temp() As String
Dim WD() As Object
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
ReDim WD(Val(txtWriteCnt.Text) - 1)
temp = Split(txtWrite.Text, vbCrLf)
For i = 0 To UBound(WD) Step 1
If i > UBound(temp) Then
WD(i) = 0
Else
WD(i) = Trim(temp(i))
End If
Next i
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbWriteMry.SelectedIndex + 1
Dim typ As EntQsPlc_Asc.PlcClient.DataType = cmbWriteType.SelectedIndex + 1
re = PLC.CmdWrite(Handle1, mry, typ, CInt(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
txtReWrite.Text = re.ToString
If re <> 0 Then
Timer1.Enabled = False
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub butScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butScan.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
ScanCount1 = 0
butScan.Text = "Stop R/W"
Else
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Dim tim As Integer = timeGetTime
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
'
Call butRead_Click(Nothing, Nothing)
Call butWrite_Click(Nothing, Nothing)
'
If (Val(txtReRead.Text) < 0) Or (Val(txtReWrite.Text) < 0) Then
butScan.Text = "Cycle R/W"
Exit Sub
Else
ScanCount1 += 1
txtScanCnt.Text = ScanCount1
txtScanPrd.Text = (timeGetTime - tim) & "ms"
End If
Timer1.Enabled = True
End Sub
Private Sub butBitTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitTest.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim rd As Boolean
Dim re As Short
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbBitMry.SelectedIndex + 1
re = PLC.Bit_Test(Handle1, mry, CUShort(txtBitAdd.Text), rd)
txtBitTest.Text = rd
txtReBit.Text = re
End Sub
Private Sub butBitSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitSet.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim re As Short
re = PLC.Bit_Set(Handle1, cmbBitMry.SelectedIndex + 1, Val(txtBitAdd.Text))
txtReBit.Text = re
End Sub
Private Sub butBitRst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitRst.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim re As Short
re = PLC.Bit_Reset(Handle1, cmbBitMry.SelectedIndex + 1, Val(txtBitAdd.Text))
txtReBit.Text = re
End Sub
End Class
热心网友
时间:2024-12-03 22:24
Abstract: Describes all kinds of basic serial port communication methods using Visual Basic 6.0 and design of free port communication program of MITSUBISH PLC, furthermore set focus on the designing method of serial port communication through dynamic linked library in Visual Basic 6.0
Key Words: Serial port communication, PLC, Visual Basic 6.0
一、引言
在工业生产中,人们常常面临着数据采集与管理。作为工厂自动化的三大支柱可编程控制器PLC(Programmable Logic Controller),由于其安全可靠,广泛的用于数据采集与控制。生产过程中,要监视PLC内部的数据与运行状况,选用市场上的人机界面或组态软件,虽然功能丰富,但大都价格昂贵,在一些中小规模的生产场合,人们希望能自己用高级语言开发一个简易实用的通信程序。微软公司推出的Visual Basic 6.0,开发串行通信程序时,有两种方法,一种是用Windows API函数,另一种是用VB支持的通信控件MSCOMM.OCX。利用Windows API函数是通过调用读写等函数来操作串口。两种比较,使用MSCOMM.OCX控件编程方便,具有更完善的发送和接收功能。这里采用了MSCOMM.OCX控件。
本文根据杭州某包装材料公司原料配料控制系统为背景,简述实现计算机与三菱FX系列PLC通信的实现过程。
二、硬件与选型
日本三菱公司是国际著名的工厂自动化设备制造商,工业可编程控制器在国内市场占有率一直保持前列。项目选用三菱FX2N-64MR型PLC,SC-09电缆作为计算机与PLC通信的连线。连接电缆的9针端连接在计算机串口上,另一端连接在PLC的RS-422编程口。系统框图如图一所示:
SC-09电缆
计算机 三菱 PLC
图一
三、软件编程
软件设计主要包括PLC梯形图设计和计算机VB程序、数据库设计。计算机操作系统为Windows98,编程语言用Visual Basic 6.0,数据库选用Access97,梯形图程序在三菱PLC编程软件GPPWIN 软件环境设计。上位机软件实现计算机对PLC内部数据读取、写入参数和保存数据到数据库。
三菱FX2N型PLC编程口通信协议如下:
命令
命令字
控制元件
备注
读元件
0
X,Y,M,S,T,C,D
读字/位元件状态
写元件
1
X,Y,M,S,T,C,D
写字/位元件状态
置位
7
X,Y,M,S,T,C
位元件置位
复位
8
X,Y,M,S,T,C
位元件复位
通信格式:
起始符
命令字
首地址
数据区
结束符
和校验
波特率9600,偶校验,8位数据位,一位停止位。
在新建工程的表单上添加MSCOMM.OCX控件,初始化程序:
Private Sub Form_Load()
MSComm1.CommPort = 1 '选择COM1口
MSComm1.Settings = "9600,e,7,1"
End Sub
系统参数主画面如下图所示:
图二
数据读取子程序:
Public Function GetData(ByVal StartAddress As String, ByVal GetBytes As Integer, ByVal Hex_Bytes As String) As String
MSComm1.InputLen = GetBytes * 2 + 4
MSComm1.Output = STX + CMD_Read + StartAddress + Hex_Bytes + ETX + GetSumChk(CMD_Read + StartAddress + Hex_Bytes + ETX)
‘传送数据组成:起始符+命令+首地址+数据位+结束符+和检验码
GetData = MSComm1.Input
MSComm1.InBufferCount = 0
MSComm1.InputLen = 0
End Function
如需读取数据,通过调用GetData子程序。
参数设置程序可参考读取子程序,只是改变命令而已。
Access97数据库中包含三个表单,包括参数表、记录表、报警记录表。
通过如下子程序是将实际称量值(记录表)更新到数据库:
Private Sub SaveData()
Dim i As Integer
Dim valid As Boolean
On Error Resume Next
valid = True
Data1.Recordset.AddNew
Data1.Recordset.Fields("dtime") = TimeSerial(OnHour, OnMinute, OnSecond)
Data1.Recordset.Fields("systime") = Time ‘系统时间
Data1.Recordset.Fields("get_xl") = LF(1) ‘小料当前称量值
Data1.Recordset.Fields("get_fjs") = LF(2) ‘方解石当前称量值
Data1.Recordset.Fields("get_cs") = LF(3) ‘长石当前称量值
Data1.Recordset.Fields("get_sys") = LF(4) ‘石英砂当前称量值
Data1.Recordset.Fields("get_cj") = LF(5) ‘纯碱当前称量值
On Error Resume Next
Data1.Recordset.Update ‘更新数据库记录
Data1.Recordset.MovePrevious
End Sub
程序中,LF(i)变量对应为图二系统参数表单中实称值。
保存参数子程序只是对数据库操作,程序结构可参考以上子程序。
上位机与PLC通信质量的好坏,对系统的安全可靠运行影响极大,因此,编写程序时,除了在两者之间的通信协议上保持绝对一致和采取相应的容错技术外,还要考虑系统的整体容错技术,防止系统的不正常。
四、结束语
串行通信是目前计算机与其他设备之间通信广泛采用的方式之一,传送数据准确,程序稳定运行是系统开发的主要目的。本设计中充分利用计算机和PLC的资源,在几乎不增加设备开支的情况下成功的实现上位机与PLC之间的通信。
热心网友
时间:2024-12-03 22:24
相关资源可以搜一下ComFxPlc/ComQsPlc等资源;