COM Port Error when port is already streaming
I have an application that reads from a COM Port and then does something with the data that it receives. I am currently using a COM Port emulator (Since i don't have the d开发者_开发问答evice available for me to use) but I am feeding it a sample of the data. The program seems to work perfectly fine if I open the COMPort before I start transmitting data. However, if I start transmitting before I open the COMPort, and then I open the port, the dataReceived event is never fired and I am never able to get any data. I have even tried to flush the INBuffer as soon as I open the port but am unable to read from it.
My code to open the port is this:
public void setupComPort(string baudRate, string dataBits, string stopBits, string parity, string portName)
{
if (comPort.IsOpen)
comPort.Close();
comPort.BaudRate = int.Parse(baudRate);
comPort.DataBits = int.Parse(dataBits);
comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
comPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
comPort.PortName = portName;
// When data is recieved through the port, call this method
comPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
try
{
// Open the port
comPort.Open();
//If there's data in buffer, discard so we can start receiving
//comPort.DiscardInBuffer();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error Opening Port", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
Any help would be appreciated.
Its probably an issue with the emulator. I'll guess the issue will go away when you have the actual hardware. The only other thing I can think to try is setting the ReceivedBytesThreshold
to something other than the default (like 10 or something).
精彩评论