You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中PLC变量无法保留设置及初始化异常问题求助

问题描述

每次扫描后plc变量都会恢复默认值,请问如何保留上次设置的值?

我尝试用GetSetting和SaveSetting函数来实现,示例代码如下:

Private Sub TxtBox_IPAdress_TextChanged(sender As Object, e As EventArgs)
   SaveSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_IP", TxtBox_IPAdress.Text)
End Sub

完整代码:

Option Explicit On
Imports S7.Net

Module GlobalVar

    '  Public PLC As Plc
    ' Public Errorlist As exceptionCode


    Public Valor_do_loop_pre_defenido As Integer = 100    'valor médio pretendido para o tempo de ciclo/loop da aplicação 


    Public hhextracao As String '= "hh"
    Public mmextracao As String '= "mm"
    Public ssextracao As String '= "ss"
    Public thisDay As String '= Date.Now.ToString("dd")
    Public thisMonth As String '= Date.Now.ToString("MM")
    Public thisYear As String '= Date.Now.ToString("yyyy")
    Public thisHour As String '= Date.Now.ToString("HH")
    Public thisMinute As String '= Date.Now.ToString("mm")
    Public thisSec As String '= Date.Now.ToString("ss")
    Public PLC1_IP As String
    Public PLC1_Rack As Integer
    Public PLC1_Slot As Integer
    Public Select_PLC1_Type As Integer
    Public PLC1_Type As CpuType
    Public PLC1 As New Plc(PLC1_Type, PLC1_IP, PLC1_Rack, PLC1_Slot)
    Public test1 As Boolean
    Public test1_in As Boolean
    Public test1_out As Boolean
    Public Sub Settings_Read()



        PLC1_IP = GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_IP", "192.168.0.10")
        PLC1_Rack = GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_Rack", "0")
        PLC1_Slot = GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_Slot", "1")
        Select_PLC1_Type = GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "Select_PLC1_Type", "3")

        Select Case Select_PLC1_Type
            Case 0
                PLC1_Type = CpuType.S7200
            Case 1
                PLC1_Type = CpuType.S7300
            Case 2
                PLC1_Type = CpuType.S7400
            Case 3
                PLC1_Type = CpuType.S71200
            Case 4
                PLC1_Type = CpuType.S71500
            Case 5
                PLC1_Type = CpuType.S7200Smart
            Case 6
                PLC1_Type = CpuType.Logo0BA8
            Case Else 'default
                PLC1_Type = CpuType.S7300
                '       ComboBox_CPUType.SelectedIndex = ComboBox_CPUType_SelectedIndex
        End Select

        ' Dim PLC As New Plc(PLC1_Type, PLC1_IP, PLC1_Rack, PLC1_Slot)
        ' PLC1 = PLC


    End Sub


End Module

遇到的错误

抛出System.TypeInitializationException异常:the type initializer for 'siemensConnext.globalvar' threw an exception
内部异常:ArgumentException: IP address must be valid. Parameter: ip


解决方案

1. 修复初始化顺序问题

核心错误是全局变量初始化顺序错误:PLC1作为全局变量,会在Settings_Read()执行前就被实例化,此时PLC1_IP是空字符串,直接导致IP地址无效的参数异常。

修改步骤:

  • 仅声明PLC1变量,不立即实例化
  • 在Settings_Read()读取完所有配置后,再创建Plc实例并赋值给PLC1

修改后的代码:

Option Explicit On
Imports S7.Net

Module GlobalVar

    Public Valor_do_loop_pre_defenido As Integer = 100    

    Public hhextracao As String 
    Public mmextracao As String 
    Public ssextracao As String 
    Public thisDay As String 
    Public thisMonth As String 
    Public thisYear As String 
    Public thisHour As String 
    Public thisMinute As String 
    Public thisSec As String 
    Public PLC1_IP As String
    Public PLC1_Rack As Integer
    Public PLC1_Slot As Integer
    Public Select_PLC1_Type As Integer
    Public PLC1_Type As CpuType
    ' 仅声明变量,不立即实例化
    Public PLC1 As Plc
    Public test1 As Boolean
    Public test1_in As Boolean
    Public test1_out As Boolean

    Public Sub Settings_Read()
        ' 读取配置,注意转换为对应数据类型
        PLC1_IP = GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_IP", "192.168.0.10")
        PLC1_Rack = CInt(GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_Rack", "0"))
        PLC1_Slot = CInt(GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "PLC1_Slot", "1"))
        Select_PLC1_Type = CInt(GetSetting(My.Application.Info.AssemblyName, "DadosGerais", "Select_PLC1_Type", "3"))

        ' 确定PLC类型
        Select Case Select_PLC1_Type
            Case 0
                PLC1_Type = CpuType.S7200
            Case 1
                PLC1_Type = CpuType.S7300
            Case 2
                PLC1_Type = CpuType.S7400
            Case 3
                PLC1_Type = CpuType.S71200
            Case 4
                PLC1_Type = CpuType.S71500
            Case 5
                PLC1_Type = CpuType.S7200Smart
            Case 6
                PLC1_Type = CpuType.Logo0BA8
            Case Else
                PLC1_Type = CpuType.S7300
        End Select

        ' 读取配置后再实例化PLC
        PLC1 = New Plc(PLC1_Type, PLC1_IP, PLC1_Rack, PLC1_Slot)
    End Sub

End Module

2. 确保配置读取时机正确

在程序启动的最早阶段(比如主窗体的Form_Load事件)调用Settings_Read(),确保所有配置加载完成后再使用PLC1变量:

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GlobalVar.Settings_Read()
    ' 后续操作PLC1的代码放在这里
End Sub

3. 完善配置保存逻辑

确保所有需要保留的参数(机架号、槽号、PLC类型等)都在对应控件值变化时调用SaveSetting,比如PLC类型选择框的事件:

Private Sub ComboBox_CPUType_SelectedIndexChanged(sender As Object, e As EventArgs)
    SaveSetting(My.Application.Info.AssemblyName, "DadosGerais", "Select_PLC1_Type", ComboBox_CPUType.SelectedIndex.ToString())
End Sub

这样修改后,程序启动时会先读取上次保存的配置,再初始化PLC实例,既解决了IP无效的异常,也能完整保留用户上次设置的所有参数。

内容的提问来源于stack exchange,提问作者Colatra93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:01:44