private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (Port.IsOpen && _streaming) { while (Port.BytesToRead > 5) { if (processCOM()) { switch (id) { case 0: APacket = (AccelDataPacket)ByteArrayToStructure(buffer, APacket); this.Invoke(new EventHandler(ProcessData)); break; default: this.BeginInvoke(new EventHandler(ReadError)); break; } } if (!Port.IsOpen || !_streaming) { break; } } } }
private void ProcessUDP() { this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText("Thread Started\n"); }); try { UDPServer = new UdpClient(receiveport); } catch (Exception e) { this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText(e.ToString() + "\n"); }); timer1.Stop(); return; } UDPServer.Client.ReceiveTimeout = 1000; while (!_shouldStop) { try { //receive is blocking byte[] data = UDPServer.Receive(ref ACCELEP); if (data == null || data.Length == 0) { this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText("\nReceive thread timed out\n"); }); } else { //this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText(ByteToHexBitFiddle(data) + "\n"); }); byte len, id; len = data[2]; id = data[3]; byte[] temp = new byte[len]; switch (id) { case 0: //accel packet Array.Copy(data, 3, temp, 0, len); APacket = (AccelDataPacket)ByteArrayToStructure(temp, APacket); //this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText(ByteToHexBitFiddle(temp) + "\n"); }); this.Invoke(new EventHandler(ProcessData)); break; } //this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText(string.Format("{0} {1}\n", len, id)); }); } } catch { //this.Invoke((MethodInvoker)delegate { DebugTextBox.AppendText("\nReceive thread timed out\n"); }); } } timer1.Stop(); UDPServer.Close(); this.Invoke((MethodInvoker)delegate { RawTextBox.AppendText("Thread Closed\n"); }); }