public TcpTransport(Int32 packetNumber, byte[] payload, ExpectedAnswer exAn) { this.PacketLength = payload.Length + 12; this.PacketNumber = packetNumber; this.Payload = payload; this.Answer = exAn; }
/// <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> /// 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); }