<< 点击显示目录 >> 主页 PVI通信 > 应用例程 > Windows > PVIServices_VB > 测试Demo下载 |
代码下载
版本信息 |
更新时间 |
说明与下载链接 |
提供人 |
1.00.0 |
2022.05.10 |
使用VB语言写的Demo案例 |
DingHanzhou |
1.00.0 |
2022.05.10 |
对接测试用AS项目 |
DingHanzhou |
1.01.0 |
2022.10.24 |
使用VB语言写的Demo案例,修正BUG,补充注释 |
YuanZhiyi |
1.01.0 |
2022.10.24 |
对接测试用AS项目 |
YuanZhiyi |
VB代码一览
Data.vb
Imports BR.AN.PviServices
Module Data
Public Const MAXPV = 5
Public service As Service
Public cpu As Cpu
Public variable(MAXPV) As Variable
Public PVName(MAXPV) As String
Public initDone As Boolean
End Module
Form1.vb
'/*!
'* @file Form1.vb
'* @brief 基于PVI Services方案,使用VB代码编写的Demo案例
'* @date 创建于:2022.05.10
'* @author Tang Qihuan/YuanZhiyi
'* @copyright B&R
'*/
'/*! History
' *****************************************************************************
' * 2022.05.10 V1.00.1 Tang Qihuan / Ding Hanzhou
' * (New) 初步实现基本功能案例
' * 2022.10.24 V1.00.2 YuanZhiyi
' * (Fix) 修正代码中存在的BUG,实现此写法下支持断线重连
' * (New) 对接AS项目程序,测试支持PLC中文信息在VB画面上显示
' * (New) 更新代码文档说明
' ******************************************************************************
'*/
'/*! Update Link
' *****************************************************************************
' * https://gitee.com/yzydeer/BuR_Assistant/tree/master/CHM_files
' * PVI详细说明文档,请访问以上连接,下载《PVI通讯使用助手.chm》
' ******************************************************************************
'*/
Imports BR.AN.PviServices
Imports System.Threading.Thread
Public Class Form1
'建议PVI变量成员声明作为Public放至单独的Module中,便于PVI通信全局使用。
'Public service As Service
'Public cpu As Cpu
'Public variable() As Variable
'Public PVName() As String
Public ip_adress As String
Public cpu_name As String
Public RobotConnectOK As Boolean
Public TestValueByte As Byte
Public TestValueReal As Double
Public TestValueArr(9) As Byte
Public TestValueString As String
Public TestValueWString As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If service Is Nothing Then
service = New Service("service")
AddHandler service.Connected, New PviEventHandler(AddressOf service_Connected)
service.Connect()
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If BackgroundWorker1.WorkerSupportsCancellation = True Then
e.Cancel = True
Return
End If
'BackgroudWorker功能中的DoWork用于承载异步操作。当调用BackgroundWorker.RunWorkerAsync()时触发。
'由于DoWork事件内部的代码运行在非UI线程之上,所以在DoWork事件内部应避免于用户界面交互,而于用户界面交互的操作应放置在ProgressChanged和RunWorkerCompleted事件中。
'为避免界面变更等操作在此DoWork中执行导致界面卡顿,建议在调用以下以下程序之前,执行线程Sleep
Sleep(10)
Call VariablesRead()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'Call VariablesRead()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If BackgroundWorker1.WorkerSupportsCancellation = False Then
If variable(0) IsNot Nothing Then
If variable(0).IsConnected And variable(0).DataValid Then
'连接变量 数值更新
TextBox1.Text = TestValueByte
TextBox2.Text = TestValueArr(0).ToString
TextBox5.Text = TestValueReal
TextBox6.Text = TestValueString
TextBox9.Text = TestValueWString
End If
End If
End If
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
service.RefreshPviClientsList()
If cpu Is Nothing Then
ip_adress = "127.0.0.1"
cpu_name = "ARsim"
'ip_adress = "192.168.10.110"
'cpu_name = "ARsim"
cpu = New Cpu(service, cpu_name)
AddHandler cpu.Connected, New PviEventHandler(AddressOf cpu_Connected)
cpu.Connection.DeviceType = DeviceType.ANSLTcp
cpu.Connection.ANSLTcp.DestinationIpAddress = ip_adress
cpu.Connection.ANSLTcp.ResponseTimeout = 2000
End If
cpu.Connect()
End Sub
Private Sub service_Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add("Service: " & "is connected" & vbCrLf)
End Sub
Private Sub cpu_Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
If e.ErrorCode = 0 Then
Me.ListBox2.Items.Add(Me.ip_adress & ": " & "is connected" & vbCrLf)
If initDone = False Then
Call VariablesInit()
initDone = True
End If
End If
End Sub
'变量链接初始化操作
Private Sub VariablesInit()
'Add your variable for init
PVName(0) = "gTest01"
variable(0) = New Variable(cpu, PVName(0))
variable(0).Connect()
AddHandler variable(0).Connected, New PviEventHandler(AddressOf Variable00Connected)
'AddHandler variable(0).Disconnected, New PviEventHandler(AddressOf Variable00DisConnected)
PVName(1) = "gTest03"
variable(1) = New Variable(cpu, PVName(1))
variable(1).Connect()
AddHandler variable(1).Connected, New PviEventHandler(AddressOf Variable01Connected)
PVName(2) = "gTesttyp"
variable(2) = New Variable(cpu, PVName(2))
variable(2).Connect()
AddHandler variable(2).Connected, New PviEventHandler(AddressOf Variable02Connected)
PVName(3) = "TestString"
variable(3) = New Variable(cpu, PVName(3))
variable(3).Connect()
AddHandler variable(3).Connected, New PviEventHandler(AddressOf Variable03Connected)
PVName(4) = "TestWString"
variable(4) = New Variable(cpu, PVName(4))
variable(4).Connect()
AddHandler variable(4).Connected, New PviEventHandler(AddressOf Variable04Connected)
'Init variables properties 初始化变量链接参数
For index = 0 To MAXPV - 1
variable(index).Active = True 'True:PLC中的变量可以被PVI访问。False:不访问此变量,当数据量大,当需要提升通信速率,可将不需要通信的变量设置为False。
variable(index).Polling = False 'PVI Monitor参数,建议设置为False,若设置为True,则写变量时,会导致写的上一次变更缓存在PVI Monitor的数值。
variable(index).WriteValueAutomatic = False 'True:注册的通信变量若在VB程序中变化数值,则自动写入到PLC中,设置为True可能会造成额外的负载。 False:关闭自动写功能
variable(index).RefreshTime = 1 '单位为ms。设置数值 > 0,即PLC中数值已固定的频率检查变量是否更新,更新则将数值更新至PVI Monitor中。对于此案例,若参数为1000,数值每1s更新一次;若参数为100,数值每100ms更新一次。
Next
' End If
'修改界面上默认的label名称
Label1.Text = PVName(0)
Label3.Text = PVName(0)
Label2.Text = PVName(1)
Label4.Text = PVName(1)
Label5.Text = PVName(2)
Label6.Text = PVName(2)
Label7.Text = PVName(3)
Label8.Text = PVName(3)
Label9.Text = PVName(4)
Label10.Text = PVName(4)
If RobotConnectOK = False Then
BackgroundWorker1.WorkerSupportsCancellation = False
BackgroundWorker1.RunWorkerAsync()
RobotConnectOK = True
End If
End Sub
'以下代码为反馈,变量连接上后的提示信息
Private Sub Variable00Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add(variable(0).Name & ": " & "is connected" & vbCrLf)
End Sub
'Private Sub Variable00DisConnected(ByVal sender As System.Object, ByVal e As PviEventArgs)
' variable(0).Dispose()
' Me.ListBox2.Items.Add(variable(0).Name & ": " & "is disconnected" & vbCrLf)
'End Sub
Private Sub Variable01Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add(variable(1).Name & ": " & "is connected" & vbCrLf)
End Sub
Private Sub Variable02Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add(variable(2).Name & ": " & "is connected" & vbCrLf)
End Sub
Private Sub Variable03Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add(variable(3).Name & ": " & "is connected" & vbCrLf)
End Sub
Private Sub Variable04Connected(ByVal sender As System.Object, ByVal e As PviEventArgs)
Me.ListBox2.Items.Add(variable(4).Name & ": " & "is connected" & vbCrLf)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call VariablesRead()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call VariablesWirte()
End Sub
Private Sub VariablesRead()
'Add your variable for read
'变量读取数值赋值
If variable(0) IsNot Nothing Then
If variable(0).IsConnected And variable(0).DataValid Then
TestValueByte = variable(0).Value
End If
If variable(1).IsConnected And variable(1).DataValid Then
TestValueArr = variable(1).Value
End If
'读取结构体变量中的成员变量
If variable(2).IsConnected And variable(2).DataValid Then
TestValueReal = Val(variable(2).Value("TypMem04[0].Mem01"))
End If
If variable(3).IsConnected And variable(3).DataValid Then
TestValueString = variable(3).Value.ToString
End If
'对接下位程序中的中文信息呈现
If variable(4).IsConnected And variable(4).DataValid Then
TestValueWString = variable(4).Value.ToString
End If
End If
End Sub
Private Sub VariablesWirte()
Call VaribleDatapackage()
For index = 0 To MAXPV - 1
variable(index).WriteValue()
Next
AddHandler variable(0).ValueWritten, New PviEventHandler(AddressOf Var_ValueWritten)
End Sub
Private Sub VaribleDatapackage()
'Add your variable for write
If variable(0).IsConnected And variable(0).DataValid Then
variable(0).Value = Val(TextBox8.Text)
End If
If variable(1).IsConnected And variable(1).DataValid Then
variable(1).Value(0) = TextBox3.Text
End If
If variable(2).IsConnected And variable(2).DataValid Then
variable(2).Value("TypMem04[0].Mem01") = TextBox4.Text
End If
If variable(3).IsConnected And variable(3).DataValid Then
variable(3).Value(0) = TextBox7.Text
End If
If variable(4).IsConnected And variable(4).DataValid Then
variable(4).Value(0) = TextBox10.Text
End If
End Sub
Private Sub Var_ValueWritten(ByVal sender As System.Object, ByVal e As PviEventArgs)
'Me.ListBox2.Items.Add(PVName(0) & ": " & "is write" & vbCrLf)
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
'断开连接释放连接操作,在一般情况下无须处理,交由PVI Manager处理。
For index = 0 To MAXPV - 1
If variable IsNot Nothing Then
variable(index).Disconnect()
End If
Next
End Sub
End Class