Results 1 to 1 of 1

Thread: Getting 'Overflow' and 'Out of range' exceptions when Serialport.readline is executed

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    1

    Getting 'Overflow' and 'Out of range' exceptions when Serialport.readline is executed

    The scenario is I'm trying to talk to an electronic device via serial port. My code reads the serial port and outputs certain serial commands based on what it reads. The problem I am having is that when I run my code in debug mode 2 exceptions occur.

    First Exception:
    Name:  non-negative number error .jpg
Views: 589
Size:  22.6 KB

    This exception looked like it occurred during the readexisting() command. See below screenshot.

    Name:  VB.net readexisting error.jpg
Views: 535
Size:  35.3 KB



    Second Exception:
    Name:  arthmetic op overflow.jpg
Views: 437
Size:  27.9 KB

    This exception occurred during the readline() command.

    Name:  vb serialport readline error.jpg
Views: 561
Size:  32.0 KB

    I'm new to writing .NET code so I'm not sure why these exceptions are popping up so any help is appreciated. Below is my code.


    Code:
    Imports System
    Imports System.IO.Ports
    Imports System.ComponentModel
    Imports System.Timers
    Imports System.Data.OleDb
    Imports Microsoft.Office.Interop
    Imports System.Text
    Imports System.Runtime.Remoting.Messaging
    
    
    Public Class Form1
        '------------------------------------------------
        Dim myPort As Array
        Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
        Delegate Function MyDelegate(ByVal text As String) As String
        Dim inputStr As String = ""
        Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
        Private Shared alarmCounter As Integer = 1
        Private Shared exitFlag As Boolean = False
        Private Declare Sub keybd_event Lib "user32" _
                             (ByVal bVk As Byte, _
                              ByVal bScan As Byte, _
                              ByVal dwFlags As Byte, _
                              ByVal dwExtraInfo As Byte)
    
        Private Const VK_RETURN As Byte = &HD
        Private Const KEYEVENTF_KEYDOWN As Byte = &H0
        Private Const KEYEVENTF_KEYUP As Byte = &H2
        Dim cn As New OleDbConnection
        Dim CM As New OleDbCommand
    
    
        '------------------------------------------------
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            myPort = IO.Ports.SerialPort.GetPortNames()
            ComboBox1.Items.AddRange(myPort)
            Button2.Enabled = False
        End Sub
    
    
        '------------------------------------------------
        Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
        End Sub
        '------------------------------------------------
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            SerialPort1.PortName = ComboBox1.Text
            SerialPort1.BaudRate = CInt(ComboBox2.Text)
            SerialPort1.Open()
            Button1.Enabled = False
            Button2.Enabled = True
            Button4.Enabled = True
            Wait_Window()   'allows program to get ready after init is pressed, 5 sec wait
    
        End Sub
        '------------------------------------------------
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim MainMenuStr As String = "Main menu"
            Dim StrPos As Integer = 0
            Dim Checker As Integer = 0
    
            SerialPort1.Write(vbCr)
            inputStr = SerialPort1.ReadLine()
            StrPos = InStr(1, inputStr, MainMenuStr, CompareMethod.Text)
          
    
            If (StrPos > 0) Then
                SerialPort1.Write("1")
            End If
    
            Cmd_30()   ' 3 seconds timer and writes 30 to BCM
            Checker = Cmd_1()   ' 6 seconds timer and writes 1 to BCM and returns 1
            'Timer_3()
    
            If (Checker = 1) Then
                CallReadCurrent() 'Read the current value of channel 1
            End If
    
        End Sub
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            SerialPort1.Close()
            Button1.Enabled = True
            Button2.Enabled = False
            Button4.Enabled = False
        End Sub
    
        Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            'SerialPort1.DiscardInBuffer()
            ReceivedText(SerialPort1.ReadExisting())
        End Sub
    
        Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
            If Me.RichTextBox2.InvokeRequired Then
                Dim x As New SetTextCallback(AddressOf ReceivedText)
                Me.Invoke(x, New Object() {(text)})
            Else
                Me.RichTextBox2.Text &= [text] 'append text
            End If
        End Sub
    
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
        End Sub
    
        Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        End Sub
    
        Private Sub Wait_Window()
            Dim t As New Threading.Thread(AddressOf closeMsgbox)
            t.Start(5) '5 second delay
            MsgBox("Please wait 5 seconds", MsgBoxStyle.Exclamation)
        End Sub
    
        Private Sub closeMsgbox(ByVal delay As Object)
            Threading.Thread.Sleep(CInt(delay) * 1000)
            AppActivate(Me.Text)
            keybd_event(VK_RETURN, 0, KEYEVENTF_KEYDOWN, 0)
            keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0)
        End Sub
    
        Private Sub Cmd_30()
            'Dim Timer1 As Timer
            'Timer1 = New Timer()
            Timer1.Enabled = True
            Timer1.Interval = 3000
        End Sub
    
        Function Cmd_1() As Integer
            Dim Pass As Integer = 1
            'Dim Timer1 As Timer
            'Timer1 = New Timer()
            Timer2.Enabled = True
            Timer2.Interval = 6000
            Return Pass
        End Function
    
        Private Sub Timer_3()
            Timer1.Enabled = True
            Timer1.Interval = 8000
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            SerialPort1.Write("30" & vbCr)
            Timer1.Enabled = False
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            SerialPort1.Write("1" & vbCr)
            Timer2.Enabled = False
        End Sub
    
        Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    
        End Sub
    
    Sub CallReadCurrent()
            Dim d As MyDelegate = AddressOf ReadCurrent
            d.BeginInvoke("hello world", New AsyncCallback(AddressOf MyCallback), Nothing)
        End Sub
    
        Sub MyCallback(ByVal result As IAsyncResult)
            Dim str As String
            MessageBox.Show("Now I know ReadCurrent finished")
            Dim resultClass = CType(result, AsyncResult)
            Dim d As MyDelegate = CType(resultClass.AsyncDelegate, MyDelegate)
            'MessageBox.Show("And I also know that the result is: " & d.EndInvoke(result))
            str = d.EndInvoke(result)
            MessageBox.Show(str)
        End Sub
    
        Function ReadCurrent(ByVal text As String) As String
            Dim n As Integer = 0
            Dim ainputStr As String
            Dim StrPos As Integer = 0
            Dim FindStr As String = "1 Current RMS(A)      ="
            Dim amps_rms As String
    
            While (StrPos = 0)
                Try
                    'ainputStr = Nothing
                    ainputStr = SerialPort1.ReadLine()
                    StrPos = InStr(1, ainputStr, FindStr, CompareMethod.Text)
                Catch generatedExceptionName As TimeoutException
                    'MessageBox.Show(inputStr)
                End Try
            End While
    
            MessageBox.Show("you found it")
            'MessageBox.Show(ainputStr)
            amps_rms = Mid(ainputStr, 33, 9)
            MessageBox.Show(amps_rms)
            Return amps_rms
        End Function
    End Class

    Thanks,
    Billy
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width