Can anyone tell my why the code below would be missing or dropping data. I have been googling all night but can't find any pointers. the serial port is using the following settings; 38400,n,8,1 xon/xoff flow control. ReceivedBytesThreshold = 1.
isReceiving and stockReceived are both members of the form
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string dataReceived = serialPort1.ReadExisting();
bytecount += dataReceived.Length;
processSerialData(dataReceived);
}
private void processSerialData(string dataReceived)
{
if (isReceiving == false)
{
int stxpos = dataReceived.IndexOf('\x02');
if (stxpos != -1)
{
dataReceived = dataReceived.Replace("\x02", "");
labelcaption = "Receiving... ";
this.Invoke(new EventHandler(SetLabel));
isReceiving = true;
}
}
int etxpos = dataReceived.IndexOf('\x03');
if (etxpos != -1)
{
dataReceived = dataReceived.Replace("\x03", "");
//tmpFile.Write(dataReceived);
writeToFile(dataReceived);
tmpFile.Close();
isReceiving = false;
stockReceived = true;
}
// Now we need to write the data to file
if (isReceiving == true)
{
if ((bytecount / recordSize) % 100 == 0)
{
labelcaption = "Receiving... " + (bytecount / recordSize);
this.Invoke(new EventHandler(SetLabel));
}
//tmpFile.Write(dataReceived);
writeToFile(dataReceived);
}
}