Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hello all i received the error Input string was not in a correct format.
 
 public static int SetPortBaudRate(int defaultPortBaudRate)
        
        {
            string baudRate="";
            _serialPort.BaudRate =Convert.ToInt32(baudRate.ToString());//Error
            defaultPortBaudRate = Convert.ToInt32(baudRate.ToString());//Error
            Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();
 
            if (baudRate == "")
            {
                baudRate = defaultPortBaudRate.ToString();
            }
 
            return int.Parse(baudRate);
        }
 
Posted 2-Jan-13 20:31pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You might consider the following function:
/// <summary>Reads the baud rate number from the Console (the keyboard) and sets the serial port to that rate.</summary>
/// <remarks>In case of error, takes the given defaultPortBaudRate.</remarks>
/// <param name="defaultPortBaudRate">Fallback in case of not matching input.</param>
/// <returns>the set baud rate.</returns>
public static int SetPortBaudRate(int defaultPortBaudRate)
{
    int baudRate = int.TryParse(Console.ReadLine(), out baudRate)
                   ? baudRate
                   : defaultPortBaudRate;
    _serialPort.BaudRate = baudRate;
    return baudRate;
}
Cheers
Andi
  Permalink  
v3
Comments
Arjunwalmiki at 4-Jan-13 0:14am
   
if i am using above code than i was not received any output
Andreas Gieriet at 4-Jan-13 3:54am
   
Output where?
Do you mean, the serial port does not work then?
Andi
Arjunwalmiki at 4-Jan-13 4:43am
   
serial port is working but not received any baudRate
Andreas Gieriet at 4-Jan-13 4:59am
   
I don't understand what you say.
BaudRate is a property of the serial port: it defines the trasmission speed. You can not "receive baudRate" over the serial line...(?).
What do you mean by "serial port is working"? If it is working, all is fine, correct?
 
What is the Console.ReadLine() for? I understand that the user of your program enters here the baud rate from the keyboard, correct?
 
You give so little an confusing answers that you make it hard for us to help you.
 
Please write in plain text what your function should do (e.g. is the comment on my function correct, i.e. does it say what your function should do?).
 
Cheers
Andi
Arjunwalmiki at 4-Jan-13 5:14am
   
sir as u understand very well so know come to point my query is i want to create a master program just u change in device management PortName ,BaudRate ,Parity,DataBits,StopBits,Handshake and no need to change the code in my program my program is capture everything from system whatever user set in that and read-data from comport so know tell me wt is correct code i have already post my code in the 1st time
Andreas Gieriet at 4-Jan-13 5:33am
   
Your code in your original post makes no sense, that's why I'm confused. And even I try hard to understand what you wrote above - I must give up, sorry. Can someone take over, please?
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This means your string is not an Int32. If it is, but with spaces, use the Trim method first. Even then, use int.TryParse to avoid an error ( and handle the case of an invalid input )
  Permalink  
Comments
Arjunwalmiki at 3-Jan-13 2:09am
   
string baudRate="";
baudRate = baudRate.Trim();
_serialPort.BaudRate =Convert.ToInt32(baudRate.ToString());
defaultPortBaudRate = Convert.ToInt32(baudRate.ToString());
 
still same error
jibesh at 3-Jan-13 3:51am
   
Have you tried the int.TryParse method provided in the above solution. double check the content of baudRate in your application. in your sample provided above you are trying to convert an empty string to an Int hence it returns error.
 
if the baudRate contains any non integer value your conversion will end up in error, but if you use int.TryParse it tries to convert gives the int value if success otherwise zero.
Christian Graus at 3-Jan-13 4:06am
   
Of course. Read my answer again. If baudRate is not an int, Convert.ToInt32 will blow up. Don't use it.
Arjunwalmiki at 3-Jan-13 4:13am
   
can u write full code how can i use it
Christian Graus at 3-Jan-13 13:58pm
   
How can you not work this out ? It's really simple. You should really learn enough basics to be able to follow simple instructions.
 
int n;
if (int.TryParse(baudRate, out n))
{
 
_serialPort.BaudRate = defaultPortBaudRate = n;
}
else
{
// Let the user know the input was not valid.
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hey i think
baudRate contains Null Value and
you convert tostring(). so that this error occures.
 
Try Below:
Convert.Tostring(baudRate) Instead of baudRate.ToString()
  Permalink  
Comments
Prasad_Kulkarni at 3-Jan-13 1:42am
   
I think Convert.ToInt32 will work as method is int.
Arjunwalmiki at 3-Jan-13 2:01am
   
if i am try this it is nor working same error
Convert.Tostring(baudRate)
prashant patil 4987 at 3-Jan-13 2:07am
   
hey initialize baudRate = "0" and then try
Arjunwalmiki at 3-Jan-13 2:16am
   
i was set
string baudRate="0";
 
know error is
Positive number required.
Arjunwalmiki at 3-Jan-13 2:18am
   
but if i was set
string baudRate="1";
than i was received result is 1 but the sys i was set 2400 so need result 2400 how can it come
prashant patil 4987 at 3-Jan-13 2:54am
   
hey try this type of parsing.
string baudRate="0";
_serialPort.BaudRate = Int32.Parse(baudRate);
in my pc your code very fine..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi,
 
in the top you define string baudRate="";, then you are again using this string in
_serialPort.BaudRate =Convert.ToInt32(baudRate.ToString());//Error
defaultPortBaudRate = Convert.ToInt32(baudRate.ToString());//Error
 
Can you directly set 0 to the Baud Rate ?
 
int baudRate = 0;
if not then you can use :-
int.TryParse(baudRate.ToString(),out baudRate)
  Permalink  
Comments
Arjunwalmiki at 3-Jan-13 2:07am
   
hello this programing is data rear from comport
 
if my code are as below i was received result 9600 it is default BourdRate but i was set 2400 or etc so i need this so let me confirm
 
public static int SetPortBaudRate(int defaultPortBaudRate)

{
string baudRate;

Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
baudRate = Console.ReadLine();
 
if (baudRate == "")
{
_ baudRate = defaultPortBaudRate.ToString();

}
return int.Parse(baudRate);
}
Turbo_23 at 3-Jan-13 5:01am
   
Use int.TryParse to solve all the issues.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

use
string baudRate=string.empty;
istead of
string baudRate="";
  Permalink  
Comments
Arjunwalmiki at 4-Jan-13 5:18am
   
i am use string baudRate=string.empty; but same error

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 Jan 2013
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