public void SendCommand(byte[] cmd, StringReceivedHandler msgReceivedHandler)
        {
            if (Busy)
            {
                throw new Exception("Port busy. Cannot send command.");
            }
            _currentCmd = CommandType.All;
            var wasOpen = _port.IsOpen;

            _jsonHandler = json => {
                var str = json.Aggregate("", (c, el) => c + ("[" + el.Key + "] => " + el.Value + "\r\n"));
                msgReceivedHandler(str);
            };
            _okHandler     = result => { msgReceivedHandler("OK"); };
            _stringHandler = msgReceivedHandler;
            try {
                if (!wasOpen)
                {
                    OpenPort();
                }
                JsonReceived += _jsonHandler;
                OkReceived   += _okHandler;
                LineReceived += _stringHandler;
                _port.Write(cmd, 0, cmd.Length);
            }
            catch (Exception) {
                EndCommand(!wasOpen);
                throw;
            }
        }
        public void OkCommand(byte[] cmd, OkReceivedHandler msgReceivedHandler)
        {
            if (Busy)
            {
                throw new Exception("Port busy. Cannot send command.");
            }
            _currentCmd = CommandType.Ok;
            var wasOpen = _port.IsOpen;

            _okHandler = result => {
                msgReceivedHandler(result);
                EndCommand(!wasOpen);
            };
            try {
                if (!wasOpen)
                {
                    OpenPort();
                }
                OkReceived += _okHandler;
                _port.Write(cmd, 0, cmd.Length);
            }
            catch (Exception) {
                EndCommand(!wasOpen);
                throw;
            }
        }
 public void SendCommand(byte[] cmd, StringReceivedHandler msgReceivedHandler)
 {
     if (Busy) throw new Exception("Port busy. Cannot send command.");
     _currentCmd = CommandType.All;
     var wasOpen = _port.IsOpen;
     _jsonHandler = json => {
         var str = json.Aggregate("", (c, el) => c + ("[" + el.Key + "] => " + el.Value + "\r\n"));
         msgReceivedHandler(str);
     };
     _okHandler = result => { msgReceivedHandler("OK"); };
     _stringHandler = msgReceivedHandler;
     try {
         if (!wasOpen) OpenPort();
         JsonReceived += _jsonHandler;
         OkReceived += _okHandler;
         LineReceived += _stringHandler;
         _port.Write(cmd, 0, cmd.Length);
     }
     catch (Exception) {
         EndCommand(!wasOpen);
         throw;
     }
 }
 public void OkCommand(byte[] cmd, OkReceivedHandler msgReceivedHandler)
 {
     if (Busy) throw new Exception("Port busy. Cannot send command.");
     _currentCmd = CommandType.Ok;
     var wasOpen = _port.IsOpen;
     _okHandler = result => {
         msgReceivedHandler(result);
         EndCommand(!wasOpen);
     };
     try {
         if (!wasOpen) OpenPort();
         OkReceived += _okHandler;
         _port.Write(cmd, 0, cmd.Length);
     }
     catch (Exception) {
         EndCommand(!wasOpen);
         throw;
     }
 }