VB.NET使用S7.NET读写S7-1200变量报错求助
在VB.NET中使用S7.NET读取S7-1200变量的正确方法
问题描述
在VB.NET中使用S7.NET库读取S7-1200的变量时持续报错,尝试了多种读取语法(代码中已注释的写法)均未解决。相关代码及PLC变量配置如下:
原代码
Option Strict Off Option Explicit On Imports S7.Net Imports System.Data.SqlClient Imports Microsoft.Reporting.WinForms Imports System.Data Imports VB = Microsoft.VisualBasic Imports Microsoft.VisualBasic.PowerPacks Imports System.Xml Imports System.Text Imports System.IO Imports System.Security.Cryptography Imports System.Threading Module PLC_InOut Public Sub PLCInOuts() ' Dim Result As BitArray() ' Result = PLC.Read("DB8.DBW0") '\\\\\\\Inputs ' Result = PLC.Read(DataType.DataBlock, 8, 0, VarType.Bit, 0, 16) 'Result = PLC.Read(DataType.DataBlock, 8, 0, VarType.Byte, 1, 0) ' Result = PLC.Read("DB7.DBX0.0") 'Dim Value1 As Byte = PLC.Read("DB8.DBW0") Dim u1 As System.UInt16 = PLC.Read("DB8.DBW0") Dim b1() As Byte = BitConverter.GetBytes(u1) If test1_out Then ' PLC.Write(DataType.Output, 0, 0, VarType.Bit, True) 'PLC.Write("O0.0", 1) 'PLC.Write("%M47.6", True) ' PLC.Write("DB7.DBX0.0", True) 'PLC.WriteBit(DataType.DataBlock, 7, 0, 0, True) End If ' ("db8.dbx0.0") '\\\\\\\outputs ' PLC.WriteBit(DataType.DataBlock, 7, 0, 0, test1_out) End Sub End Module
PLC变量配置说明
PLC中DB8块包含字类型变量,DB7块包含位类型变量;需注意S7-1200默认开启优化的块访问,该模式会导致传统地址读取失败,需提前禁用。
解决方案
1. 初始化PLC连接对象
原代码缺失PLC实例的创建与连接步骤,这是核心问题之一。需先创建Plc对象并建立连接:
' 定义全局PLC实例 Private Shared PLC As Plc ' 初始化连接 Public Sub InitPlcConnection() ' S7-1200需指定S7ProtocolType.S1200 PLC = New Plc(S7ProtocolType.S1200, "PLC实际IP地址", 0, 1) Try PLC.Open() If PLC.IsConnected Then Console.WriteLine("PLC连接成功") End If Catch ex As Exception Console.WriteLine("连接失败: " & ex.Message) End Try End Sub
2. 正确的变量读取写法
根据S7.NET语法规范,结合S7-1200地址规则,提供两种可靠读取方式:
方式一:符号地址读取(需禁用优化块访问)
' 读取DB8.DBW0(字类型,对应UInt16) Dim u1 As UInt16 = CType(PLC.Read("DB8.DBW0"), UInt16) ' 读取DB7.DBX0.0(位类型,对应Boolean) Dim bitValue As Boolean = CType(PLC.Read("DB7.DBX0.0"), Boolean)
方式二:参数化读取(更灵活)
' 读取DB8中起始地址0的1个字(UInt16) Dim u1 As UInt16 = CType(PLC.Read(DataType.DataBlock, 8, 0, VarType.Word, 1), UInt16) ' 读取DB7中起始地址0的第0位 Dim bitValue As Boolean = CType(PLC.Read(DataType.DataBlock, 7, 0, VarType.Bit, 0), Boolean)
3. 关键注意事项
- 禁用优化块访问:在TIA Portal中打开目标DB块属性,取消勾选「优化的块访问」,编译后下载到PLC。
- 类型严格匹配:读取的变量类型必须与PLC中定义的类型一致(如DBW对应UInt16,DBX对应Boolean)。
- 先检查连接状态:读写操作前务必确认
PLC.IsConnected为True,避免无效操作。 - 添加异常捕获:通过异常处理定位具体报错原因,示例代码:
Try If PLC.IsConnected Then ' 执行读写操作 Else Console.WriteLine("PLC未连接") End If Catch ex As Exception Console.WriteLine("操作失败: " & ex.Message) End Try
4. 修改后的完整示例代码
Option Strict Off Option Explicit On Imports S7.Net Imports System.Data.SqlClient Imports Microsoft.Reporting.WinForms Imports System.Data Imports VB = Microsoft.VisualBasic Imports Microsoft.VisualBasic.PowerPacks Imports System.Xml Imports System.Text Imports System.IO Imports System.Security.Cryptography Imports System.Threading Module PLC_InOut ' 全局PLC实例 Private Shared PLC As Plc ' 示例控制变量 Private test1_out As Boolean = True ' 初始化PLC连接 Public Sub InitPlcConnection() ' 替换为你的PLC实际IP地址 PLC = New Plc(S7ProtocolType.S1200, "192.168.0.1", 0, 1) Try PLC.Open() If PLC.IsConnected Then Console.WriteLine("PLC连接成功") End If Catch ex As Exception Console.WriteLine("连接失败: " & ex.Message) End Try End Sub Public Sub PLCInOuts() ' 检查连接状态 If Not PLC?.IsConnected Then Console.WriteLine("PLC未连接,请先初始化连接") Return End If Try ' 读取DB8.DBW0 Dim u1 As UInt16 = CType(PLC.Read("DB8.DBW0"), UInt16) Dim b1() As Byte = BitConverter.GetBytes(u1) Console.WriteLine("DB8.DBW0读取值: " & u1) ' 读取DB7.DBX0.0 Dim db7Bit As Boolean = CType(PLC.Read("DB7.DBX0.0"), Boolean) Console.WriteLine("DB7.DBX0.0读取值: " & db7Bit) ' 写入操作示例 If test1_out Then PLC.Write("DB7.DBX0.0", True) Console.WriteLine("已将DB7.DBX0.0设为True") End If Catch ex As Exception Console.WriteLine("读写操作失败: " & ex.Message) End Try End Sub End Module
内容的提问来源于stack exchange,提问作者Colatra93
相关产品推荐
相关产品推荐

