Beckhoff Demo.rar

  • S1_885453
  • 1.4MB
  • rar
  • 0
  • VIP专享
  • 0
  • 2022-05-18 03:18
通过VB.NET读写Beckhoff 数字输入输出信号,模拟量信号,实现对Beckhoff PLC的控制,基于TwinCAT 3写的DEMO
Beckhoff Demo.rar
Imports System.IO Imports TwinCAT.Ads Imports System.Linq Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports System.Text.RegularExpressions Public Class TwinCAT3 Dim Client As TcAdsClient Private ClientNetID As String Private PortNum As Integer Public M As UInteger = 61489 '输入模块组地址&HF031 Public P As UInteger = 61473 '输出模块组地址&HF021 Public Output_State(23) As Integer '检查每个输出IO状态 Public Picture_Num(23) As PictureBox Public Input_Pic(15) As PictureBox Dim Input(15) As Integer Dim Output(23) As Integer Dim Pic_pth1 As String = Directory.GetCurrentDirectory() + "\Red.png" Dim Pic_pth2 As String = Directory.GetCurrentDirectory() + "\Green.png" Sub PictureVariable() '将PictureBox赋值给一个数组,通过数组来操作PictureBox Picture_Num(0) = PictureBox1 Picture_Num(1) = PictureBox2 Picture_Num(2) = PictureBox3 Picture_Num(3) = PictureBox4 Picture_Num(4) = PictureBox5 Picture_Num(5) = PictureBox6 Picture_Num(6) = PictureBox7 Picture_Num(7) = PictureBox8 Picture_Num(8) = PictureBox9 Picture_Num(9) = PictureBox10 Picture_Num(10) = PictureBox11 Picture_Num(11) = PictureBox12 Picture_Num(12) = PictureBox13 Picture_Num(13) = PictureBox14 Picture_Num(14) = PictureBox15 Picture_Num(15) = PictureBox16 Picture_Num(16) = PictureBox17 Picture_Num(17) = PictureBox18 Picture_Num(18) = PictureBox19 Picture_Num(19) = PictureBox20 Picture_Num(20) = PictureBox21 Picture_Num(21) = PictureBox22 Picture_Num(22) = PictureBox23 Picture_Num(23) = PictureBox24 Input_Pic(0) = PictureBox50 Input_Pic(1) = PictureBox49 Input_Pic(2) = PictureBox48 Input_Pic(3) = PictureBox47 Input_Pic(4) = PictureBox46 Input_Pic(5) = PictureBox45 Input_Pic(6) = PictureBox44 Input_Pic(7) = PictureBox43 Input_Pic(8) = PictureBox42 Input_Pic(9) = PictureBox41 Input_Pic(10) = PictureBox40 Input_Pic(11) = PictureBox39 Input_Pic(12) = PictureBox38 Input_Pic(13) = PictureBox37 Input_Pic(14) = PictureBox36 Input_Pic(15) = PictureBox35 End Sub Public Shared Function GetNumbers(ByVal str As String) As String Return Regex.Replace(str, "[a-z]", "", RegexOptions.IgnoreCase).Trim() End Function Private Sub TwinCAT3_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed Client.Dispose() Client.Close() Dim TwinCAT3 As Process() = Process.GetProcessesByName("TwinCAT Control.vshost") For Each P As Process In TwinCAT3 P.Kill() Next Dim TwinCAT_Control As Process() = Process.GetProcessesByName("Serach Ads Address.vshost") For Each P As Process In TwinCAT_Control P.Kill() Next Me.Close() End Sub Private Sub TwinCAT3_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim n As Integer Dim Com_Text As String For n = 1 To 24 Com_Text = "Output" & n CmbNum.Items.Add(Com_Text) Next ToolStripStatusLabel2.Text = "Access to the Beckhoff EtherCAT link library by address." Try Dim ds As New AdsStream(30) Dim bw As New AdsBinaryWriter(ds) Call PictureVariable() Client = New TcAdsClient Client.Connect("", 851) Timer1.Enabled = True Catch ex As Exception MessageBox.Show("ADS 写入错误,请确认地址与偏移量是否正确及PLC程序是否运行!") End Try End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim j, k As UInteger ToolStripStatusLabel1.Text = Now Try '通过一个新的端口号读取硬件的状态 Client = New TcAdsClient Client.Connect("", 27905) For j = 0 To 15 Dim N As UInteger = 792 '输入模块偏移地址,&H318,十进制数据为792, N = N + j Input(j) = Client.ReadAny(M, N, GetType(Byte)) If Input(j) = 0 Then Input_Pic(j).Image = Image.FromFile(Pic_pth1) Else Input_Pic(j).Image = Image.FromFile(Pic_pth2) End If Next For k = 0 To 23 Dim Q As UInteger = 664 '输出模块偏移地址,&H298,十进制数据为664, Q = Q + k Output(k) = Client.ReadAny(P, Q, GetType(Byte)) If Output(k) = 0 Then Picture_Num(k).Image = Image.FromFile(Pic_pth1) ElseIf Output(k) = 1 Then Picture_Num(k).Image = Image.FromFile(Pic_pth2) End If Next Catch ex As Exception MessageBox.Show("ADS 读入错误,请确认地址与偏移量是否正确及PLC程序是否运行!") End Try End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim x As UInteger Dim Output_ON As String = "1" Dim Output_OFF As String = "0" Dim Str As String Str = GetNumbers(CmbNum.Text).ToString If Str = "" Then MessageBox.Show("Please Check The Output Numer!") Else Dim W As UInteger = 664 '输出模块偏移地址,&H298,十进制数据为792, x = CType(Str, Integer) W = W + x - 1 If Output(x - 1) = 0 Then Try Client.WriteAny(P, W, Byte.Parse(Output_ON)) Catch ex As Exception MessageBox.Show("ADS 写入错误,请确认地址与偏移量是否正确及PLC程序是否运行!") End Try 'End If Else : Try Client.WriteAny(P, W, Byte.Parse(Output_OFF)) Catch ex As Exception MessageBox.Show("ADS 写入错误,请确认地址与偏移量是否正确及PLC程序是否运行!") End Try End If End If End Sub End Class