1

I am currently building a WPF application that receives and displays data from an Arduino using a SerialPort connection. I have managed to get the live data to display as it is received, however when the text reaches the bottom of the TextBlock the text stops. I would like to replace the old values with the new data coming in. Is this possible?

This is my code

public partial class MainWindow : Window
{
    SerialPort sp = new SerialPort();

    public MainWindow()
    {            
        InitializeComponent();
    }

    private void btnCon_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            String portname = txtCom.Text;
            sp.PortName = portname;
            sp.BaudRate = 9600;
            sp.DtrEnable = true;
            sp.Open();
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            txbStatus.Text = "Connected";
        }
        catch (Exception)
        {
            MessageBox.Show("Please enter a valid port number");
        }
    }

    private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        this.Dispatcher.Invoke(() =>
        {
            SerialPort sp = (SerialPort)sender;
            txbStatus.Text += sp.ReadExisting(); //Displaying data in TextBlock
        });
    }

Thanks

2 Answers2

1

Well the cheap way to do this would be to replace this:

txbStatus.Text += sp.ReadExisting(); //Displaying data in TextBlock

with this:

if (txbStatus.Text.Length > MAGIC_NUMBER) 
{
    txbStatus.Text = sp.ReadExisting(); //Replace existing content
}
else
{
    txbStatus.Text += sp.ReadExisting(); //Append content
}

This will append the text up to a certain point and replace it if it gets too long.

You will have to come up with MAGIC_NUMBER with trial and error based on the size of the text area, font size, volume of data, usability, etc.

Another approach:

var oldText = txbStatus.Text;
var newText = sp.ReadExisting();
var combinedText = oldText + newText;
var shortenedText = combinedText.Substring(combinedText.Length - MAXIMUM_LENGTH);
txbStatus.Text = shortenedText;

This will force the text to truncate at MAXIMUM_LENGTH, keeping only the newest of the text.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • this did partially resolve the issue as the values were being replaced. However I still had a small issue with this, the values were appearing on different lines when you switched from a four digit to a one digit number. Changing ReadExisting to ReadLine and removing the + resolved this. Thanks – white_reece Aug 17 '17 at 14:16
0

Just change

txbStatus.Text +=

to

txbStatus.Text =

EDIT in response to comment

You might want to use ReadLine instead, but make sure to set the newline character with SerialPort.NewLine. See also this question's replies.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Thanks for your answer. That did replace the values entered however it is now replacing the data before the whole value is received. For example 635 is now being displayed as 6 or 56. Any suggestions. – white_reece Aug 16 '17 at 19:25
  • @inspiro That fixed my issue, works perfectly now. Thanks for your help – white_reece Aug 16 '17 at 19:44