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 6537291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:57+00:00 2026-05-25T10:35:57+00:00

I’m writing a .Net app that will need to talk to a serial port

  • 0

I’m writing a .Net app that will need to talk to a serial port device. The device is basically a transmitter for some old school alphanumeric pagers. Occasionally, my app will need to open up a serial port and send a message to the transmitter.

I know the protocol for talking to the device. It’s a bit of a back and forth “chatty” protocol. Send a command … wait for a particular response … send another command … wait for another particular response … send the actual message … wait for an “accepted” response. I can get this to work with some really hacky code involving a series of Write(…) method calls on a SerialPort object, with Thread.Sleep calls in between.

Of course, I don’t want to actually do this by relying on Thread.Sleep to wait for the device to respond. It seems like the Reactive Extensions framework should be suited for this type of thing, but I’m having trouble getting my head around it. I started with this, but quickly got lost and wasn’t sure where to go next, or if this even makes sense:

var receivedData = Observable.FromEventPattern<SerialDataReceivedEventArgs>(serialPort, "DataReceived");
receivedData
    .Where(d => d.EventArgs.EventType == SerialData.Chars)
    .Subscribe(args =>
            {
                var response = serialPort.ReadExisting();
                // Now what?
            });

First, how do I kick this thing off with the first serialPort.Write() call? Then how do I chain them together by checking for the expected response before issuing the next Write() call? And of course if I don’t get the expected response I’d want to break out and throw an exception or something. Am I even barking up the right tree with Rx, or is there another pattern that would be better suited for this? Thanks!

  • 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-05-25T10:35:58+00:00Added an answer on May 25, 2026 at 10:35 am

    Rx is abstraction over “data source pushing data” scenarios. In your case you have modeled the serial port “read” method as Rx observable and this need to be combined with serial port write method. One possible solution would be something like below, although it may require some other modifications based on the specific needs of your application.

                var serialPort = new System.IO.Ports.SerialPort("COM1");
                serialPort.Open();
                var receivedData = Observable.FromEvent<SerialDataReceivedEventArgs>(serialPort, "DataReceived")
                                   .Where(d => d.EventArgs.EventType == SerialData.Chars)
                                   .Select(_ => serialPort.ReadExisting());
                var replay = new ReplaySubject<string>();
                receivedData.Subscribe(replay);
                var commands = (new List<string>() { "Init", "Hello", "Done" });
                commands.Select((s, i) =>
                {
                    serialPort.Write(s);
                    var read = replay.Skip(i).First();
                    //Validate s command against read response
                    return true;//To some value to indicate success or failure
                }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.