/// <summary>
        /// Default BrickConnection constructor
        /// </summary>
        /// <param name="config"></param>
        /// <param name="rbtqDataPort"></param>
        public BrickConnection(PowerControllerConfig config, RbtqDataPort rbtqDataPort, TrackRoamerBrickPowerService service)
        {
            this.PowerControllerConfig = config;       // keep the pointer
            this._rbtqDataPort         = rbtqDataPort; // where to send incoming data
            this._service = service;                   // used for tracing

            _service.LogInfoViaService("BrickConnection()");

            _controller = new ControllerRQAX2850_CCR(PowerControllerConfig.PortName);

            // wireup handlers for drive-related data - encoders and whiskers:
            _controller.onValueReceived_EncoderLeftAbsolute  += new OnValueReceived(m_controller_onValueReceived_EncoderLeftAbsolute);
            _controller.onValueReceived_EncoderRightAbsolute += new OnValueReceived(m_controller_onValueReceived_EncoderRightAbsolute);

            _controller.onValueReceived_EncoderSpeed += new OnValueReceived(m_controller_onValueReceived_EncoderSpeed);

            _controller.onValueReceived_DigitalInputF     += new OnValueReceived(m_controller_onValueReceived_DigitalInputF);       // WhiskerLeft
            _controller.onValueReceived_DigitalInputEmerg += new OnValueReceived(m_controller_onValueReceived_DigitalInputEmerg);   // WhiskerRight

            _controller.onValueReceived_DigitalInputE += new OnValueReceived(_controller_onValueReceived_DigitalInputE);

            // some measured values coming from the controller as result of queries:
            _controller.onValueReceived_Voltage             += new OnValueReceived(_controller_onValueReceived_Voltage);
            _controller.onValueReceived_MotorPower          += new OnValueReceived(_controller_onValueReceived_MotorPower);
            _controller.onValueReceived_MotorAmps           += new OnValueReceived(_controller_onValueReceived_MotorAmps);
            _controller.onValueReceived_AnalogInputs        += new OnValueReceived(_controller_onValueReceived_AnalogInputs);
            _controller.onValueReceived_HeatsinkTemperature += new OnValueReceived(_controller_onValueReceived_HeatsinkTemperature);

            HcConnected = false;
        }
        /// <summary>
        /// Open a RoboteQ AX2850 serial port.
        /// </summary>
        /// <param name="comPort"></param>
        /// <param name="baudRate"></param>
        /// <returns>true for success</returns>
        public bool Open(int comPort, int baudRate)
        {
            _comPort  = comPort;
            _baudRate = baudRate;

            _service.LogInfoViaService("BrickConnection: Open() port=" + comPort + "  baud=" + baudRate);

            if (_serialPort != null)
            {
                Close();    // will set  _serialPort=null  and remove event handler (serialPort_DataReceived)
            }

            if (!ValidBaudRate(baudRate))
            {
                throw new System.ArgumentException(Resources.InvalidBaudRate, "baudRate");
            }

            _serialPort             = new SerialPort("COM" + comPort.ToString(CultureInfo.InvariantCulture), baudRate, Parity.Even, 7, StopBits.One);
            _serialPort.Handshake   = Handshake.None;
            _serialPort.Encoding    = Encoding.ASCII;
            _serialPort.NewLine     = "\r";
            _serialPort.DtrEnable   = true;
            _serialPort.RtsEnable   = true;
            _serialPort.ReadTimeout = 1100;

            if (_rbtqDataPort == null)
            {
                _rbtqDataPort = new RbtqDataPort();
            }

            bool serialPortOpened = false;

            try
            {
                if (comPort > 0)
                {
                    if (TrySerialPort(_serialPort.PortName, baudRate))
                    {
                        // Open the port for communications
                        _serialPort.Open();
                        _serialPort.DiscardInBuffer();
                        _serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
                        serialPortOpened          = true;

                        _controller.serialPort = _serialPort;
                        _controller.init(); // serialPort must be set to avoid reopening in ensurePort()
                    }
                }
            }
            catch (Exception exc)
            {
                _service.LogInfoViaService("Error in BrickConnection: Open() port=" + comPort + "  baud=" + baudRate + " " + exc);

                if (serialPortOpened)
                {
                    serialPortOpened          = false;
                    _serialPort.DataReceived -= new SerialDataReceivedEventHandler(serialPort_DataReceived);
                }
            }

            return(serialPortOpened);
        }