2015-09-04 38 views
-1

類型的第一個機會異常「System.OverflowException」在 發生Microsoft.VisualBasic.dll中System.OverflowException

我試圖從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必須改變。由於字符限制,我無法上傳完整的代碼。但是我上傳的是大部分代碼。

謝謝

+2

你從哪裏得到錯誤? – SLaks

+0

在調試器框@SLaks –

+5

什麼是堆棧跟蹤? – SLaks

回答

0

你可能想看看AdvancedHMI。它是Visual Studio的一個HMI工具集,將爲您處理大量的通信工作。它有一個ModbusRTU和ModbusTCP驅動程序。

相關問題