Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hello all i received the error Input string was not in a correct format.

C#
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

You might consider the following function:
C#
/// <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
 
Share this answer
 
v3
Comments
Arjunwalmiki 4-Jan-13 0:14am    
if i am using above code than i was not received any output
Andreas Gieriet 4-Jan-13 3:54am    
Output where?
Do you mean, the serial port does not work then?
Andi
Arjunwalmiki 4-Jan-13 4:43am    
serial port is working but not received any baudRate
Andreas Gieriet 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 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
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 )
 
Share this answer
 
Comments
Arjunwalmiki 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 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 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 3-Jan-13 4:13am    
can u write full code how can i use it
Christian Graus 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.
}
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()
 
Share this answer
 
Comments
Prasad_Kulkarni 3-Jan-13 1:42am    
I think Convert.ToInt32 will work as method is int.
Arjunwalmiki 3-Jan-13 2:01am    
if i am try this it is nor working same error
Convert.Tostring(baudRate)
prashant patil 4987 3-Jan-13 2:07am    
hey initialize baudRate = "0" and then try
Arjunwalmiki 3-Jan-13 2:16am    
i was set
string baudRate="0";

know error is
Positive number required.
Arjunwalmiki 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
hi,

in the top you define string baudRate="";, then you are again using this string in
C#
_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)
 
Share this answer
 
Comments
Arjunwalmiki 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);
}
[no name] 3-Jan-13 5:01am    
Use int.TryParse to solve all the issues.
use
C#
string baudRate=string.empty;

istead of
C#
string baudRate="";
 
Share this answer
 
Comments
Arjunwalmiki 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)



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