how to read data from serial port fastly(1 millisecond)
how to read data (at every 1 millisecond)from serial port in c# windows applications . i have read the the data from serial port Continuosly at that time not able to send data onto the serial port . at that time again connect the serial port then able to send the data. so at that time how to clear the serial port receive buffer(how to clear the data on port)
Please help me anyone
hear my receive function code
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] bLocalBuf = new byte[2];
int nLength = 0;
GlobVar.nLeng = GlobVar.TheMCU.serialPort1.BytesToRead;
if (GlobVar.nLeng > 0)
{
for (int s = 0; s < GlobVar.nLeng; s++)
{
nLength = GlobVar.TheMCU.serialPort1.Read(bLocalBuf, 0, 1);
if (nLength == 1)
{
if (bLocalBuf[0] == 0xc9 && GlobVar.nSOMFlag == 0)
{
GlobVar.nRindex = 0;
GlobVar.RxBufferData[GlobVar.nRindex++] = bLocalBuf[0];
GlobVar.nSOMFlag = 1;
}
else if (bLocalBuf[0] == 0xcf && GlobVar.nSOMFlag == 1)
{
Rx_Checksum(); // To compare the recv data Checksum //
GlobVar.RxBufferData[GlobVar.nRindex++] = bLocalBuf[0];
if ((GlobVar.RxBufferData[2] == 0x01)) Rx_LinkCheck();
//if ((GlobVar.RxBufferData[2] == 0x01) && (GlobVar.RxBufferData[4] == GlobVar.bRxChksum)) Rx_LinkCheck(); // read the link check data from serialport //
if ((GlobVar.RxBufferData[2] == 0x02) && (GlobVar.RxBufferData[4] == GlobVar.bRxChksum)) Rx_SelfTest(); // read the SelfTest data from serialport //
if ((GlobVar.RxBufferData[2] == 0x05) && (GlobVar.RxBufferData[17] == GlobVar.bRxChksum)) Rx_Read_ConfigurationData(); // read the Read_ConfigurationData from serialport //
if ((GlobVar.RxBufferData[2] == 0x07) && (GlobVar.RxBufferData[43] == GlobVar.bRxChksum)) DisplayFins_AchvdData();
if ((GlobVar.RxBufferData[2] == 0x08) && (GlobVar.RxBufferData[45] == GlobVar.bRxChksum))
{
// SendLogDataACK();
for (int i = 0; i < GlobVar.nRindex; i++)
{
GlobVar.bGraphData[GlobVar.nGraphDataCount] = GlobVar.RxBufferData[i];
GlobVar.nGraphDataCount++;
}
}
GlobVar.nSOMFlag = 0;
Array.Clear(GlobVar.RxBufferData, 0, GlobVar.RxBufferData.Length); // to clear the byte buffer //
Array.Clear(bLocalBuf, 0, bLocalBuf.Length); // to clear the byte buffer //
}
else if (GlobVar.nSOMFlag == 1)
{
if (GlobVar.nRindex > 47) { GlobVar.nRindex = 0; GlobVar.nSOMFlag = 0; }
GlobVar.RxBufferData[GlobVar.nRindex++] = bLocalBuf[0];
}
}
}
}
}
private void Rx_Checksum() // Recv data Checksum //
{
GlobVar.bRxChksum = 0;
for (int i = 1; i < GlobVar.nRindex - 2; i++)
{
GlobVar.bRxChksum = Convert.ToByte(GlobVar.bRxChksum ^ GlobVar.RxBufferData[i]);
GlobVar.bRxChksum = Convert.ToByte(GlobVar.bRxChksum & 0x7f);
}
}