[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

How do I read the response from a serial device?

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

0
bigdave79
Asked:
bigdave79
1 Solution
 
jandromedaCommented:
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.
0
 
bigdave79Author 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.
0
 
mac-willCommented:
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now