bigdave79
asked on
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.
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();
}
}
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.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.