private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = sender as SerialPort;
if (sp != null)
{
int bytes = sp.BytesToRead;
if (bytes > 0)
{
byte[] data = new byte[bytes];
sp.Read(data, 0, bytes);
if (activeResponse != null && !activeResponse.Complete)
{
if (activeResponse.Raw == null)
{
activeResponse.Raw = data;
}
else
{
byte[] new_data = new byte[data.Length + activeResponse.Raw.Length];
Array.Copy(activeResponse.Raw, 0, new_data, 0, activeResponse.Raw.Length);
Array.Copy(data, 0, new_data, activeResponse.Raw.Length, data.Length);
activeResponse.Raw = new_data;
}
if (activeResponse.Raw.Length > activeResponse.Length)
{
activeResponse.Result = (ResponseCode)activeResponse.Raw[activeResponse.Length];
activeResponse.Complete = true;
activeResponse.WaitEvent.Set();
}
}
}
}
}
#endregion