I have DataReceviedHandler, that is reading the data from the Rx terminal. I will like to change the status of the radioButton after getting Data at Rx (from the DataReceviedHandler) but I'm getting this error: "Cross-thread operation not valid: Control 'radioButton3' accessed from a thread other than the thread it was created on." what should i do to resolve?
Thanks, Oz
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_ser.GetType();
m_ser.PortName = "COM28";
m_ser.BaudRate = 115200;
m_ser.Open();
m_ser.ReadTimeout = 500;
m_ser.WriteTimeout = 500;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
{
byte[] m_rxUSBbuff = new byte[500];
SerialPort sptemp = (SerialPort)sender;
UInt16 rxbuffLength = (UInt16)m_ser.BytesToRead;
byte[] rxBuff = new byte[rxbuffLength];
try
{
rxbuffLength = (UInt16)sptemp.Read(rxBuff, 0, sptemp.BytesToRead);
}
catch (System.IO.IOException)
{
Console.WriteLine("Faild to Read from serial InputBuffer");
return;
}
int i;
int m_rxUSBLstMsgLength;
m_rxUSBLstMsgLength = (UInt16)rxbuffLength;
for (i = 0; i < m_rxUSBLstMsgLength; i++)
{
m_rxUSBbuff[i] = rxBuff[i];
}
switch (m_rxUSBbuff[0])
{
case 1:
radioButton1.Checked = true;
break;
case 2:
radioButton2.Checked = true;
break;
case 4:
radioButton3.Checked = true;
break;
case 8:
radioButton4.Checked = true;
break;
case 16:
radioButton5.Checked = true;
break;
default:
break;
}
}
private void CheckLeds_Click(object sender, EventArgs e)
{
byte[] txb = new byte[1] { 0xf2 };
m_ser.Write(txb, 0, 1);
}
}