关于vb.net与三菱PLC通信
展开全部
'实例化PLC通讯组件库EntFxPlc_ADP;
Dim PLC As New EntFxPlc_ADP.PlcClient
'定义连接状态标志
Dim EntLink As Boolean
'定义PLC句柄,多PLC系统时对应不同的PLC模块
Dim PlcHand As Int32
'与PLC建立以太网连接
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", PlcHand, 1000, CBool(cmbCmdType.SelectedIndex))
txtReLink.Text = re.ToString
If re = 0 Then
EntLink = True
MsgBox("PLC联接成功!")
Else
EntLink = False
MsgBox("PLC联接失败: " & restr)
End If
End Sub
'断开与PLC的以太网连接
Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClose.Click
Dim re As Short
re = PLC.DeLink(PlcHand)
txtReClose.Text = re.ToString
EntLink = False
End Sub
'读取PLC的数据
Private Sub butRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRead.Click
Dim i As Short
Dim re as Short
Dim RD() As Object
ReDim RD(Val(txtReadCnt.Text - 1))
If Not EntLink Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
'INT,DINT,REAL等数据类型;
Dim var1 As Integer = cmbReadType.SelectedIndex + 1
Dim typ As EntFxPlc_ADP.PlcClient.DataType = var1
'根据所选的PLC内存区域(X,Y,M,D,R...),读取PLC的数据
Select Case cmbReadMry.SelectedIndex
Case 0 : re = PLC.CmdRead(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DI, typ, CUShort(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
Case 1 : re = PLC.CmdRead(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DQ, typ, CUShort(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
Case 2 : re = PLC.CmdRead(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.MR, typ, CUShort(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
Case 3 : re = PLC.CmdRead(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DR, typ, CUShort(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
Case 4 : re = PLC.CmdRead(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.RR, typ, CUShort(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
End Select
'显示函数结果
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))
Else
lstRead.Items.Add("0")
End If
Next i
End Sub
'数据写入PLC
Private Sub butWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butWrite.Click
Dim i As Short
Dim re as Short
Dim temp() As String
Dim WD() As Object
If Not EntLink Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
'准备写入PLC的数据
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
'INT,DINT,REAL等数据类型;
Dim var1 As Integer = cmbWriteType.SelectedIndex + 1
Dim typ As EntFxPlc_ADP.PlcClient.DataType = var1
'根据所选的PLC内存区域(X,Y,M,D,R...),写入数据数据
Select Case cmbWriteMry.SelectedIndex
Case 0 : re = PLC.CmdWrite(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DI, typ, CUShort(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
Case 1 : re = PLC.CmdWrite(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DQ, typ, CUShort(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
Case 2 : re = PLC.CmdWrite(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.MR, typ, CUShort(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
Case 3 : re = PLC.CmdWrite(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.DR, typ, CUShort(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
Case 4 : re = PLC.CmdWrite(PlcHand, EntFxPlc_ADP.PlcClient.PlcMemory.RR, typ, CUShort(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
End Select
txtReWrite.Text = re.ToString
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为何不使用三菱的MX Component部件呢?在三菱官网可以下载4.08版本,支持VS2010,你可以在MX软件的手册上找到例程。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询