I am using Visual Studio 2015 Community and am coding using C#.
I am receiving a string from a Serial-port. I then need to display 12 sub-strings from it and display them in real time in 12 different text-boxes.
The values are to be displayed only when I click the LOAD button in the Windows form that I have created.
I was not able to do it until I used a timer with a time interval of 2 seconds.
So, now I get the real time values in the text-boxes, but I am unable to click other buttons in my form.
I am badly stuck at this. Is there any way I can load real time values in the form, and still be able to click other buttons too?
Here is my code thus far:
using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
namespace Taskcontinue
{
public partial class Form1 : Form
{// defining a serial port in the code
SerialPort myport = new SerialPort();
int flag = 0;
string input = " "; public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//detect a * in string and then start extracting the sub-string as defined
private void Skeleton()
{
{
if(myport.IsOpen)
{
myport.Close();
}
myport.BaudRate = 9600;
myport.PortName = "COM9";
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.Open();
{
while (flag == 0)
{
input = "";
char data = Convert.ToChar(myport.ReadChar());
if (data == '*')
{
flag = 1;
input += data;
Debug.WriteLine("star is detected::");
}
}
if (flag == 1)
{
// Debug.WriteLine("WE RE INSIDE FLAG::");
for (int i = 0; i <= 129; i++)
{
// Debug.WriteLine("WE RE IN FOR LOOP::");
input += Convert.ToChar(myport.ReadChar());
}
realvalues();
flag = 0;
}
Debug.WriteLine(input);
}
}
}
//this is the button which on clicking should display the values in text boxes
private void load_values_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void myport_DataReceived(objectsender,SerialDataReceivedEventArgs e)
{
string data = Convert.ToChar(myport.ReadChar()).ToString();
}
//extracting substring from the string from serial port
private void realvalues()
{
tb1.Text = input.Substring(4, 7);
tb2.Text = input.Substring(14, 7);
tb3.Text = input.Substring(24, 7);
tb4.Text = input.Substring(34, 7);
tb5.Text = input.Substring(44, 7);
tb6.Text = input.Substring(54, 7);
tb7.Text = input.Substring(64, 7);
tb8.Text = input.Substring(75, 7);
tb9.Text = input.Substring(86, 7);
tb10.Text = input.Substring(97, 7);
tb11.Text = input.Substring(108, 7);
tb12.Text = input.Substring(123, 7);
}
private void timer1_Tick(object sender, EventArgs e)//timer tick event
{
timer1.Stop();
timer1.Start();
Skeleton();
}
}
}