i have a device that receive data. i want to send this data to PC to show in data grid view . how can i wait for serial port to completely receive data ?. i have code dosen't work. ( "displayvalue" is a void convert byte to value )
serialPort.ReadTimeout = 200;
byte[] byteToReceiv = new byte[serialPort.BytesToRead];
int count = serialPort.BytesToRead;
var offset = 0;
//Thread.Sleep(1000);
while (count > 0)
{
var readCount = serialPort.Read(byteToReceiv, offset, count);
offset = readCount;
count -= readCount;
}
textBox1.Text = displayValue(byteToReceiv);
CodePudding user response:
The SerialPort class has a DataReceived event which fires when data has been completely received. I would advise you to use it instead of using a timeout in this case. You can find an example for the same in Microsoft's docs : https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=dotnet-plat-ext-6.0