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(""); } } }
/// <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("#"); } }
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); }
/// <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) { lock (lockObject) { SharedSerial.Transmit(message); // TODO replace this with your requirements return(SharedSerial.ReceiveTerminated("#")); } }
/// <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(""); } } }
/// <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); }
public static string SendCommandWithReturn(string command) { string ret = ""; try { CheckConnected(); lock (writeLock) { SharedSerial.Transmit(command + "\n"); } string cmd = command.Split(' ')[0]; string response = ""; lock (readLock) { /// Read and Write are locked with different locks /// Can Read/Write simultaneously, but only one instance and Read/Write at a time response = SharedSerial.ReceiveTerminated("\n").TrimEnd(spchar); } if (response == "Unknown command") { return(""); } do { //Console.WriteLine(">" + response + "<"); string[] s = response.Split(' '); if (s.Length != 0) { // Empty string int retCode; if (int.TryParse(s[0], out retCode)) { if (s.Length >= 2 && s[1] == cmd) { break; } } if (s[0] == cmd) { // Message addressed to this command // Append the rest of the response to the output ret = ret + String.Join(" ", s.Skip(1).ToArray()) + "\n"; } } lock (readLock) { response = SharedSerial.ReceiveTerminated("\n").TrimEnd(spchar); } } while (true); } catch (Exception) { // Error happened, try disconnect the Serial Console.WriteLine("Error: serial disconnected"); dropConnection(); } finally { ret = ret.TrimEnd(spchar); } return(ret); }