firstoption
Member
- Joined
- Apr 23, 2014
- Messages
- 10
- Programming Experience
- Beginner
Good day to all,
Please I need your guide and support on how I can stop my GUI from freezing. I have 12 buttons, 6 trackbars and a checkbox on my GUI. The 12 buttons are used to turn ON/OFF LEDs while I am using the 6 trackbar to control the brightness of the Leds. The checkbox is used to receive DATA from the microcontroller whenever it is checked.
I have two functions in my microcontroller. One function sends DATA to the GUI while the second function receives DATA from the GUI. The problem I am having is that I can not run the two functions at the same time in my main function in the microcontroller. If I run the two functions at the same time the GUI freezes. However if I run a single function, everything will work fine. Below are sample of the functions:
-------------------------------------------------------------------------------------
----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
Below IS MY C# CODE
------------------------------------------------------------------------------
You advice and suggestions will be highly appreciate.Once again thank you for support
Please I need your guide and support on how I can stop my GUI from freezing. I have 12 buttons, 6 trackbars and a checkbox on my GUI. The 12 buttons are used to turn ON/OFF LEDs while I am using the 6 trackbar to control the brightness of the Leds. The checkbox is used to receive DATA from the microcontroller whenever it is checked.
I have two functions in my microcontroller. One function sends DATA to the GUI while the second function receives DATA from the GUI. The problem I am having is that I can not run the two functions at the same time in my main function in the microcontroller. If I run the two functions at the same time the GUI freezes. However if I run a single function, everything will work fine. Below are sample of the functions:
C#:
int main(void)
{
DDRB |= 1 <<PINB0; // PIN B3 Ausgang Clock Out
DDRD |= 1 <<PIND6; // PIN D6 Ausgang Data Out
InitRegister();
uart_init (95);
SetRegister_1(0,0,0,0,0,0);
while(1)
{
_delay_ms(1000);
uart_transmit (temp); // this function sends Data to the GUI and everything worked fine
temp++;
}
}//
C#:
int main(void)
{
DDRB |= 1 <<PINB0; // PIN B3 Ausgang Clock Out
DDRD |= 1 <<PIND6; // PIN D6 Ausgang Data Out
InitRegister();
uart_init (95);
SetRegister_1(0,0,0,0,0,0);
while(1)
{
_delay_ms(1000);
UARTReceiver(); // this function receives Data from the GUI and everything worked fine
temp++;
}
}//
C#:
int main(void)
{
DDRB |= 1 <<PINB0; // PIN B3 Ausgang Clock Out
DDRD |= 1 <<PIND6; // PIN D6 Ausgang Data Out
InitRegister();
uart_init (95);
SetRegister_1(0,0,0,0,0,0);
//when i put the two functions in the loop at the same time like in the arrangement below,the GUI freezed
while(1)
{
UARTReceiver();
_delay_ms(1000);
UARTReceiver();
temp++;
}
}//
Below IS MY C# CODE
------------------------------------------------------------------------------
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using FTD2XX_NET;
namespace Led_Temp_Control_Interface
{
public partial class Form1 : Form
{
byte[] data = new byte[100];
byte[] tempvalue = new byte[10];
int LastValue1, LastValue2, LastValue3, LastValue4, LastValue5, LastValue6;
UInt32 ftdiDeviceCount = 0;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
// Create new instance of the FTDI device class
FTDI myFtdiDevice = new FTDI();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Determine the number of FTDI devices connected to the machine
ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
// Allocate storage for device info list
FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
// Populate our device list
ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
// Set up device data parameters
// Set Baud rate to 9600
ftStatus = myFtdiDevice.SetBaudRate(9600);
// Set data characteristics - Data bits, Stop bits, Parity
ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
// Set flow control - set RTS/CTS flow control
ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
// Set read timeout to 5 seconds, write timeout to infinite
ftStatus = myFtdiDevice.SetTimeouts(5000, 0);
// Open first device in our list by serial number
ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
label1.Text = "Ger?t Nicht Verbinden";
}
else
{
label1.Text = " Ger?t Verbunden";
}
}
private void button2_Click(object sender, EventArgs e)
{
myFtdiDevice.Close();
if (myFtdiDevice.IsOpen)
{
label1.Text = "Ger?t Verbunden";
}
else
{
label1.Text = "Ger?t Getrennt";
}
}
private void button3_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 1;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 1;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button4_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 1;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button5_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 2;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 2;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button6_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 2;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button7_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 3;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 3;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button8_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 3;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button9_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 4;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 4;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button10_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 4;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button11_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 5;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 5;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button12_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 5;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button13_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 6;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 6;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void button14_Click(object sender, EventArgs e)
{
UInt32 numBytesWritten = 0;
data[0] = 6;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 40;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
data[0] = 0x6A;
myFtdiDevice.Write(data, 1, ref numBytesWritten);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
LastValue1 = trackBar1.Value;
label2.Text = trackBar1.Value + "%";
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
LastValue2 = trackBar2.Value;
label3.Text = trackBar2.Value + "%";
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
LastValue3 = trackBar3.Value;
label4.Text = trackBar3.Value + "%";
}
private void trackBar4_Scroll(object sender, EventArgs e)
{
LastValue4 = trackBar4.Value;
label5.Text = trackBar4.Value + "%";
}
private void trackBar5_Scroll(object sender, EventArgs e)
{
LastValue5 = trackBar5.Value;
label6.Text = trackBar5.Value + "%";
}
private void trackBar6_Scroll(object sender, EventArgs e)
{
LastValue6 = trackBar6.Value;
label7.Text = trackBar6.Value + "%";
}
private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue1 == trackBar1.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#0";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar1.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void trackBar2_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue2 == trackBar2.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#1";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar2.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void trackBar3_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue3 == trackBar3.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#2";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar3.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void trackBar4_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue4 == trackBar4.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#3";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar4.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void trackBar5_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue5 == trackBar5.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#4";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar5.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void trackBar6_MouseUp(object sender, MouseEventArgs e)
{
if (LastValue6 == trackBar6.Value)
{
UInt32 numBytesWritten = 1;
string dataToWrite = "#5";
myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
data[0] = Convert.ToByte(trackBar6.Value);
data[1] = 0x6A;
myFtdiDevice.Write(data, 2, ref numBytesWritten);
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked && !this.backgroundWorker1.IsBusy)
{
this.backgroundWorker1.RunWorkerAsync();
}
else if (!this.checkBox1.Checked && this.backgroundWorker1.IsBusy)
{
this.backgroundWorker1.CancelAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
UInt32 numBytesRead = 0;
UInt32 numBytesToRead = 1;
byte[] readData = new byte[10];
while (!this.backgroundWorker1.CancellationPending)
{
// Do some work.
Thread.Sleep(1000);
ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
// Update the UI.
this.backgroundWorker1.ReportProgress(0, readData[0].ToString());
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label11.Text = (string)e.UserState + "?C";
}
}
}
You advice and suggestions will be highly appreciate.Once again thank you for support