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.
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.
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.
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.
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.
tuxduino:
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........
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.
To OP: you can't have two applications open the same com port at the same time. If you have the Arduino IDE running and serial monitor open, COM4 (or whatever it is) is locked. You have to close the serial monitor or probably quit the entire Arduino IDE before your C# app can open COM4.
This will make the "port locked" error go away.
If the program doesn't work, keeping the Arduino IDE open is not the solution.
I usually code in vb.net, but... I don't see where you initialize and start timer1, not where you associate timer1_Tick method with the timer1 tick event...