Hello guys. I am somewhat new to C# and .NET Framework. I am trying to make a program that sends data to device and receives it form a device using a serial port. First attempt was with a console app it worked fine for a concept. Now I am making a GUI app. So what I need is to send data to a device (I can do that) then wait for data to be received I am using SerialDataReceivedEvent, as dont know when will data come.
I have managed to display that data by using this.Invoke(new EventHandler()) and new method to do it here is that snippet.
I am not sure how does SerialDataReceived handler work. Is it like an interrupt routine in micro controllers and lets other part of the program run or is it just a method that pauses every thing.
I have been reading System.Io.Ports documentation and it seems that serial.Read() would be the best option because it waits for the data to be received if I am right.
I have managed to display that data by using this.Invoke(new EventHandler()) and new method to do it here is that snippet.
C#:
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] dataInHex = new byte[6];
string[] dataInHexSting = new string[6];
serialPort1.Read(dataInHex, 0, dataInHex.Length);
for(int i = 0; i < dataInHex.Length; i++)
{
dataInHexSting[i] = string.Format("{0:X2}", dataInHex[i]);
}
dataIn = string.Join(",", dataInHexSting);
this.Invoke(new EventHandler(ShowData));
}
private void ShowData(object sender, EventArgs e)
{
receive.Text = receive.Text + dataIn + " ";
}
I am not sure how does SerialDataReceived handler work. Is it like an interrupt routine in micro controllers and lets other part of the program run or is it just a method that pauses every thing.
I have been reading System.Io.Ports documentation and it seems that serial.Read() would be the best option because it waits for the data to be received if I am right.