Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,i'm asking you a question for first time.
I'm new in VB and i want to connect an Atmel with a PC via rs232,i have also installed the NI Measurement Components into my Visual Studio 2008.
The atmel is sending the value that it is reading from ADC port thru is UART port "Print Value". (i don't send chr(13)-CR)
My code in VB reads correctly the data from COM and print them into a text box.
How can i use these data in order to use them into the tank1.value=integer ?
i have try many conversions but nothing.

Imports System.IO.Ports.SerialPort
Public Class Form1

Public Delegate Sub myDelegate()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If SerialPort1.IsOpen Then
End If

With SerialPort1
.PortName = "Com4"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReceivedBytesThreshold = 1
End With


Catch ex As Exception
End Try

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

Catch ex As Exception
End Try

End Sub

Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub

Public Sub updateTextBox()
Dim tankval As integer
tankval = AscW(CChar(SerialPort1.ReadExisting))
With TextBox1
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
Tank1.Value = tankval
TextBox2.Text = tankval
End With
End Sub

End Class

You can use the Integer.Parse() method. However, You want to make sure to only use the part of the string that contains 1 representation of an integer. If you have several together in a row you get an exception or possibly parse the wrong value. Is this data terminated with a linefeed or carriage return or delimited? If so, you can use SerialPort.ReadLine() to get one line at a time. ReadExisting() is not a great function because it can return partial messages or no message at all.
Share this answer
the integer.parse() method is very close to succeed,first hit was displayed,but after that the application stops and i get an error "Input string was not in a correct format."
data are not in line.In hyperterminal i get data by rows without need to send CR.I tried with chr(10) linefeed and chr(13) carriage return and faced the same error.
the atmel is programmed in BASCOM,it just send any data with the PRINT command,i add the chars 10 & 13 near my value.i'm comfused.
Share this answer
Your problem is this line:
tankval = AscW(CChar(SerialPort1.ReadExisting))

ReadExisting() returns a string and you are converting it to a character and then converting it to a string.

If you are sure you only have one message you can do this:

tankval = Integer.Parse(SerialPort1.ReadExisting().Trim())

If not you may consider using String.Split() or some other way to get the individual values out of the return string.

BTW- It's generally not good form to do so much processing in the event handler, but that's a whole different question.
Share this answer
You really helped find the error,the transmited value need a line feed chr(10) and in VB must be "tankval=Integer.Parse(SerialPort1.ReadLine.Trim())".
thank you very much for your help ! :-D :-D :-D
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900