How do I read the response from a serial device?

Posted on 2009-02-18
Last Modified: 2013-11-07
I have a hardware device that will eventually control my application. Before I do that, I want to be able to control the settings on the device via simple commands.

A typical setting can be made by typing:
B1\n (where B is the setting, 1 is the value and \n is a new line to actually send the command)

This all works fine, but what I want to do is query what the setting is first before I set it. I'm able to do this via HyperTerminal by typing
this returns "=3" on the same line.. This is strange, as I told it to use a newline. It must be removing the newline and ammending "=3" to the "B" i just typed.

If I use SerialPort.ReadExisting() on the DataReceived event it returns simply "B". I'm sure it has something to do with the strange behaviour (above) but I dont know how I can grab the full output.
public bool Initialise()


            Port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

            Port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);




                return true;


            catch (IOException ex) { return false; }


        private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)


            string data = string.Empty;

            while (Port.BytesToRead > 0)


                data += Port.ReadExisting().Trim();



Open in new window

Question by:bigdave79
    LVL 12

    Expert Comment

    Can you try with Environment.NewLine rather than "\n"? If this doesn't work please send the full code, so that I can have a look in detail.

    Author Comment

    I've tried using Environment.NewLine, but it produces the same result.

    I guess to simplify my question, how do I read the response to a command sent to the serial device? Although it appears in HyperTerminal - should I expect the response of a command to appear in the buffer?

    Are there any good ways of grabbing everything thats being output from the device within Maybe that could help me determine when to grab the response.
    LVL 8

    Accepted Solution

    The hardware is probably just echoing the "B" and not the "B\n" than putting the "=3".  Rememeber you normally don't see what you type in hyperterminal unless something is echoing it.

    One way of doing something like this testing where you are not doing a specific protocol is to set the receive threshold to 1.

    like this:

    this.Port.ReceivedBytesThreshold = 1;

    This way the event is fired for every byte received.

    You then just read that byte an append it to your text box or whatever...
    private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                    myTextBox.Text += Port.ReadExisting();

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now