Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort
Getting Imput From SerialPort Getting Imput From SerialPort
Getting Imput From SerialPort
Go Back  Xtreme Visual Basic Talk > > > Getting Imput From SerialPort


Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2008, 07:41 PM
abelchoo abelchoo is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default Getting Imput From SerialPort


Dear all,

I would like to find out how to convert an integer to string.
Currently, I'm using SerialPort. And i'm displaying what was set in the design form on the application.

Here are the codes
mainform.SerialPort1.PortName = PortNumber_TextBox.Text
mainform.SerialPort1.BaudRate = BaudRate_TextBox.Text
mainform.SerialPort1.DataBits = DataBits_TextBox.Text



MsgBox("Serial Port has been assigned to " + mainform.SerialPort1.PortName + ".", MsgBoxStyle.Information, "Serial Port")
mainform.ToolStripStatus_Com.Text = mainform.SerialPort1.PortName

The error lies in the above: Conversion from type string to type integer is not valid.

I read up and from what i know, i should convert it to string first.
But do not know how to go abt doing it. Please assist..

Many thanks!
Reply With Quote
  #2  
Old 07-16-2008, 11:07 PM
gpraceman's Avatar
gpracemanGetting Imput From SerialPort gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

I assume you mean convert from String to Integer. You could try this:

Code:
mainform.SerialPort1.BaudRate = CInt(BaudRate_TextBox.Text)
mainform.SerialPort1.DataBits = CInt(DataBits_TextBox.Text)
But if the textbox value cannot be converted to an integer, an exception will be thrown.

Better would be to do it this way:

Code:
Dim iBaud, iDataBits As Integer

Integer.TryParse(BaudRate_TextBox.Text, iBaud)
Integer.TryParse(DataBits_TextBox.Text, iDataBits)

mainform.SerialPort1.BaudRate = iBaud
mainform.SerialPort1.DataBits = iDataBits
Integer.TryParse will attempt to parse the string value to an integer. If it cannot, it will not throw an exception. It will just set the integer variable to zero.

Though, you should first validate that apropriate values are being entered for the baud rate and data bits, since even zero values will lead to an exception when trying to use the serial port.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #3  
Old 07-17-2008, 07:20 AM
AtmaWeapon's Avatar
AtmaWeaponGetting Imput From SerialPort AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There's also a nifty way to use TryParse for error checking. The function returns True if the string was successfully converted into an integer, so if you write something like this:
Code:
If Integer.TryParse(BaudRate_TextBox.Text, iBaud) AndAlso _
   Integer.TryParse(DataBits_TextBox.Text, iDataBits)
   mainform.SerialPort1...
Else
    MessageBox.Show("You must enter an integer for the baud rate and data bits.")
End If
Note that there is a NumericUpDown control which only allows the user to input numbers that might be more useful.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort Getting Imput From SerialPort
Getting Imput From SerialPort
Getting Imput From SerialPort
 
Getting Imput From SerialPort
Getting Imput From SerialPort
 
-->