Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:47:25+00:00 2026-06-15T22:47:25+00:00

I have a serial port class, and I would like to control send/receive via

  • 0

I have a serial port class, and I would like to control send/receive via my GUI, and have the GUI update based on receipt of data from the serial port (or other events). So the two relevant classes are the serial class and the main window class.

I have the code below which compiles, but I get an exception when I try to run.

public class MySerThread
{
    public SerialPort serport;
    public event SerialDataReceivedEventHandler newSerData;

    public MySerThread()
    {
        serport = new SerialPort("COM1", 115200);
        serport.Open();
        serport.DataReceived += DataReceivedHandler;
    }

    public void DataReceivedHandler(object s, SerialDataReceivedEventArgs e)
    {
        byte[] data = new byte[serport.BytesToRead];
        serport.Read(data, 0, data.Length);

        // here's where I think I'm going wrong?
        if(newSerData != null)
            newSerData(s,e);
    }
}

And then in my GUI class…

public partial class MainWindow : Window
{
    MySerThread myPort;

    public MainWindow()
    {
        // Exception triggers here
        myPort.newSerData += DisplaySerDataHandler;
    }

    private void DisplaySerDataHandler(object sender, SerialDataReceivedEventArgs e)
    {
        this.ReceivedCallback(e);
    }

    private void ReceivedCallback(SerialDataReceivedEventArgs e)
    {
        if(this.someTextBlock.Dispatcher.CheckAccess())
        {
            this.UpdateTextBlock(e);
        }
        else
        {
            this.someTextBlock.Dispatcher.BeginInvoke(new Action<SerialDataReceivedEventArgs>(this.UpdateTextBlock), e);
        }
    }

    private void UpdateTextBlock(SerialDataReceivedEventArgs e)
    {
        someTextBlock.Text = "got new data";
    }
}

So, what am I doing wrong here? What is the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:47:26+00:00Added an answer on June 15, 2026 at 10:47 pm

    You can’t access myPort without creating an instance.

    MySerThread myPort = new MySerThread();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously have been reading NMEA data from a GPS via a serial port
I have a class that picks up data from the serial port and I
I would like to send and receive bytes over serial. My receiver should get
I'm trying to send data to an hplc pump via the serial port using
I have to monitor a serial port and process its data. As a test
I have a WPF test app for evaluating event-based serial port communication (vs. polling
i have a problem. i try to listen serial port(COM20), and then i receive
I'm using RXTX library to send some data to serial port. After sending data
I have created a library class CircuitController that communicates with a control circuit via
I have a need to send bytes through the serial port and wanted to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.