7

I am getting some values from my Arduino over the serial port. The data has the format: "value1,value2,value3\r" as a string.

Example: "4.5,550.0,0.02\r"

I can recieve and separate the values but when I try to convert them to double I get the following exception

"input string was not in a correct format"`.

double Battery = 0, Voltage = 0, Current = 0;

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string data = serialPort1.ReadLine();
    string bv = data.Substring(0, data.IndexOf(",") - 1);
    data = data.Substring(data.IndexOf(",") + 1);
    string v0 = data.Substring(0, data.IndexOf(",") - 1);
    data = data.Substring(data.IndexOf(",") + 1);
    string i = data;

    double batteryVoltage = Convert.ToDouble(bv);
    double V0 = Convert.ToDouble(v0);
    double I = Convert.ToDouble(i);

    Battery = batteryVoltage;
    Voltage = V0;
    Current = I;

}
8
  • 2
    You've abused Substring quite a lot here, what is wrong with string.Split(',') and TryParse? Commented Sep 18, 2013 at 9:46
  • 1
    This code works with your sample data. Please, give data which causes error Commented Sep 18, 2013 at 9:48
  • Are you sure your input is 4.5,550.0,0.02\r and not 4.5,550.0,0.02\\r? Commented Sep 18, 2013 at 9:52
  • 1
    @user2790895 can you give us value of data when you get exception? Commented Sep 18, 2013 at 9:56
  • 1
    Since some people aren't aware of this - the sample data fails on certain regional settings, e.g. Polish uses "," as a decimal point and doesn't consider "4.5" a proper string to convert. Commented Sep 18, 2013 at 10:35

4 Answers 4

13

Two things are a problem here. First, the conversion may fail because of regional settings (invalid decimal point character), so use CultureInfo.InvariantCulture from System.Globalization namespace as an additional parameter. Second thing, you are cutting off one character from your substrings, so remove -1.

string data = "4.5,550.0,0.02\r";
string bv = data.Substring(0, data.IndexOf(","));
data = data.Substring(data.IndexOf(",") + 1);
string v0 = data.Substring(0, data.IndexOf(","));
data = data.Substring(data.IndexOf(",") + 1);
string i = data;

double batteryVoltage = Convert.ToDouble(bv, CultureInfo.InvariantCulture);
double V0 = Convert.ToDouble(v0, CultureInfo.InvariantCulture);
double I = Convert.ToDouble(i, CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

2 Comments

Good notice on cutting off last character. That probably will be the reason of error
Right, fixed my answer as it does mess with the conversion. Still, CultureInfo setting should be there if the user has a regional setting which doesn't recognize "." as a decimal point.
0

Possible the \r char is causing the issue. Try trimming it before processing.

Comments

0

Your code works for me with the provided example input. Can you elaborate on the exception?

Try specifying the CultureInfo in the conversion.

Also, you should use the String.Split() method:

string data = "4.5,550.0,0.02\r";
var strings = data.Split(',');
var doubles = strings.Select(s => Convert.ToDouble(s, CultureInfo.InvariantCulture)).ToList();

double batteryVoltage = doubles[0];
double V0 = doubles[1];
double I = doubles[2];

2 Comments

It should be a comment to question, not an answer to the reason of error
and it will also probably fail on 0.02\r
0

Try this to split your string

 string[] separotrs = { ",", "\r" };
 string[] result= aurdino.Split(separotrs, StringSplitOptions.RemoveEmptyEntries);

       double batteryVoltage = Convert.ToDouble(result[1]); //Sorry if order is incorrect:-)
       double v0 = Convert.ToDouble(result[0]);
       double I = Convert.ToDouble(result[2]);

One thing i do not get is where exactly are you getting error however your method will not fetch correct numbers(cutting off the last value) ,and you variable i (Current) will fetch 0.02\r which will be handled by Convert.ToDouble and you will get 0.02 as your Current value however other values will be wrong but i don't think you are getting error on this method as per your example.

See here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.