示例#1
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);
        }
        /// <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("");
                }
            }
        }
示例#3
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);
        }