Hello,
I am trying to write a Windows form program to listen to a COM port and intercept the data stream.
I have managed this, but I would now like to add the option to pause the listening.
I am using the CLOSE() option, but his then also makes my program terminate also.
Come someone please advise on what I am doing wrong, and what I can to pause the listening?
Thank you in advance.
Code snippet
private void Form1_Load(object sender, EventArgs e)
{
--- do some other stuff, then create com port
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
///# Open (above) port
mySerialPort.Open();
}
private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string output = sp.ReadTo("\r");
---- do some stuff with the data
}
private void stopListeningToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
mySerialPort.Close(); // <----------------------- Closes program. need it not to. Just Pause listening to port
}
catch (Exception ex)
{
Form1.instance.richTextBox1.AppendText(ex.Message.ToString() + Environment.NewLine); // If port already closed, then say so.
}
}
private void startListeningToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
mySerialPort.Open();
}
catch (Exception ex)
{
Form1.instance.richTextBox1.AppendText(ex.Message.ToString() + Environment.NewLine); // If port already open, then say so.
}
}
--- End Of Code snippet