Macko
Macko

Reputation: 47

Serial Port Reading into TextBox

my eventual aim is to receive data and display it as both a graph and in individual text boxes (or something better!) the data is 2 x temperature readings and a humidity reading, for example the data sent would be "222160" etc.

However before I even tackle that I am having issues simply showing any data in a textbox. This is the code I am using; (Consists of a textbox for the UI)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace WFARxSimple
{
    public partial class Form1 : Form
    {
        string rxString;
        public Form1()
        {
            InitializeComponent();
            myport = new SerialPort();
            myport.PortName = "COM5";
            myport.BaudRate = 9600;
            myport.Parity = Parity.None;
            myport.DataBits = 8;
            myport.StopBits = StopBits.One;
            myport.Open();
        }

        private SerialPort myport;
        private void DisplayText(object sender, EventArgs e)
        {
            showRx.AppendText(rxString);
        }

        private void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            rxString = myport.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }
    }
}

However I cannot get any data to show in my textbox.

Upvotes: 0

Views: 2589

Answers (1)

glenebob
glenebob

Reputation: 1973

You need, at the very least: myPort.DataReceived += myPort_DataReceived;. Do this in Form1().

Upvotes: 4

Related Questions