1

我正在使用以下 VB 代码:

Option Strict On
Option Infer On

Public Class mainForm

    Private readBuffer As String = String.Empty
    Private Bytenumber As Integer = 1
    Private ByteToRead As Integer = 1
    Private byteEnd(2) As Char
    Private comOpen As Boolean


    Private Sub Form1_FormClosed(ByVal sender As System.Object, _
                                 ByVal e As System.Windows.Forms.FormClosedEventArgs) _
                                 Handles MyBase.FormClosed
        If comOpen Then SerialPort1.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sensorButton.Checked = True
        ' read avaiable COM Ports:
        Dim Portnames As String() = System.IO.Ports.SerialPort.GetPortNames
        If Portnames Is Nothing Then
            MsgBox("There are no Com Ports detected!")
            Me.Close()
        End If
        cboComPort.Items.AddRange(Portnames)
        cboComPort.Text = Portnames(0)
        cboBaudRate.Text = "115200"

    End Sub


    Private Sub btnComOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComOpen.Click

        ' device params
        With SerialPort1

            .ParityReplace = &H3B                    ' replace ";" when parity error occurs
            .PortName = cboComPort.Text
            .BaudRate = CInt(cboBaudRate.Text)
            .Parity = IO.Ports.Parity.None
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
            .RtsEnable = False
            .ReceivedBytesThreshold = 1             'threshold: one byte in buffer > event is fired
            .NewLine = vbCr         ' CR must be the last char in frame. This terminates the SerialPort.readLine
            .ReadTimeout = 10000

        End With

        ' check whether device is avaiable:
        Try
            SerialPort1.Open()
            comOpen = SerialPort1.IsOpen
        Catch ex As Exception
            comOpen = False
            MsgBox("Error Open: " & ex.Message)
            picOpen.BackColor = Color.Red
        End Try

        If comOpen Then
            picOpen.BackColor = Color.Green
            cboComPort.Enabled = False
            cboBaudRate.Enabled = False
        End If

    End Sub

    ''' <summary>
    ''' close ComPort
    ''' </summary>
    Private Sub Button_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComClose.Click
        If comOpen Then
            ' clear input buffer
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()
        End If
        comOpen = False
        picOpen.BackColor = Color.Red
        picDataReceived.BackColor = Color.Gray
        cboComPort.Enabled = True
        cboBaudRate.Enabled = True
    End Sub

    ''' <summary>
    ''' clear TextBoxes
    ''' </summary>
    Private Sub Button_clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        tbRx.Text = String.Empty

        txTimer.Stop()
    End Sub

    ''' <summary>
    ''' write content of Textbox to Port
    ''' </summary>
    Private Sub button_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        ...

    End Sub

    ''' <summary>
    ''' close app
    ''' </summary>
    Private Sub Button_ende_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        If comOpen Then
            ' clear input buffer
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()
        End If
        comOpen = False
        Me.Close()
    End Sub

    ''' <summary>
    ''' send control panel key to com port
    ''' </summary>
    ''' <param name="sender">return key name</param>
    Private Sub Tasten_Click(ByVal sender As System.Object,
                             ByVal e As System.EventArgs)


        Dim key As String = CType(sender, Button).Text
        If comOpen Then SerialPort1.Write(key)

    End Sub

    ''' <summary>
    ''' Timer datareceived event
    ''' </summary>
    Private Sub Timer1_Tick(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles Timer1.Tick
        picDataReceived.BackColor = Color.Gray
        Timer1.Enabled = False
    End Sub

二次线程 ''' Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles SerialPort1.DataReceived ...

    End Sub

    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)

...

    End Sub

    .....

End Class

原始代码可在此处找到:https ://code.msdn.microsoft.com/windowsapps/SerialPort-Sample-in-VBNET-fb040fb2

我已经修改了代码以满足我的需要,当我从另一台 PC 上的 Debug 文件夹运行应用程序时,我看到错误“索引超出了数组的范围”(这些文件在我的 PC 上使用 Visual工作室)。我认为问题出在我的修改上,但看起来原始文件无法运行。我试图用值 1 初始化整数“Bytenumber”和“ByteToRead”,我仍然是这个问题。可能是什么原因造成的?

异常文本是:

************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at LMUEngineering.mainForm.Form1_Load(Object sender, EventArgs e) in C:\Users\---\OneDrive\frmMain.vb:line 38
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
4

0 回答 0