Using C# .NET
I've implemented serial com with MSComm. My application works with a
few errors. Most importantly, could someone tell me why the OnComm
event fires twice. Let me explain:
I have a data file that is sent across the serial port. The OnComm
event fires indicating activity. With a file of let's say 4 records.
Stepping through my app I noticed that in my com.Input variable, the
first and only the first record is visible. That record is processed,
but then the app continues to the same method (no while loop, just an
if) only this time the variable contains the remaining records in my
file.
Threshold is set to 75 wanting the event to fire at that number,
InputLen set to 0. I've included a snippet of code to help clarify.
Help me see what I'm missing.
FORM LOAD
private void Form1_Load(object sender, System.EventArgs e)
{
com.CommPort = 1;
if (com.PortOpen) com.PortOpen = false;
com.EOFEnable = true;
com.RThreshold = 75;
com.Settings = "9600,n,8,1";
com.DTREnable = true;
com.Handshaking = MSCommLib.HandshakeConstants.comNone;
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
com.InputLen = 0;
com.NullDiscard = false;
com.OnComm += new System.EventHandler(this.OnComm);
com.PortOpen = true;
}
ONCOMM EVENT
private void OnComm(object sender, System.EventArgs e)
{
if (com.InBufferCount > 0)
{
ProcessComData((string) com.Input);
}
}
PROCESS DATA ****** OCCURS TWICE *******
private void ProcessComData(string input)
{
lstData.Items.Add(input);
StreamWriter listwriter = new StreamWriter("C:\\Interrogator.txt",
false);
listwriter.Write(input);
listwriter.Close();
//Delay or breakpoint is necessary for accurate output.
MessageBox.Show ("New information available", "TITLE",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}