×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

serial port working and get result but not asyncronus by timer why

serial port working and get result but not asyncronus by timer why

serial port working and get result but not asyncronus by timer why

(OP)
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("SX" + "\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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close