Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0
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
SerialPort1.Close()
End If
 
Try
With SerialPort1
.PortName = "Com4"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReceivedBytesThreshold = 1
End With
 
SerialPort1.Open()
 

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
 
Try
SerialPort1.Close()
Catch ex As Exception
MsgBox(ex.ToString)
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)
.AppendText(SerialPort1.ReadExisting)
.ScrollToCaret()
Tank1.Value = tankval
TextBox2.Text = tankval
End With
End Sub
 
End Class
Posted 18-Dec-09 10:20am
aparis200
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 ! Big Grin | :-D Big Grin | :-D Big Grin | :-D
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 18 Dec 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100