We help IT Professionals succeed at work.

How do I read the response from a serial device?

bigdave79
bigdave79 asked
on
Medium Priority
1,056 Views
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
B\n
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);
            try
            {
                Port.Open();
                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

Comment
Watch Question

Top Expert 2008

Commented:
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

Commented:
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 c#.net? Maybe that could help me determine when to grab the response.
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.