Reputation: 51
I'm writing a sequence to a serial port telling it to send me data. After sending the sequence and trying to read the data from the serial port there is nothing there. However, if I close the program and open the port in HyperTerminal, the data immediately shows up without needed to prompt for it.
private string ReadSerialPort()
{
var _comPort = new SerialPort()
{
PortName = "COM2",
BaudRate = 1200,
DataBits = 7,
Parity = System.IO.Ports.Parity.Odd,
StopBits = System.IO.Ports.StopBits.One,
NewLine = Environment.NewLine,
WriteTimeout = 500,
ReadTimeout = 500,
Handshake = System.IO.Ports.Handshake.None
};
if (!_comPort.IsOpen)
{
_comPort.Open();
}
int unicode = 27;
char character = (char)unicode;
string text = character + "P";
_comPort.WriteLine(text);
//System.Threading.Thread.Sleep(200); // Didn't help
string serialData = string.Empty;
while (serialData.Length == 0)
{
serialData = _comPort.ReadLine();
}
return serialData;
}
If I write the escape sequence to the serial port 3 times, I will see nothing in my code but if I open up HyperTerminal it will appear 3 times.
What could be causing the code to not see anything in the buffer, but I can see it once I exit the program?
Upvotes: 0
Views: 979
Reputation: 51
My solution was to set DtrEnable = true
. Everything is now working as intended!
This question helped lead me to the solution
Upvotes: 2