得说明是什么型号的PLC啊,串口还是网口,VB6还是VB.NET?
Imports System.NetImports 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
我做念型的台达的,西门子的也一样,你参考一下Private Sub Command1_Click()
Dim strout As String
Dim str As String
Timer1.Enabled = False
str = "00050C30FF00" 'M1072 为PLC起动停止标志位。查地址表,M1072为OC30.FF00为置ON,0000为置OFF。
'以上都是固定格式,要牢记。
LRCC = LRC(str) '计算 str的lrc校验码。
strout = ":" + str + LRCC + Chr$(13) + Chr$(10) '游高谈欲传送之数据。13为D,10为神碰A
MSComm1.Output = strout
Timer1.Enabled = True
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)