DS18B20 C# as a integer

Update: I got this to work, I will update the code to show what I did

Hello,

This is my first post, And I hope that i read the terms well enough. I tried searching for about 2 days and 8-10 hours to get a answer but nothing I have tried seems to work.

I have built a C# user form that has a Dundas Guage in it. This needs to have a INT in order to display right. I can populate a text box with the temperature from the arduino but for the life of me I cannot get the guage to populate. I beleive it has something to do with the conversion.

#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 3

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
 
  
//read temperature and output via serial
sensors.requestTemperatures();
Serial.println(sensors.getTempFByIndex(0));
}

Here is C# code section, I am unsure how to parse it into a int.

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line = serialPort1.ReadLine();
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}

private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox1.Text = line;
gaugeContainer1.Values["Default"].Value = 100;


}

I have editied this initial post to try to simplify the code to get the guage to display. Let me know if this has enough info to help

Int eh code i set the guage to 100 manually and it does work, now i just need to get the line from the arduino to convert to int

When I try to convert the string from arduino to a int i get an error.

If I tried compiling the Arduino code you posted, I'd get more than one. If I did, and I wanted you to help me resolve it, I think I'd tell you what it was.

DO NOT USE COPY FOR FORUM. EVER.

You get the temperature as a float. You send the float value to the serial port. Why? You claim that you want the value as an int.

Clearly, none of the commented out code is relevant to your problem. So, don't post it. DELETE IT!

Thank you for your reply, Your time means allot. I will clean up the code and repost it. I was not sure what uncommented may matter. I noticed after posting the code thing didn't work right. I am unsure of what you mean by never copy code in a forum. I have posted c# code in forum's before and it copied ok. I will try to remedy those problems first

Thanks again

I have edited my first post and I hope it has the info needed to help. I simplified the code and started over to make it as small as possible, once i get it working a this simple level i can add it to my larger project

Sorry for the first post being messy, hope it is fixed now

I think "copy for forum" is a menu option in the Arduino IDE.

...R

Still trying

when i use this code in C#

private void LineReceived(string line)
{

    try
    {

        int num = Convert.ToInt32(line);// sometimes conversion does not work
        textBox1.Text = line;
        gaugeContainer1.Values["Default"].Value = 100; //this line works just need to be set to int from the line
    }

    catch
    {
    // do nothing
    }

}

basically it never is able to convert to int. Does the signal from arduino really not a string?

I wanted to thank everyone who took the time to respond, I got it to work and will update the first post, I am unsure if i did it correctly but it is working

Thanks

Tim

basically it never is able to convert to int. Does the signal from arduino really not a string?

Yes, it is a string. But, the string contains something like "23.50", which Int32.Parse() is smart enough to recognize is NOT an int.

You have two choices. Store the float in an int, and Serial.print() the int OR Use Float.Parse() to parse the string as a float, and cast the result to an int.