C2.Flash.frmMain.port_DataReceived C# (CSharp) Method

port_DataReceived() private method

private port_DataReceived ( object sender, System.IO.Ports.SerialDataReceivedEventArgs e ) : void
sender object
e System.IO.Ports.SerialDataReceivedEventArgs
return void
		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