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.

Go Up