发生了'System.OverflowException'类型的第一次机会异常 Microsoft.VisualBasic.dll中
我正在尝试从PLC读取寄存器,给出0到65536之间的不同值。但是,在任何时刻,它都显示此错误,值变为0.请分享您的知识并告诉我为什么会发生这种情况以及如何消除这个错误。有时我也会得到StackOverflowException。作为VB的初学者和编程中的菜鸟,我已经做了很多努力,但这是我最终得到的。任何帮助将不胜感激。
以下是代码:
Imports Communication
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim plc As PLCEthernetReadWrite
Dim APP As New Excel.Application
Dim worksheet As Excel.Worksheet
Dim workbook As Excel.Workbook
Dim FilePath As String
Dim SpeedDisplay As Double
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
plc = New PLCEthernetReadWrite("192.168.1.5")
Timer1.Start()
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
plc.writeword("404145", 0)
workbook.Application.DisplayAlerts = False
APP.Quit()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MeterCount.Text = plc.ReadWord("404597")
MeterCount.Text = Val(MeterCount.Text) / 100
Label4.Text = plc.ReadWord("404141")
SpeedDisplay = Val(Label4.Text) / 1000
SpeedDisplay = Math.Round(SpeedDisplay, 1)
Speed.Text = SpeedDisplay
Label3.Text = plc.ReadWord("404145")
End Sub
Private Sub BrowseButton_Click(sender As Object, e As EventArgs) Handles BrowseButton.Click
Dim myFileDlog As New OpenFileDialog()
'look for files in the c drive
myFileDlog.InitialDirectory = "d:\Defects Data"
'specifies what type of data files to look for
myFileDlog.Filter = "All Files (*.xlsx*)|*.xlsx*"
'specifies which data type is focused on start up
myFileDlog.FilterIndex = 2
'Gets or sets a value indicating whether the dialog box restores the current directory before closing.
myFileDlog.RestoreDirectory = True
'seperates message outputs for files found or not found
If myFileDlog.ShowDialog() = _
DialogResult.OK Then
If Dir(myFileDlog.FileName) <> "" Then
MsgBox("File Loaded: " & _
myFileDlog.FileName, _
MsgBoxStyle.Information)
Else
MsgBox("File Not Found", _
MsgBoxStyle.Critical)
End If
End If
'Adds the file directory to the text box and Loads the File
PathTextBox.Text = myFileDlog.FileName
FilePath = Me.PathTextBox.Text
workbook = APP.Workbooks.Open(FilePath)
worksheet = workbook.Worksheets("sheet1")
PieceNoReadLabel.Text = worksheet.Cells(2, 1).Value
PieceMtrReadLabel.Text = worksheet.Cells(2, 3).Value
FileVerificationLabel.Text = worksheet.Cells(1, 1).Value
If FileVerificationLabel.Text <> "PieceNo" Then
MsgBox("Wrong File")
Else
MsgBox("Right File")
End If
'Getting data from excel file
DC1.Text = worksheet.Cells(2, 4).Value
DC2.Text = worksheet.Cells(3, 4).Value
DC3.Text = worksheet.Cells(4, 4).Value
DC4.Text = worksheet.Cells(5, 4).Value
DC5.Text = worksheet.Cells(6, 4).Value
DC6.Text = worksheet.Cells(7, 4).Value
DC7.Text = worksheet.Cells(8, 4).Value
DC8.Text = worksheet.Cells(9, 4).Value
'Writing to PLC
plc.writeword("404518", S1m1.Text)
plc.writeword("404519", T1m1.Text)
plc.writeword("404520", S2m1.Text)
plc.writeword("404521", T2m1.Text)
plc.writeword("404522", S3m1.Text)
plc.writeword("404523", T3m1.Text)
plc.writeword("404524", S4m1.Text)
plc.writeword("404525", T4m1.Text)
plc.writeword("404526", S5m1.Text)
plc.writeword("404527", T5m1.Text)
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
plc.writeword("404217", 1)
Threading.Thread.Sleep(1000)
plc.writeword("404217", 0)
StartButton.Enabled = False
BrowseButton.Enabled = False
PathTextBox.Enabled = False
PathLabel.Enabled = False
End Sub
'“404097”,“002071”等是PLC的寄存器编号。
'我还在编写更多寄存器并从excel文件中获取更多值,并且如果条件超过我在Timer1_tick事件中读取的3个寄存器的值,则还有很多条件。就像值超过某个值一样,标签的BackColor必须更改。由于字符限制,我无法上传完整代码。但我上传的是大部分代码。
由于
答案 0 :(得分:0)
您可能需要查看AdvancedHMI。它是Visual Studio的HMI工具集,它将为您完成大量的通信工作。它有一个ModbusRTU和ModbusTCP驱动程序。