/ / Capteur de couleur RVB VB.net « Integer is not valid » - vb.net

Capteur de couleur RVB VB.net «L'entier n'est pas valide» - vb.net

J'essaie de faire en sorte que VB.net affiche les "couleurs" des données de mon Arduino, Mon port série fonctionne très bien, mais j'ai ce massage à chaque fois que j'appuie sur connexion et des données étranges s'affichent

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

Additional information: Conversion from string " = " to type "Integer" is not valid.

Quelqu'un peut-il m'aider en cela? c'est mon code

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

Réponses:

0 pour la réponse № 1

Mon problème venait de mon code Arduino et non de VB.net la solution est de supprimer "=" de 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);
}

donc le code ressemblera à ça

  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);
}

Merci à tous, tout fonctionne très bien maintenant