colinodowd
Member
- Joined
- Jan 23, 2020
- Messages
- 12
- Programming Experience
- 1-3
So I have a C# windows form application with a GUI that allows me to connect to an Arduino via a Bluetooth Serial connection and I want to turn the GUI background RED when the connection is broken.
I have created an ErrorHandler in my connectToArduino function but it does not seem to be working properly. When I pull the power on the Arduino, the GUI does not turn red!
Error Handler:
I have created an ErrorHandler in my connectToArduino function but it does not seem to be working properly. When I pull the power on the Arduino, the GUI does not turn red!
C#:
private void connectToArduino()
{
string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem);
portArduino = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
portArduino.RtsEnable = true;
portArduino.DtrEnable = true;
try
{
portArduino.Open();
isConnectedArduino = true;
}
catch (Exception e)
{
label8.Text = "Connection to Micro Failed. Try Again";
}
portArduino.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
portArduino.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorReceivedHandler);
button1.Text = "Disconnect";
enableControlsArduino();
}
Error Handler:
C#:
private void ErrorReceivedHandler(object sender, SerialErrorReceivedEventArgs e)
{
Invoke(new Action(() =>
{
this.BackColor = Color.Red;
}));
}