VB.net RGB颜色传感器“;整数无效";

VB.net RGB颜色传感器“;整数无效";,vb.net,Vb.net,我试图让VB.net显示来自Arduino的数据“颜色”, 我的串行端口工作得很好,但我每次按connect时都会有这个消息&奇怪的数据显示出现了 An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Additional information: Conversion from string " = " to type 'Integer' is no

我试图让VB.net显示来自Arduino的数据“颜色”, 我的串行端口工作得很好,但我每次按connect时都会有这个消息&奇怪的数据显示出现了

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string " = " to type 'Integer' is not valid.
有人能帮我吗? 这是我的密码

Public Class Form1
    Private _msg As String
    Dim R As String
    Dim G As String
    Dim B As String

    Dim iR As String
    Dim iG As String
    Dim iB As String

    Private indata As String
    Dim IsConnected As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each str As String In IO.Ports.SerialPort.GetPortNames()
            Ports.Items.Add(str)
        Next
        If (Ports.Items.Count > 0) Then
            Ports.SelectedIndex = 0
        End If
        IsConnected = False
        Status.Text = "Disconnected"
        Status.BackColor = Color.MistyRose
        Button1.Enabled = True
        Button2.Enabled = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If (SerialPort1.IsOpen()) Then

            Try
                SerialPort1.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


            IsConnected = False
            Status.Text = "Disconnected"
            Status.BackColor = Color.MistyRose
            Button1.Enabled = True
            Button2.Enabled = False
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (SerialPort1.IsOpen = False) Then
            SerialPort1.PortName = Ports.SelectedItem
            SerialPort1.Open()
            IsConnected = True
            Status.Text = "Connected"
            Status.BackColor = Color.LightGreen
            Button1.Enabled = False
            Button2.Enabled = True
        End If
    End Sub



    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'user chose string
        'read data waiting in the buffer
        Try
            Dim msg As String = SerialPort1.ReadExisting()

            indata = indata + msg
            'Dim where As Integer = InStr(indata, ControlChars.Lf)
            Dim where As Integer = InStr(indata, "R=")
            If (indata.Length > where + 18) Then
                R = indata.Substring(where + 1, 3)
                G = indata.Substring(where + 7, 3)
                B = indata.Substring(where + 13, 3)
                indata = ""
            End If

            'display the data to the user
            _msg = msg
            _msg = _msg.Replace(ControlChars.Cr, "")
            : DisplayData(msg)
        Catch ex As Exception

        End Try


    End Sub

