Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Recieve Data From Serial Com Port C# In One Line And Convert To integer

ahmedsaahmedsa Posts: 5Member

Hi guys I have problem. I have code that takes data from serial com port for weighing machine, it's working okay, but receives data in more lines. I'm trying to make it receive data in one line.

In the code below I need to take weight from weigh bridge machine automatically by timer using Visual Studio 2005. It works, but I receive weight values in more lines. I need to receive weight in one line and automatically update and convert it to an integer. using System; using System.IO.Ports; using System.Windows.Forms; namespace CommSample { public partial class Form1 : Form { /// /// Initializes a new instance of the class. /// public Form1() { InitializeComponent(); } /// /// Handles the Idle event of the Application control. /// ///

The source of the event. /// The instance containing the event data. void Application_Idle(object sender, EventArgs e) { label3.Text = serialPort1.IsOpen ? "[Open]" : "[Closed]"; } /// /// Handles the Click event of the button1 control. /// /// The source of the event. /// The instance containing the event data. 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"); } } } /// /// Closes the port. /// private void ClosePort() { if (serialPort1.IsOpen) { serialPort1.DataReceived -= new SerialDataReceivedEventHandler(serialPort1_DataReceived); serialPort1.Close(); } } /// /// Handles the Click event of the closePortToolStripMenuItem control. /// /// The source of the event. /// The instance containing the event data. private void closePortToolStripMenuItem_Click(object sender, EventArgs e) { ClosePort(); } /// /// Handles the Click event of the exitToolStripMenuItem control. /// /// The source of the event. /// The instance containing the event data. private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } /// /// Handles the Load event of the Form1 control. /// /// The source of the event. /// The instance containing the event data. 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); } /// /// Opens the port. /// private void OpenPort() { serialPort1.Open(); serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived); } /// /// Handles the Click event of the openPortToolStripMenuItem control. /// /// The source of the event. /// The instance containing the event data. private void openPortToolStripMenuItem_Click(object sender, EventArgs e) { OpenPort(); } /// /// Handles the Click event of the optionsToolStripMenuItem control. /// /// The source of the event. /// The instance containing the event data. 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); } } } /// /// Handles the DataReceived event of the serialPort1 control. /// /// The source of the event. /// The instance containing the event data. void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (!InvokeRequired) { if (e.EventType == SerialData.Chars) { string portData = serialPort1.ReadExisting(); textBox2.AppendText(portData); } } 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(); } } } }
rar
rar
weight bridge code and output screenshoot.rar
337K
Tagged:
Sign In or Register to comment.