ahmed sa

ahmed sa

  • NA
  • 289
  • 129.5k

serial port working get result but not asyncronus by timer

Apr 9 2014 1:02 AM
Can any one helping me in this code :
using System;
using System.IO.Ports;
using System.Windows.Forms;
using System.Threading;
using System.Text;

namespace CommSample
{
public partial class Form1 : Form
{
string portData = "";
public Form1()
{
InitializeComponent();
}

void Application_Idle(object sender, EventArgs e)
{
label3.Text = serialPort1.IsOpen ? "[Open]" : "[Closed]";
}

private void button1_Click(object sender, EventArgs e)
{
if (pollingCheckbox.Checked)
{
timer1.Enabled = true;
}
else
{
timer1.Enabled = false;
TransmitCommand();
}
}

private void TransmitCommand()
{
if (textBox1.Text.Length > 0)
{
if (serialPort1.IsOpen)
{
serialPort1.Write(textbox1.text + "\r");

}
}
}

private void ClosePort()
{
if (serialPort1.IsOpen)
{
TransmitCommand();
serialPort1.DataReceived -= new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Close();
}
}

private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
{
ClosePort();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}

private void Form1_Load(object sender, EventArgs e)
{
serialPort1.PortName = Properties.Settings.Default.Port;
serialPort1.BaudRate = Properties.Settings.Default.Baud;
serialPort1.DataBits = Properties.Settings.Default.DataBits;
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);

Application.Idle += new EventHandler(Application_Idle);
}

private void OpenPort()
{
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
timer1.Enabled = true;
}

private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenPort();
label4.Visible = true;
label5.Visible = true;
}

private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
ClosePort();

using (Form2 form = new Form2())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
serialPort1.PortName = Properties.Settings.Default.Port;
serialPort1.BaudRate = Properties.Settings.Default.Baud;
serialPort1.DataBits = Properties.Settings.Default.DataBits;
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);
}
}
}

void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string debugMessage = "";
float value;
if (!InvokeRequired)
{
if (e.EventType == SerialData.Chars)
{
portData += serialPort1.ReadExisting();
while (portData.Contains("\r"))
{
string temp = portData.Substring(0, portData.IndexOf('\r'));

foreach (byte x in Encoding.UTF8.GetBytes(portData))
{
debugMessage = debugMessage + x.ToString("x2");
}
Console.WriteLine("Debug : " + debugMessage);

portData = portData.Substring(portData.IndexOf('\r') + 1);
if (temp.Length == 12)
{
char status = temp[11];
switch (status)
{
case ' ':
if (float.TryParse(temp.Substring(1, 8).Trim(), out value))
{
//enter code here to process integer
textBox2.Text = temp.Substring(1, 8).Trim();
Console.WriteLine("Parse Number : {0}", textBox2.Text);
}
else
{
textBox2.Text = "Not Numeric";
Console.WriteLine("Cannot parse : {0}", portData);
}
break;
case 'I':
textBox2.Text = "Invalid";
Console.WriteLine("Invalid : {0}", portData);
break;
case 'M':
textBox2.Text = "In Motion";
Console.WriteLine("In Motion : {0}", portData);
break;
case 'O':
textBox2.Text = "Under/Over Range";
Console.WriteLine("Uneder/Over : {0}", portData);
break;
}
}
}
}
}

else
{
SerialDataReceivedEventHandler invoker = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
BeginInvoke(invoker, new object[] { sender, e });
}

}



private void pollingCheckbox_CheckedChanged(object sender, EventArgs e)
{
if (timer1.Enabled && !pollingCheckbox.Checked)
{
timer1.Enabled = false;
}
}

private void timer1_Tick(object sender, EventArgs e)
{
if (pollingCheckbox.Checked)
{
TransmitCommand();
}
}

}
}
Notes : textbox2 in multiline mode and my weight bridge weighting is ricelake IQ+355
This is code above working good his result is ok according to weight bridge value
but it is not asyncronusly
i make timer is enabled but also it is not asyncronusly meaning you need close application and open again to take the value of weight bridge
Now How i make code above asyncronusly working and changes when weight bridge in same time
thanks