#Region "DisplayData"
    ''' <summary>
    ''' Method to display the data to and
    ''' from the port on the screen
    ''' </summary>
    ''' <remarks></remarks>
    <STAThread()> _
    Private Sub DisplayData(ByVal msg As String)
        DisplayWindow.BeginInvoke(New EventHandler(AddressOf DoDisplay))
    End Sub
#End Region

#Region "DoDisplay"
    Private Sub DoDisplay(ByVal sender As Object, ByVal e As EventArgs)
        'DisplayWindow.SelectedText = String.Empty
        'DisplayWindow.SelectionFont = New Font(_DisplayWindow.SelectionFont, FontStyle.Bold)
        'DisplayWindow.SelectionColor = MessageColor(CType(_type, Integer))
        DisplayWindow.AppendText(_msg)
        DisplayWindow.ScrollToCaret()
    End Sub
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RL.Text = R
        GL.Text = G
        BL.Text = B

        iR = CInt(R) * CInt(RM.Text)
        iG = CInt(G) * CInt(GM.Text)
        iB = CInt(B) * CInt(BM.Text)

        RF.Text = iR
        GF.Text = iG
        BF.Text = iB

        Try
            Label6.BackColor = Color.FromArgb(CType(iR, Byte), CType(iG, Byte), CType(iB, Byte))
        Catch ex As Exception

        End Try

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        System.Diagnostics.Process.Start("http://www.narzan.weebly.com/p-1049.html")
    End Sub

    Private Sub LinkLabel1_LinkClicked_1(sender As Object, e As LinkLabelLinkClickedEventArgs)

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        picOn.Visible = True
        SerialPort1.Open()
        SerialPort1.Write("^")
        SerialPort1.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        picOn.Visible = False
        SerialPort1.Open()
        SerialPort1.Write("<")
        SerialPort1.Close()
    End Sub

    Private Sub picOn_Click(sender As Object, e As EventArgs) Handles picOn.Click

    End Sub

    Private Sub RL_Click(sender As Object, e As EventArgs) Handles RL.Click

    End Sub

    Private Sub RM_TextChanged(sender As Object, e As EventArgs) Handles RM.TextChanged

    End Sub
End Class
公共类表单1
私有消息作为字符串
调暗R为字符串
将G作为字符串
将B变暗为字符串
作为字符串的Dim iR
作为字符串的Dim-iG
将iB设置为字符串
私有indata作为字符串
Dim作为布尔值断开连接
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
对于IO.Ports.SerialPort.GetPortNames()中的每个字符串
Ports.Items.Add(str)
下一个
如果(Ports.Items.Count>0),则
Ports.SelectedIndex=0
如果结束
断开连接=错误
Status.Text=“已断开连接”
Status.BackColor=Color.MistyRose
按钮1.已启用=真
按钮2.Enabled=False
端接头
私有子按钮2\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮2。单击
如果是(SerialPort1.IsOpen()),则
尝试
SerialPort1.Close()
特例
MsgBox(例如消息)
结束尝试
断开连接=错误
Status.Text=“已断开连接”
Status.BackColor=Color.MistyRose
按钮1.已启用=真
按钮2.Enabled=False
如果结束
端接头
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
如果(SerialPort1.IsOpen=False),则
SerialPort1.PortName=Ports.SelectedItem
SerialPort1.Open()
断开连接=正确
Status.Text=“已连接”
Status.BackColor=Color.LightGreen
按钮1.已启用=错误
按钮2.Enabled=True
如果结束
端接头
私有子SerialPort1_DataReceived(ByVal sender作为System.Object,ByVal e作为System.IO.Ports.SerialDataReceivedEventArgs)处理SerialPort1.DataReceived
'用户选择了字符串
'读取缓冲区中等待的数据
尝试
Dim msg As String=SerialPort1.ReadExisting()
indata=indata+msg
'Dim,其中作为整数=InStr(indata,ControlChars.Lf)
Dim,其中为整数=InStr(indata,“R=”)
如果(indata.Length>where+18),则
R=indata.Substring(其中+1,3)
G=indata.子字符串(其中+7,3)
B=indata.子字符串(其中+13,3)
indata=“”
如果结束
'向用户显示数据
_味精=味精
_msg=_msg.Replace(ControlChars.Cr,“”)
:DisplayData(msg)
特例
结束尝试
端接头
#区域“显示数据”
''' 
''方法来向和显示数据
“从屏幕上的端口”
''' 
''' 
_
私有子显示数据(ByVal消息作为字符串)
DisplayWindow.BeginInvoke(新的EventHandler(DoDisplay的地址))
端接头
#末端区域
#区域“DoDisplay”
私有子数据显示(ByVal发送方作为对象,ByVal e作为事件参数)
'DisplayWindow.SelectedText=String.Empty
'DisplayWindow.SelectionFont=新字体(_DisplayWindow.SelectionFont,FontStyle.Bold)
'DisplayWindow.SelectionColor=MessageColor(CType(\u type,Integer))
DisplayWindow.AppendText(_msg)
DisplayWindow.ScrollToCaret()
端接头
#末端区域
私有子Timer1_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理Timer1.Tick
RL.Text=R
GL.Text=G
BL.Text=B
iR=CInt(R)*CInt(RM.Text)
iG=CInt(G)*CInt(GM.Text)
iB=CInt(B)*CInt(BM.Text)
RF.Text=iR
GF.Text=iG
BF.Text=iB
尝试
Label6.BackColor=Color.FromArgb(CType(iR,字节),CType(iG,字节),CType(iB,字节))
特例
结束尝试
端接头
私有子链接Label1\u LinkClicked(ByVal发件人作为System.Object,ByVal e作为System.Windows.Forms.LinkLabelLinkClickedEventArgs)
系统.诊断.进程.启动(“http://www.narzan.weebly.com/p-1049.html")
端接头
私有子链接Label1\u LinkClicked\u 1(发送方作为对象,e作为链接LabelLinkClickedEventArgs)
端接头
私有子按钮4\u单击(发送者作为对象,e作为事件参数)处理按钮4。单击
picOn.Visible=True
SerialPort1.Open()
SerialPort1.写入(“^”)
SerialPort1.Close()
端接头
私有子按钮3\u单击(发送者作为对象,e作为事件参数)处理按钮3。单击
picOn.Visible=False
SerialPort1.Open()

SerialPort1.Write(“我的问题来自我的Arduino代码,而不是VB.net 解决方案是从R、G、B中删除“=”

void printColour(){
Serial.print("R = ");
Serial.println(int(colourArray[0]));
Serial.print("G = ");
Serial.println(int(colourArray[1]));
Serial.print("B = ");
Serial.println(int(colourArray[2]));
//delay(2000);
}
所以代码看起来是这样的

  void printColour(){
    Serial.print("");
    Serial.println(int(colourArray[0]));
    Serial.print("");
    Serial.println(int(colourArray[1]));
    Serial.print("");
    Serial.println(int(colourArray[2]));
    //delay(2000);
    }

谢谢大家,现在一切都很好

显然问题与C无关,也与Arduino无关。请提供一个我也在考虑发布我的Arduino代码!也许我的问题就在那里,我不知道yetSet断点并逐步完成代码。这有助于使用它们,否则调试问题可能是a$$中的一大难题…这就是为什么首先,它们是for…我认为第一个计时器勾号导致代码将CInt语句中的“=”值解释为整数,从而导致异常。由于DataReceived例程中的Instr方法仅跳过“R=”位置之外的一个字符,因此从传入数据中解析“=”值,这是=“签名。尝试向前跳过该索引。Arduino的数据流是什么样子的?”。