コード例 #1
0
ファイル: DaqException.cs プロジェクト: hamishharvey/daqflex
 internal DaqException(string errorMessage, ErrorCodes errorCode)
     : base(errorMessage)
 {
     m_errorCode = errorCode;
     m_level = ErrorLevel.Error;
     m_lastResponse = null;
 }
コード例 #2
0
ファイル: DaqException.cs プロジェクト: hamishharvey/daqflex
 internal DaqException(DaqDevice device, string errorMessage, ErrorCodes errorCode, ErrorLevel level, DaqResponse lastResponse)
     : base(errorMessage)
 {
     m_errorCode = errorCode;
     m_level = level;
     m_lastResponse = lastResponse;
     m_inputScanStatus = device.DriverInterface.InputScanStatus.ToString();
     m_inputScanCount = device.DriverInterface.InputScanCount;
     m_inputScanIndex = device.DriverInterface.InputScanIndex;
     m_outputScanStatus = device.DriverInterface.OutputScanState.ToString();
     m_outputScanCount = device.DriverInterface.OutputScanCount;
     m_outputScanIndex = device.DriverInterface.OutputScanIndex;
 }
コード例 #3
0
        private void ReadPort()
        {
            try
            {
                // Read the DI port
                string message = "?DIO" + ChannelSpec + ":VALUE";
                Response = Device.SendMessage(message);
                int value = (int)Response.ToValue();
                valueLabel.Text = value.ToString();
                statusLabel.Text = String.Empty;

                if ((value & 1) != 0)
                    led0.BackColor = Color.LimeGreen;
                else
                    led0.BackColor = Color.Silver;

                if ((value & 2) != 0)
                    led1.BackColor = Color.LimeGreen;
                else
                    led1.BackColor = Color.Silver;

                if ((value & 4) != 0)
                    led2.BackColor = Color.LimeGreen;
                else
                    led2.BackColor = Color.Silver;

                if ((value & 8) != 0)
                    led3.BackColor = Color.LimeGreen;
                else
                    led3.BackColor = Color.Silver;

                if ((value & 16) != 0)
                    led4.BackColor = Color.LimeGreen;
                else
                    led4.BackColor = Color.Silver;

                if ((value & 32) != 0)
                    led5.BackColor = Color.LimeGreen;
                else
                    led5.BackColor = Color.Silver;

                if ((value & 64) != 0)
                    led6.BackColor = Color.LimeGreen;
                else
                    led6.BackColor = Color.Silver;

                if ((value & 128) != 0)
                    led7.BackColor = Color.LimeGreen;
                else
                    led7.BackColor = Color.Silver;

            }
            catch (Exception ex)
            {
                statusLabel.Text = ex.Message;
            }
        }
コード例 #4
0
ファイル: DaqDevice.cs プロジェクト: hamishharvey/daqflex
        //==============================================================================
        /// <summary>
        /// Creates an Exception object based on the error code
        /// </summary>
        /// <param name="errorCode">The error that occurred</param>
        /// <param name="response">The last response</param>
        /// <returns>The Exception</returns>
        //==============================================================================
        internal DaqException ResolveException(ErrorCodes errorCode, DaqResponse response)
        {
            DaqException daqException;

            string errorMessage = GetErrorMessage(errorCode);

            ErrorLevel level;

            if (errorCode < ErrorCodes.DeviceNotResponding)
                level = ErrorLevel.Warning;
            else
                level = ErrorLevel.Error;

            daqException = new DaqException(this, errorMessage, errorCode, level, response);

            return daqException;
        }
