コード例 #1
0
        public static string SendPassThroughCommand(string message, string terminator)
        {
            lock (lockObject) {
                tl.LogMessage("OAT Server", "Lock Object");

                try {
                    if (SharedSerial.Connected && !String.IsNullOrEmpty(message))
                    {
                        tl.LogMessage("Telescope", "Send message: " + message);
                        SharedSerial.ClearBuffers();
                        SharedSerial.Transmit(message);
                        if (!string.IsNullOrEmpty(terminator))
                        {
                            return(SharedSerial.ReceiveTerminated(terminator.ToString()));
                        }
                        return(string.Empty);
                    }
                    else
                    {
                        tl.LogMessage("OAT Server", "Not connected or Empty Message: " + message);
                        return("");
                    }
                }
                catch (Exception e) {
                    tl.LogMessage($"Caught exception while SendPassThroughCommand - ${message}", e.Message);
                    return("");
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Example of a shared SendMessage method, the lock
        /// prevents different drivers tripping over one another.
        /// It needs error handling and assumes that the message will be sent unchanged
        /// and that the reply will always be terminated by a "#" character.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        ///

        public static string SendMessage(string message)
        //  public static string SendMessage(string function, string message)
        {
            lock (lockObject)
            {
                //   tl.LogMessage("scopefocusServer", "Lockl Object");

                string msg = message + "#";  // adds # to all messages, removed #'s from the indivual commands
                if (SharedSerial.Connected && !String.IsNullOrEmpty(msg))
                {
                    tl.LogMessage("Controller", "Send Msg: " + msg);
                    SharedSerial.ClearBuffers();
                    SharedSerial.Transmit(msg);
                    string strRec = SharedSerial.ReceiveTerminated("#");
                    SharedSerial.ClearBuffers();
                    tl.LogMessage("Controller", "Response Msg: " + strRec);
                    return(strRec);
                }
                else
                {
                    tl.LogMessage("Controller", "Not Connected or Empty Send Msg: " + message);
                    return("");
                }
                //SharedSerial.Transmit(message);
                //// TODO replace this with your requirements
                //return SharedSerial.ReceiveTerminated("#");
            }
        }
コード例 #3
0
        public static string SendSerialMessage(string message)
        {
            string retval = String.Empty;

            if (SharedSerial.Connected)
            {
                lock (lockObject)
                {
                    SharedSerial.ClearBuffers();
                    SharedSerial.Transmit(CMD_START + message + CMD_END);
                    //LogMessage("SharedResources::SendSerialMessage", "Message: {0}", CMD_START + message + CMD_END);

                    try
                    {
                        retval = SharedSerial.ReceiveTerminated(CMD_END).Replace(CMD_END, String.Empty);
                        //LogMessage("SharedResources::SendSerialMessage", "Message received: {0}", retval);
                    }
                    catch (Exception)
                    {
                        LogMessage("SharedResources::SendSerialMessage", "Serial timeout exception while receiving data");
                    }

                    LogMessage("SharedResources::SendSerialMessage", "Message sent: {0} received: {1}", CMD_START + message + CMD_END, retval);
                }
            }
            else
            {
                //throw new NotConnectedException("SendSerialMessage");
                LogMessage("SharedResources::SendSerialMessage", "NotConnectedException");
            }

            return(retval);
        }
コード例 #4
0
        /// <summary>
        /// A shared SendMessage method, the lock prevents different drivers tripping over one another.
        /// The message passed should end in one of these endings:
        ///	 #   - the command does not expect a reply
        ///	 #,n - the command expect a single numerical digit (always 0 or 1)
        ///	 #,# - the command expects a string reply that ends in a #
        ///	 It is very important to send the right variation since otherwise the protocol will become confused.
        /// </summary>
        public static string SendMessage(string message)
        {
            string retVal = string.Empty;

            lock (lockObject)
            {
                tl.LogMessage("OAT Server", "Locked Serial");

                ExpectedAnswer expect = ExpectedAnswer.None;

                if (message.EndsWith("#,#"))
                {
                    message = message.Substring(0, message.Length - 2);
                    expect  = ExpectedAnswer.HashTerminated;
                }
                else if (message.EndsWith("#,n"))
                {
                    message = message.Substring(0, message.Length - 2);
                    expect  = ExpectedAnswer.Digit;
                }
                else if (!message.EndsWith("#"))
                {
                    message += "#";
                }

                if (SharedSerial.Connected && !String.IsNullOrEmpty(message))
                {
                    tl.LogMessage("OAT Server", "Send message (expected reply : " + expect.ToString() + ") : " + message);
                    SharedSerial.ClearBuffers();
                    SharedSerial.Transmit(message);
                    switch (expect)
                    {
                    case ExpectedAnswer.Digit:
                        tl.LogMessage("OAT Server", "Wait for number reply");
                        retVal = SharedSerial.ReceiveCounted(1);
                        break;

                    case ExpectedAnswer.HashTerminated:
                        tl.LogMessage("OAT Server", "Wait for string reply");
                        retVal = SharedSerial.ReceiveTerminated("#");
                        tl.LogMessage("OAT Server", "Raw reply :" + retVal);
                        retVal = retVal.TrimEnd('#');
                        break;
                    }

                    tl.LogMessage("OAT Server", "Reply: " + retVal);
                }
                else
                {
                    tl.LogMessage("OAT Server", "Not connected or Empty Message: " + message);
                }
            }

            tl.LogMessage("OAT Server", "Unlocked Serial");
            return(retVal);
        }
コード例 #5
0
        /// <summary>
        /// Example of a shared SendMessage method, the lock
        /// prevents different drivers tripping over one another.
        /// It needs error handling and assumes that the message will be sent unchanged
        /// and that the reply will always be terminated by a "#" character.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private static string SendMessage(string message, Boolean reply)
        {
            lock (lockObject) {
                tl.LogMessage("OAT Server", "Lock Object");
                if (!message.EndsWith("#"))
                {
                    message += "#";
                }

                if (SharedSerial.Connected && !String.IsNullOrEmpty(message))
                {
                    tl.LogMessage("Telescope", "Send message: " + message);
                    SharedSerial.ClearBuffers();
                    SharedSerial.Transmit(message);
                    if (reply)
                    {
                        string retVal;
                        string cmdGroup = message.Substring(1, 1);
                        switch (cmdGroup)
                        {
                        case "S":
                        case "M":
                        case "h":
                        case "Q":
                            retVal = SharedSerial.ReceiveCounted(1);
                            break;

                        default:
                            retVal = SharedSerial.ReceiveTerminated("#");
                            retVal = retVal.Replace("#", "");
                            break;
                        }

                        return(retVal);
                    }
                    else
                    {
                        return("");
                    }
                }
                else
                {
                    tl.LogMessage("OAT Server", "Not connected or Empty Message: " + message);
                    return("");
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// A shared SendMessage method, the lock prevents different drivers tripping over one another.
        /// The message passed should end in one of these endings:
        ///	 #   - the command does not expect a reply
        ///	 #,n - the command expect a single numerical digit (always 0 or 1)
        ///	 #,# - the command expects a string reply that ends in a #
        ///	 It is very important to send the right variation since otherwise the protocol will become confused.
        /// </summary>
        public static string SendMessage(string message)
        {
            string retVal    = string.Empty;
            long   messageNr = Interlocked.Increment(ref messageNumber);

            LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} > ({message}) - awaiting lock");
            lock (lockObject)
            {
                LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - acquired lock");

                ExpectedAnswer expect = ExpectedAnswer.None;

                if (message.EndsWith("#,#"))
                {
                    message = message.Substring(0, message.Length - 2);
                    expect  = ExpectedAnswer.HashTerminated;
                }
                else if (message.EndsWith("#,##"))
                {
                    message = message.Substring(0, message.Length - 3);
                    expect  = ExpectedAnswer.DoubleHashTerminated;
                }
                else if (message.EndsWith("#,n"))
                {
                    message = message.Substring(0, message.Length - 2);
                    expect  = ExpectedAnswer.Digit;
                }
                else if (!message.EndsWith("#"))
                {
                    message += "#";
                }

                if (SharedSerial.Connected && !String.IsNullOrEmpty(message))
                {
                    LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Sending command, expecting {expect} reply for '{message}'");
                    SharedSerial.ClearBuffers();
                    SharedSerial.Transmit(message);
                    switch (expect)
                    {
                    case ExpectedAnswer.Digit:
                        retVal = SharedSerial.ReceiveCounted(1);
                        break;

                    case ExpectedAnswer.HashTerminated:
                        retVal = SharedSerial.ReceiveTerminated("#");
                        retVal = retVal.TrimEnd('#');
                        break;

                    case ExpectedAnswer.DoubleHashTerminated:
                        retVal = SharedSerial.ReceiveTerminated("#");
                        retVal = retVal.TrimEnd('#');
                        retVal = SharedSerial.ReceiveTerminated("#");
                        break;
                    }

                    LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Reply: " + retVal);
                }
                else
                {
                    LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Not connected or Empty Message: " + message);
                }
                LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} - Releasing lock");
            }

            LogMessage(LoggingFlags.Serial, $"SendMessage Nr{messageNr,0:0000} < Released lock");
            return(retVal);
        }