'Receive data from Serial weight scale into textbox using a button

I am trying to create a program that can take the current value that is on the scale, save it to a textbox with the click of a button. When the button is clicked it must save the value that is on the scale and await the next value. It must be saved vertically under each other. Currently the data just keeps streaming into the textbox and does not save the value when i click the button. Please help me solve this problem with some example code if you can. Help is much appreciated. The scale uses a serial to usb converter. The scale model is Micro A12EPicture of UI

using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;


namespace ScaleV5
{
    public partial class Form1 : Form
    {
        private SerialPort _serialPort;         //<-- declares a SerialPort Variable to be used throughout the form
        private const int BaudRate = 9600;      //<-- BaudRate Constant. 9600 seems to be the scale-units default value

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();     //<-- Reads all available comPorts
            foreach (var portName in portNames)
            {
                cboSelectPort.Items.Add(portName);                  //<-- Adds Ports to combo box
            }
            cboSelectPort.SelectedIndex = 0;                        //<-- Selects first entry (convenience purposes)
        }
         

        private delegate void Closure();
        private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
        {
            if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
                BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
            else
            {
                int dataLength = _serialPort.BytesToRead;
                byte[] data = new byte[dataLength];
                int nbrDataRead = _serialPort.Read(data, 0, dataLength);
                if (nbrDataRead == 0)
                    return;
                string str = System.Text.Encoding.UTF8.GetString(data);
                txbWeightList.Text = str.ToString();

            }
        }
        private void btnWeigh_Click(object sender, EventArgs e)
        {
            //<-- This block ensures that no exceptions happen
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();
            if (_serialPort != null)
                _serialPort.Dispose();
            //<-- End of Block

            _serialPort = new SerialPort(cboSelectPort.Text, BaudRate, Parity.None, 8, StopBits.One);       //<-- Creates new SerialPort using the name selected in the combo box
            _serialPort.DataReceived += SerialPortOnDataReceived;       //<-- this event happens every time when new data is received by the ComPort
            _serialPort.Open();     //<-- make the comport listen

            txbWeightList.Text += "Listening on " + _serialPort.PortName + "\r\n";
           

            Thread.Sleep(100);

            string response = _serialPort.ReadExisting();

           
            txbWeightList.Text += response;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            var excelApp = new Excel.Application();

            excelApp.Workbooks.Open(@"C:\ExcelTest\Test2.xlsx");
            Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
            workSheet.Cells[1, "A"] = txbWeightList.Text;
            workSheet.Cells[1, "B"] = txbWeightList.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txbWeightList.Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source