コード例 #5
0
ファイル: DaqDevice.cs プロジェクト: hamishharvey/daqflex
        //===========================================================================================
        /// <summary>
        /// Virtual method to preprocess a message
        /// </summary>
        /// <param name="message">The message to process</param>
        /// <returns>True if the message is to be sent to the device, otherwise false</returns>
        //===========================================================================================
        internal virtual bool PreprocessMessage(ref string message, string messageType)
        {
            SendMessageToDevice = true;

            if (message.Contains(Constants.EQUAL_SIGN) && message.Contains(Constants.QUERY.ToString())) {
                m_apiMessageError = ErrorCodes.InvalidMessage;
                SendMessageToDevice = false;
                return SendMessageToDevice;
            }

            // first check if an Ai Cal is in progress by the cal thread id
            // Cal is running if the cal thread id is non-zero
            if (Ai != null && Ai.CalThreadId != 0 && Thread.CurrentThread.ManagedThreadId != Ai.CalThreadId) {
                // if a calibration is running, then respond with device busy and
                // don't send the message to the device
                if (messageType != DaqComponents.AICAL && !message.Contains(DaqProperties.STATUS)) {
                    ApiResponse = new DaqResponse(PropertyValues.DEVICE_BUSY, double.NaN);
                    SendMessageToDevice = false;
                    return SendMessageToDevice;
                }
            }

            // next check if an Ao Cal is in progress by the cal thread id
            // Cal is running if the cal thread id is non-zero
            if (Ao != null && Ao.CalThreadId != 0 && Thread.CurrentThread.ManagedThreadId != Ao.CalThreadId) {
                // if a calibration is running, then respond with device busy and
                // don't send the message to the device
                if (messageType != DaqComponents.AOCAL && !message.Contains(DaqProperties.STATUS)) {
                    ApiResponse = new DaqResponse(PropertyValues.DEVICE_BUSY, double.NaN);
                    SendMessageToDevice = false;
                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.DEV) {
                if (message.Contains(APIMessages.DEVPID)) {
                    ApiResponse = new DaqResponse(APIMessages.DEVPID.Remove(0, 1) + Constants.EQUAL_SIGN + m_deviceID.ToString(), (double)m_deviceID);
                    SendMessageToDevice = false;
                    return SendMessageToDevice;
                }

                if (message.Contains(DaqCommands.LOADCAPS)) {
                    LoadDeviceCaps(true);

                    if (Ai != null)
                        Ai.Initialize();

                    if (Ao != null)
                        Ao.Initialize();

                    if (Dio != null)
                        Dio.Initialize();

                    if (Ctr != null)
                        Ctr.Initialize();

                    if (Tmr != null)
                        Tmr.Initialize();

                    ApiResponse = new DaqResponse(DaqComponents.DEV + ":" + DaqCommands.LOADCAPS, Double.NaN);

                    SendMessageToDevice = false;

                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.AI ||
                    messageType == DaqComponents.AISCAN ||
                        messageType == DaqComponents.AITRIG ||
                            messageType == DaqComponents.AICAL ||
                                messageType == DaqComponents.AIQUEUE) {
                if (Ai != null) {
                    m_apiMessageError = Ai.PreprocessMessage(ref message, messageType);

                    if (m_apiMessageError == ErrorCodes.NoErrors)
                        Ai.SetCriticalParams(message, messageType);

                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.AO || messageType == DaqComponents.AOSCAN || messageType == DaqComponents.AOCAL) {
                if (Ao != null) {
                    m_apiMessageError = Ao.PreprocessMessage(ref message, messageType);

                    if (m_apiMessageError == ErrorCodes.NoErrors)
                        Ao.SetCriticalParams(message, messageType);

                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.DIO) {
                if (Dio != null) {
                    m_apiMessageError = Dio.PreprocessMessage(ref message, messageType);

                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.CTR) {
                if (Ctr != null) {
                    m_apiMessageError = Ctr.PreprocessMessage(ref message, messageType);

                    return SendMessageToDevice;
                }
            }

            if (messageType == DaqComponents.TMR) {
                if (Tmr != null) {
                    m_apiMessageError = Tmr.PreprocessMessage(ref message, messageType);

                    return SendMessageToDevice;
                }
            }

            return true;
        }
コード例 #6
0
        private void OnTimerTick(object sender, EventArgs e)
        {
            try
            {
                string counter = counterComboBox.SelectedItem.ToString();

                // read the channel value
                string message;
                message = "?CTR{*}:VALUE";
                message = message.Replace("*", counter);
                Response = Device.SendMessage(message);
                responseTextBox.Text = Response.ToString();
                statusLabel.Text = String.Empty;
            }
            catch (Exception ex)
            {
                statusLabel.Text = ex.Message;
            }
        }
コード例 #7
0
        private void OnStartButtonClicked(object sender, EventArgs e)
        {
            timer1.Enabled = true;

            try
            {
                string counter = counterComboBox.SelectedItem.ToString();

                string message;

                message = "CTR{*}:START";
                message = message.Replace("*", counter);
                Device.SendMessage(message);

                // get an immediate reading
                message = "?CTR{*}:VALUE";
                message = message.Replace("*", counter);
                Response = Device.SendMessage(message);
                responseTextBox.Text = Response.ToString();
                statusLabel.Text = String.Empty;
            }
            catch (Exception ex)
            {
                statusLabel.Text = ex.Message;
            }
        }
コード例 #8
0
ファイル: DAQFlexAPI.cs プロジェクト: hamishharvey/daqflex
        //======================================================================
        /// <summary>
        /// Main MBD API to send a message to a device
        /// </summary>
        /// <param name="message">The message content</param>
        /// <returns>The device's response</returns>
        //======================================================================
        public DaqResponse SendMessage(string message)
        {
            try
            {
                Monitor.Enter(m_deviceLock);

                m_messagePending = true;

                DaqException dex;

                if (m_deviceReleased)
                {
                    dex = ResolveException(ErrorCodes.DeviceHasBeenReleased);
                    m_messagePending = false;
                    throw dex;
                }

                if (message == null || message == String.Empty)
                {
                    dex = ResolveException(ErrorCodes.MessageIsEmpty);
                    m_messagePending = false;
                    throw dex;
                }

                m_apiMessageError = ErrorCodes.NoErrors;

                //********************************************************
                // Convert the message to upper case
                //********************************************************
                string msg;

                if (!message.Contains(Constants.QUERY.ToString()) && message.Contains(DaqComponents.DEV) && message.Contains(DaqProperties.ID))
                {
                    // when the device ID is being set ("DEV:ID=myID"), don't convert the value of the ID
                    string id = MessageTranslator.GetPropertyValue(message);
                    msg = message.Remove(message.IndexOf(Constants.EQUAL_SIGN) + 1,
                                         message.Length - message.IndexOf(Constants.EQUAL_SIGN) - 1);
                    msg = msg.ToUpper();
                    msg += id;
                }
                else
                {
                    msg = message.ToUpper();
                }

                // remove leading and trailing white spaces
                msg = msg.Trim();

                //if (this is VirtualDevice && msg[0] == Constants.REFLECTOR_SYMBOL)
                //{
                //    return ((VirtualDevice)this).SendVirtualMessage(msg);
                //}

                //********************************************************
                // Check for a Device Reflection Message
                //********************************************************
                if (msg[0] == Constants.REFLECTOR_SYMBOL)
                {
                    // '@' denotes a Device Reflection message - let the reflector handle it then return
                    DaqResponse featureResponse = GetDeviceCapability(msg);
                    m_messagePending = false;

                    Monitor.Exit(m_deviceLock);

                    return featureResponse;
                }

                //********************************************************
                // Messages with a '>' are sent directly to the device
                //********************************************************
                if (msg[0] == Constants.DIRECT_ROUTING_SYMBOL)
                {
                    msg = msg.Substring(1);
                    ErrorCodes directErrorCode = ErrorCodes.NoErrors;
                    directErrorCode = SendMessageDirect(msg);

                    if (directErrorCode != ErrorCodes.NoErrors)
                    {
                        dex = ResolveException(directErrorCode);
                        m_messagePending = false;
                        throw dex;
                    }

                    string directResponse = m_driverInterface.ReadStringDirect();
                    double directValue = m_driverInterface.ReadValueDirect();

                    Monitor.Exit(m_deviceLock);

                    return new DaqResponse(directResponse, directValue);
                }

                //********************************************************
                // Get the component type this message pertains to
                //********************************************************
                string componentType = GetComponentType(msg);

                if (componentType == String.Empty)
                {
                    dex = ResolveException(ErrorCodes.InvalidComponentSpecified);
                    m_messagePending = false;
                    throw dex;
                }

                //********************************************************
                // Preprocess the message
                //********************************************************
                PreprocessMessage(ref msg, componentType);

                if (m_apiMessageError != ErrorCodes.NoErrors)
                {
                    if (m_apiMessageError == ErrorCodes.InvalidMessage)
                        dex = ResolveException(msg, m_apiMessageError);
                    else
                        dex = ResolveException(m_apiMessageError);

                    m_messagePending = false;
                    throw dex;
                }

                //********************************************************
                // Return API response for API-only messages
                //********************************************************
                if (!SendMessageToDevice)
                {
                    m_messagePending = false;
                    Monitor.Exit(m_deviceLock);

                    return m_apiResponse;
                }

                //********************************************************
                // Preprocess the data
                //********************************************************

                ErrorCodes result;

                if (msg.Contains("VALUE") && msg.Contains("="))
                {
                    result = PreprocessData(ref msg, componentType);

                    if (result != ErrorCodes.NoErrors)
                    {
                        dex = ResolveException(result);

                        m_messagePending = false;
                        throw dex;
                    }
                }

                 //********************************************************************************
                // Queue the device message in case the device configuration needs to be restored
                //********************************************************************************
                QueueDeviceMessage(msg);

                //********************************************************
                // Transfer the message to the driver interface
                //********************************************************

                byte[] messageBytes = new byte[Constants.MAX_COMMAND_LENGTH];

                // convert message to an array of bytes for the driver interface
                for (int i = 0; i < msg.Length; i++)
                    messageBytes[i] = (byte)msg[i];

                // let the driver interface transfer the message to the device
                result = m_driverInterface.TransferMessage(messageBytes);

                // retry once on device not responding
                if (result == ErrorCodes.DeviceNotResponding)
                    result = m_driverInterface.TransferMessage(messageBytes);

                // if there was an error throw an exception
                // the application needs to catch this
                if (result != ErrorCodes.NoErrors)
                {
                    if (result == ErrorCodes.InvalidMessage)
                    {
                        ErrorCodes ec = m_driverInterface.TransferMessage(m_devIdMessage);
                        if (ec != ErrorCodes.NoErrors)
                            result = ErrorCodes.DeviceNotResponding;
                    }

                    dex = ResolveException(msg, result);
                    m_messagePending = false;
                    throw dex;
                }

                //********************************************************
                // Read back the mesage response
                //********************************************************

                DaqResponse response = null;
                string responseText = m_driverInterface.ReadString();
                double value = m_driverInterface.ReadValue();

                // create the response object
                response = new DaqResponse(responseText, value);

                //********************************************************
                // Post process any data sent back in the response
                //********************************************************

                if (message.Contains(Constants.QUERY.ToString()))
                {
                    // PostProcessData is allowed to modify the response
                    result = PostProcessData(componentType, ref responseText, ref value);

                    // process the response before throwing an error or warning
                    responseText = AmendResponse(responseText);

                    // recreate the response in case responseText and value were modified
                    response = new DaqResponse(responseText, value);

                    if (result != ErrorCodes.NoErrors)
                    {
                        dex = ResolveException(result, response);
                        m_messagePending = false;
                        throw dex;
                    }
                }

                //********************************************************
                // Post process the message
                //********************************************************
                PostProcessMessage(ref message, componentType);

                if (m_updateRanges)
                    UpdateIoCompRanges();

                m_messagePending = false;
                Monitor.Exit(m_deviceLock);

                //********************************************************************************
                // Recalculate the actual max rate when the channel count changes
                //********************************************************************************
                if (msg.Contains("CHAN") && msg.Contains("="))
                {
                    RateCalculator.CalculateMaxAiScanRate(this);
                }

                System.Diagnostics.Debug.Assert(response != null);

                RestoreApiFlags(componentType);

                return response;
            }
            catch (DaqException dex)
            {
                Monitor.Exit(m_deviceLock);
                throw dex;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Assert(false, String.Format("{0} is not a DaqException", ex.Message));
                Monitor.Exit(m_deviceLock);
                throw ex;
            }
        }
コード例 #9
0
 private void OnTimerTick(object sender, EventArgs e)
 {
     try
     {
         // Read the AI channel
         string message = "?AI" + ChannelSpec + ":VALUE/VOLTS";
         Response = Device.SendMessage(message);
         responseTextBox.Text = Response.ToString();
         statusLabel.Text = String.Empty;
     }
     catch (Exception ex)
     {
         statusLabel.Text = ex.Message;
     }
 }