Recieve Data From Serial Com Port C# In One Line And Convert To int
Posted A minute ago

Hi guys i have problem i make code that take data from serial com port for weighting machine
it is ok working but it recieve data in more lines.I try to make it receive data in one line
In code below i need to take weight fro weight bridge machine automatically by timer in visual studio 2005.it works but receive weight values in more lines.
I need to receive weight in one line and automatically updated then convert t to integer

using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace CommSample
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }      

        /// <summary>
        /// Handles the Idle event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void Application_Idle(object sender, EventArgs e)
        {
            label3.Text = serialPort1.IsOpen ? "[Open]" : "[Closed]";
        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (pollingCheckbox.Checked)
            {
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
                TransmitCommand();
            }
        }

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

                }
            }
        }

        /// <summary>
        /// Closes the port.
        /// </summary>
        private void ClosePort()
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.DataReceived -= new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Close();
            }
        }

        /// <summary>
        /// Handles the Click event of the closePortToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClosePort();
        }

        /// <summary>
        /// Handles the Click event of the exitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Handles the Load event of the Form1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        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);
        }

        /// <summary>
        /// Opens the port.
        /// </summary>
        private void OpenPort()
        {
            serialPort1.Open();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
        }

        /// <summary>
        /// Handles the Click event of the openPortToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenPort();
        }

        /// <summary>
        /// Handles the Click event of the optionsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        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);
                }
            }
        }

        /// <summary>
        /// Handles the DataReceived event of the serialPort1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
        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 });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pollingCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (timer1.Enabled && !pollingCheckbox.Checked)
            {
                timer1.Enabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (pollingCheckbox.Checked)
            {
                TransmitCommand();
            }
        }

    }
}

Do you mean the function on line 154? What constitutes end-of-line? Does the machine sending the weights append '\n' or something to indicate end-of-line? You don't have any control over that, so if you want all the weights on one line then save them into your own string when received. Then on line 161 instead of appending each individual weight first get the text already in the textbox, append the new weight, erase the textbox, then finally put the new text back into the textbox.

This article has been dead for over six months. Start a new discussion instead.