I have a error please>>Conversion from string "&h308 " to type 'Integer' is not valid

blooz

Member
Joined
Mar 25, 2012
Messages
10
Programming Experience
1-3
I have a error please>>Conversion from string "&h308 " to type 'Integer' is not valid

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'MsgBox(SerialPort1.ReadExisting)


'TextBox1.Text.(SerialPort1.ReadExisting)




Dim values As String
Dim vArray As Array


Dim sHEX As String
Dim iHex As Integer


values = SerialPort1.ReadExisting
vArray = Split(values, ",")


sHEX = ("&h" + vArray(0))
iHex = CType(sHEX, Integer)


TextBox1.Text = iHex




Dim doAppendOutput As New AppendOutputDelegate(AddressOf AppendOutput)
Invoke(doAppendOutput, SerialPort1.ReadExisting)






End Sub
 
You can drop the &H, which is a numeric coding notation, then use Integer.Parse with NumberStyles.HexNumber (will allow trailing white), or Convert.ToIn32 with fromBase parameter set to 16 (remove the trailing white first).
 
Exactly as he said in his post:
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim vArray() As String = SerialPort1.ReadExisting.Split(","c)
        Dim iHex As Integer = Integer.Parse(vArray(0), Globalization.NumberStyles.HexNumber)
        TextBox1.Text = iHex.ToString()
    End Sub
 
Back
Top