Michael
Michael

Reputation: 175

Convert string to double: input string was not in a correct format

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;

}

Upvotes: 7

Views: 16597

Answers (4)

Suraj Singh
Suraj Singh

Reputation: 4059

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

Upvotes: 0

Rik
Rik

Reputation: 29243

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];

Upvotes: 0

S_F
S_F

Reputation: 887

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);

Upvotes: 13

Caroline Aime
Caroline Aime

Reputation: 11

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

Upvotes: 0

Related Questions