private byte[] Send(byte[] frame) { try { bool lockTaken = false; try { Monitor.Enter(comThreadSync, ref lockTaken); expectedCmd = (ControlProtocol.FrameCmds)frame[2]; } finally { if (lockTaken) { Monitor.Exit(comThreadSync); } } byte[] response = new byte[0]; serialPort.Write(frame, 0, frame.Length); bool flag = true; if (autoEvent.WaitOne(100)) { flag = false; response = rxFrame; } if (flag) { throw new Exception("Serial port " + serialPort.PortName + " timed out sending command " + expectedCmd.ToString()); } if (response.Length == 0 || response.Length != (((int)response[0] << 8) + (int)response[1])) { throw new Exception(string.Concat( "Error frame length (", response.Length.ToString(), ", expected ", (((int)response[0] << 8) + (int)response[1]).ToString(), ")" )); } if (expectedCmd != (ControlProtocol.FrameCmds)response[2]) { throw new Exception("Wrong command received (" + ((ControlProtocol.FrameCmds)response[2]).ToString() + ", expected " + expectedCmd.ToString() + ")"); } } catch (Exception ex) { Console.WriteLine((object)ex); return(null); } return(rxFrame); }
private byte[] Send(byte[] frame) { try { bool lockTaken = false; try { Monitor.Enter(comThreadSync, ref lockTaken); expectedCmd = (ControlProtocol.FrameCmds)frame[2]; } finally { if (lockTaken) Monitor.Exit(comThreadSync); } byte[] response = new byte[0]; serialPort.Write(frame, 0, frame.Length); bool flag = true; if (autoEvent.WaitOne(100)) { flag = false; response = rxFrame; } if (flag) throw new Exception("Serial port " + serialPort.PortName + " timed out sending command " + expectedCmd.ToString()); if (response.Length == 0 || response.Length != (((int)response[0] << 8) + (int)response[1])) { throw new Exception(string.Concat( "Error frame length (", response.Length.ToString(), ", expected ", (((int)response[0] << 8) + (int)response[1]).ToString(), ")" )); } if (expectedCmd != (ControlProtocol.FrameCmds)response[2]) throw new Exception("Wrong command received (" + ((ControlProtocol.FrameCmds)response[2]).ToString() + ", expected " + expectedCmd.ToString() + ")"); } catch (Exception ex) { Console.WriteLine((object)ex); return null; } return rxFrame; }