【问题标题】:Exception Thrown - Receive data from Arduino to Visual Basic抛出异常 - 从 Arduino 接收数据到 Visual Basic
【发布时间】:2021-02-10 00:21:41
【问题描述】:

我正在尝试从 Arduino 发送串行数据并在 Visual Basic 上读取它。当我执行代码时,有时有效,有时无效:抛出异常,System.ArgumentOutOfRangeException:'索引和长度必须引用字符串中的位置。你能帮助我吗? 我是 Visual Basic 新手,谢谢。

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
Dim TWSL, TWAL, THL, AoAL, WAL, PeL, RoilL, RyL, RydL As Integer
Dim TWS, TWA, TH, AoA, WA, Pe, Roil, Ry, Ryd, TWSResult, TWAResult, THResult, AoAResult, WAResult, PeResult, RoilResult, RyResult, RydResult As String
Dim StrSerialIn, StrSerialInRam As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.CenterToParent()
    SerialPort1.PortName = "COM4"
    SerialPort1.BaudRate = 9600
    SerialPort1.Open()
    Timer1.Start()
    SerialPort1.Write(TrackBarAWA.Value & Chr(10))
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Try
        StrSerialIn = SerialPort1.ReadExisting

        Dim TB As New TextBox
        TB.Multiline = True
        TB.Text = StrSerialIn

        If TB.Lines.Count > 0 Then
            If TB.Lines(0) = "Failed to read" Then
                Timer1.Stop()
                SerialPort1.Close()
                Return
            End If

            StrSerialInRam = TB.Lines(0).Substring(0, 2)
            If StrSerialInRam = "A" Then
                TWS = TB.Lines(0)
                TWSL = TWS.Length
            Else
                TWS = TWS
            End If

            StrSerialInRam = TB.Lines(1).Substring(0, 3)
            If StrSerialInRam = "B" Then
                TWA = TB.Lines(1)
                TWAL = TWA.Length
            Else
                TWA = TWA
            End If

            StrSerialInRam = TB.Lines(2).Substring(0, 3)
            If StrSerialInRam = "C" Then
                TH = TB.Lines(2)
                THL = TH.Length
            Else
                TH = TH
            End If

            StrSerialInRam = TB.Lines(3).Substring(0, 2)
            If StrSerialInRam = "D" Then
                AoA = TB.Lines(3)
                AoAL = AoA.Length
            Else
                AoA = AoA
            End If

            StrSerialInRam = TB.Lines(4).Substring(0, 1)
            If StrSerialInRam = "E" Then
                WA = TB.Lines(4)
                WAL = WA.Length
            Else
                WA = WA
            End If

            StrSerialInRam = TB.Lines(5).Substring(0, 3)
            If StrSerialInRam = "F" Then
                Pe = TB.Lines(5)
                PeL = Pe.Length
            Else
                Pe = Pe
            End If

            StrSerialInRam = TB.Lines(6).Substring(0, 3)
            If StrSerialInRam = "G" Then
                Roil = TB.Lines(6)
                RoilL = Roil.Length
            Else
                Roil = Roil
            End If

            StrSerialInRam = TB.Lines(7).Substring(0, 3)
            If StrSerialInRam = "H" Then
                Ry = TB.Lines(7)
                RyL = Ry.Length
            Else
                Ry = Ry
            End If

            StrSerialInRam = TB.Lines(8).Substring(0, 3)
            If StrSerialInRam = "I" Then
                Ryd = TB.Lines(8)
                RydL = Ryd.Length
            Else
                Ryd = Ryd
            End If

            TWSResult = Mid(TWS, 2, TWSL)
            TWAResult = Mid(TWA, 2, TWAL)
            THResult = Mid(TH, 2, THL)
            AoAResult = Mid(AoA, 2, AoAL)
            WAResult = Mid(WA, 2, WAL)
            PeResult = Mid(Pe, 2, PeL)
            RoilResult = Mid(Roil, 2, RoilL)
            RyResult = Mid(Ry, 2, RyL)
            RydResult = Mid(Ryd, 2, RydL)

            TWSvalue.Text = TWSResult
            TWAvalue.Text = TWAResult
            THvalue.Text = THResult
            AoAvalue.Text = AoAResult
            WAvalue.Text = WAResult
            PeValue.Text = PeResult
            RoilValue.Text = RoilResult
            RyValue.Text = RyResult
            RydValue.Text = RydResult

【问题讨论】:

  • 如果你有StrSerialInRam = TB.Lines(0).Substring(0, 2)(从位置0开始的两个字符),StrSerialInRam = "A"怎么能?也许您想要StrSerialInRam.StartsWith("A") 代替?你确定那里有足够的字符吗?你永远不会测试它。 -- 是什么让您认为 TexttBox(为什么是 TextBox 而不是 List(Of String)?)包含 9 行文本?您是否将 8 0x0A + 0x0C 发送到端口?你也应该测试一下。等
  • 那么该错误消息表明了什么?调试时会发生什么?

标签: vb.net exception arduino serial-communication


【解决方案1】:

据我所知,您正试图从文本框中特定行的特定位置获取字符。根据您在问题中包含的错误消息,我假设错误发生在包含“String.Substring”方法的代码行上。如果您从中获取子字符串的字符串太短而无法覆盖您在 substring 方法中指定的范围,您将收到此错误。例如,如果你得到一个从第 2 行开始的 3 个字符的子字符串,从字符 0 开始,并且它少于 3 个字符,你会得到这个错误。

请参阅有关 String.Substring 方法 here 的文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多