Go Down

Topic: Can't store the values I get from my temperature in the database. (Read 1 time) previous topic - next topic

zahidaoui

I am working on a project where I use the DS18B20 temperature sensor with Arduino to get the temperature, and then I store these temperatures in an Access database. Now I was able to get the temperature values from DS18B20 and show them using the Arduino software. But for the second part of my work, I did a windows form application that contains a code that is supposed to take these temperature values and store them in the database. but when I run this application, as soon as I press any of the buttons in it, I get the following error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: The port is closed."

This exception only appears when I have the arduino software running and getting the temperature values. When it's not running, nothing happens, and the buttons don't work. here is the code of my WFApplication.

Code: [Select]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace test1
{
    public partial class Form1 : Form
    {
        int j = 0;
        SerialPort sp1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sp1 = new SerialPort("COM4", 9600);
            sp1.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sp1.Write("a");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp1.Write("b");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string POT = sp1.ReadExisting();
            label1.Text = POT;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (j < 4)
                j++;
            sp1.Write(j.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (j > 0)
                j--;
            sp1.Write(j.ToString());
        }

    }
}


And for the record I am using C#. So what could the problem be? Thanks in advance.

zahidaoui

I am working on a project where I use the DS18B20 temperature sensor with Arduino to get the temperature, and then I store these temperatures in an Access database. Now I was able to get the temperature values from DS18B20 and show them using the Arduino software. But for the second part of my work, I did a windows form application that contains a code that is supposed to take these temperature values and store them in the database. but when I run this application, as soon as I press any of the buttons in it, I get the following error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: The port is closed."

This exception only appears when I have the arduino software running and getting the temperature values. When it's not running, nothing happens, and the buttons don't work. here is the code of my WFApplication.

Code: [Select]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace test1
{
    public partial class Form1 : Form
    {
        int j = 0;
        SerialPort sp1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sp1 = new SerialPort("COM4", 9600);
            sp1.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sp1.Write("a");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp1.Write("b");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string POT = sp1.ReadExisting();
            label1.Text = POT;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (j < 4)
                j++;
            sp1.Write(j.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (j > 0)
                j--;
            sp1.Write(j.ToString());
        }

    }
}


And for the record I am using C#. So what could the problem be? Thanks in advance.

tuxduino



PaulS

Do you know WHEN the Form1_Load event occurs? I'd add a button to the form, and open the serial port in the Click event for that button. A dropdown list to show the available ports and a list of speeds would be useful, too.

Then, of course, handle the exceptions.

robtillaart


you don't check the return values of any call (aka good weather programming)

Code: [Select]

sp1 = new SerialPort("COM4", 9600);
rv = sp1.open();


why do you assume   sp1 = new SerialPort("COM4", 9600);    returns a valid value?

Code: [Select]

sp1 = new SerialPort("COM4", 9600);

if (sp1 != null)

  try
  {
    rv = sp1.open();
  }
  catch()
  {
    // open failed ...
  }
}

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

#6
Dec 29, 2012, 04:53 pm Last Edit: Dec 29, 2012, 04:56 pm by robtillaart Reason: 1
  sp1 = new SerialPort("COM4", 9600);
  sp1.Open();

sp1.open() should be in a try catch construct as it can throw exceptions. ignoring them makes code less robust.



update: oops I cross-answered ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

tuxduino

A method I succesfully implemented in vb.net involves looking at this registry key:

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

(it's a folder, actually)

inside it you'll find "arduino" serial ports are those containing "VCP".

You can use a timer to check once in a second or two if a new "VCP" port has been added, and assume it's the one arduino is attached to.

NickPyner

I'm a newbie too, and I don't understand anything about your code, but this might be all you need

http://www.parallax.com/tabid/393/default.aspx

It isn't made for, but can be adapted to, Arduino. The vital serial commands look like this:

Code: [Select]
Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);

void loop() {
  running();
  GetClock();
  myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
Serial.print("DATA,TIME,       ");

  int ret=0;
  //get the values from the DS8B20's
  sensors.requestTemperatures();

  float InTemp = (sensorValue(InThermo));
  float OutTemp = (sensorValue(OutThermo)); 
  float DrainTemp = (sensorValue(DrainThermo));

  float diff = OutTemp - InTemp;

  datastreams[0].setFloat(InTemp);
  datastreams[1].setFloat(OutTemp);
  datastreams[2].setFloat(DrainTemp);
  datastreams[3].setFloat(diff);
  Serial.print(InTemp);
  Serial.print(" ,  ");
  Serial.print(OutTemp);
  Serial.print(" ,  ");
  Serial.print(DrainTemp);
  Serial.println(" ,  ");

  lcd.setCursor(49,0);
  lcd.print(InTemp);
  lcd.setCursor(49,1);

tuxduino

Nick, the OP has a problem with a C# program running on the PC, which gets data from Arduino. There's nothing a sketch can do here... :)

tuxduino

#10
Dec 30, 2012, 03:06 am Last Edit: Dec 30, 2012, 03:59 am by tuxduino Reason: 1

Yes I am using com4


Sorry, missed this reply before posting my note about com port discovery...

Nick Gammon

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator
http://www.gammon.com.au/electronics

NickPyner

#12
Dec 30, 2012, 07:57 am Last Edit: Dec 30, 2012, 08:04 am by NickPyner Reason: 1

Nick, the OP has a problem with a C# program running on the PC, which gets data from Arduino. There's nothing a sketch can do here... :)


OK. I said

Quote
this might be all you need


which still applies.  And, in this case, the solution actually is in how the Arduino exports the data.  


tuxduino



Nick, the OP has a problem with a C# program running on the PC, which gets data from Arduino. There's nothing a sketch can do here... :)

OK. I said
Quote
this might be all you need

which still applies.  And, in this case, the solution actually is in how the Arduino exports the data.  


The OP error is this (please reread the first post):

Quote
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: The port is closed."


This happens when the C# program tries to open COM4. At that point, how the data coming from Arduino is formatted is irrelevant.

:)

NickPyner

#14
Dec 31, 2012, 01:32 am Last Edit: Dec 31, 2012, 01:43 am by NickPyner Reason: 1


The OP error is this (please reread the first post):


Not necessarily.

It could be that the OP's real error is that he is taking the wrong approach, trying to re-invent the wheel, and ending up with a square one.  

Needless to say, he has every right to bash his head against a brick wall for the intellectual exercise, but there is no suggestion that that is his intent. The intent could be to actually solve the problem, and the problem is reasonably clear (please re-read the title of the post, large orange letters).  

I have no idea about what OP is trying to do in C# but, as far as this forum is concerned, he is like me, a newbie by definition, and both of us have been faced with the same problem. The principle difference between us is that I can send my temperatures to a database, and he can't.  Hence my post.

Hopefully OP will find it a useful approach, he might even find it a blessed relief........





Go Up