chandan kumar

chandan kumar

  • NA
  • 1
  • 1.2k

Serail Read and invoking textbox issue

Jan 30 2014 12:22 PM
Hi All,
I'm working on WPF forms. I'm using serial module to display data received on serial in text box.It works perfectly fine for most of the time and  in few instances i observed data getting overwritten and goes in Error condition . I think serial read is perfectly fine ,but invoking the test box to display data may taking time,in such case ,serial buffer data is getting overwritten, Not sure what could be exact reason.Why i suspect this because ,when i comment inovking textbox it works 
Please any one can check whether the invoking method is used in my code,let me know do i need to change anything?
Note:UI receives data from device for every 50ms .
private void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
{
int data = readOneByteFromUART();
//buffer data and then passed to below function
packet_received(data) 
}
packet_recieved(Serail packet)
{
switch(type):
{
case Error: 
/* some times it comes in this case ,always firt byte should be FF it receives different value in this case*/
case type1
textBoxErrMsg.Dispatcher.Invoke((Action)(() 
{
if (textBoxErrMsg.LineCount >100)
textBoxErrMsg.Text ="";
}));
char tmpBuff;
for (int i = 0; i < (packet.datasize); i++)
{
textBoxErrMsg.Dispatcher.Invoke((Action)(() 
{
tmpBuff=3D (char)packet.content[i];
textBoxErrMsg.Text +=3D tmpBuff;
blackfinMsgs_fs.Write(tmpBuff);
}));
}
break;
case type2:
telemtextBox1.Dispatcher.Invoke((Action)(() 
{
telemtextBox1.Text ="";
}));
for (int i = 0; i < packet.datasize; i++)
{
telemtextBox1.Dispatcher.Invoke((Action)(() 
{
telemtextBox1.Text += String.Format("0x{0=
,0:X} ", packet.content[i]);
}));
try
{
telemetry_fs.WriteByte(packet.content[i]);
}
catch ( Exception e)
{
}
break
}
private void readOneByteFromUART
{
while (_serialPort.IsOpen && (_serialPort == null || _serialPort.Bytes=
ToRead ==0))
System.Threading.Thread.Sleep(50);
if (_serialPort == null || !_serialPort.IsOpen)
{
if (!isConnected && ErrorReadWrite !=null)
ErrorReadWrite(true);
return 0;
}
return (byte)_serialPort.ReadByte();
}
Best Regards,
Chandan