private void commPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { lock (CommPortMonitor) { SerialPort LocalPort = (SerialPort)sender; if (LocalPort != null) { try { //read data waiting in the buffer int bytes = LocalPort.BytesToRead; if (bytes > 0) { byte[] commBuffer = new byte[bytes]; CommPort.Read(commBuffer, 0, bytes); //Convert byte received to floats & populate paket to send trought UDP byte[] prevRemainig; byte[] actualRemaning; List <float> receivedFloats = CommParserEncoder.ParseBytes(commBuffer, out prevRemainig, out actualRemaning); //Send packets trought the socket; List <byte[]> packetsSent = SendPackets(receivedFloats); if (this.ShowReceivedSerialValues.Checked) { //print floats on serial text box, in main thread with Action Delegate (best way to implement 'almost anonymous delegate' in .NET 2.0) this.BeginInvoke(new Action <List <float> >(PrintFloats), new object[] { receivedFloats }); } if (this.LogSerialValuesOnFile.Checked) { DebugLogger.LogPackets(receivedFloats, commBuffer, prevRemainig, actualRemaning, packetsSent); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "COM Port Receiving Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
private void udpReceivingHandler(object sender, byte[] udpDatagram) { //Create Easylab packets from UDP dataghram List <byte[]> packets = CommParserEncoder.EncodeBytes(udpDatagram); if (this.ShowReceivedUDPPackets.Checked) { //print UDP packet in UDP text box, in main thread with Action Delegate (best way to implement 'almost anonymous delegate' in .NET 2.0) this.BeginInvoke(new Action <List <byte[]> >(PrintUDP), new object[] { packets }); } if (this.LogUDPFloatsOnFile.Checked) { DebugLogger.LogUDPPackets(packets); } //Echo received bytes on Comm port SerialPort LocalPort = CommPort; if (LocalPort != null) { foreach (byte[] p in packets) { try { if (LocalPort.IsOpen) { CommPort.Write(p, 0, p.Length); } else { break; } } catch (Exception /*ex*/) { //MessageBox.Show(ex.Message, "UDP Receiving Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }