public static Command CommandFromString(string CommandAsString) { Command cmd; string [] sa; string [] saParams; sa = Command.DecodeCommand(System.Text.Encoding.ASCII.GetBytes(CommandAsString),CommandAsString.Length); if (sa.Length > 1) { saParams = new string[sa.Length - 1]; Array.Copy(sa,1,saParams,0,sa.Length - 1); cmd = new Command(sa[0],saParams); } else { cmd = new Command(sa[0],null); } return cmd; }
public static bool IsValidCommand(Command cmd) { switch (cmd.VerbString) { case "LST": return true; case "UPL": if (2 != cmd.Params.GetLength(0)) return false; return true; case "DNL": if (1 != cmd.Params.GetLength(0)) return false; return true; case "ISR": return true; case "REC": if (1 != cmd.Params.GetLength(0)) return false; return true; case "STP": return true; case "SET": if (2 != cmd.Params.GetLength(0)) return false; return true; case "GET": if (1 != cmd.Params.GetLength(0)) return false; return true; case "DEL": if (1 != cmd.Params.GetLength(0)) return false; return true; case "PULSECHECK": return true; case "RBT": if (1 != cmd.Params.GetLength(0)) return false; return true; default: return false; } }
private bool SendYesNoTypeCommand(Command cmd, ref bool bYes, ref string sErrorReturn) { log.Context = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name; if (m_conn == null) { return false; } if(!m_conn.Connected) { return false; } string serr = ""; m_sLastError = ""; Command retcmd = m_conn.SendCommandWaitForResponse(cmd); if (retcmd == null) { serr = "Command failed. Return value null"; } else { if (retcmd.VerbString == cmd.VerbString) { if (retcmd.Params.GetLength(0) > 0 && retcmd.Params[0] != "ERROR") { if (retcmd.Params[0] == "YES") { bYes = true; } else { bYes = false; } return true; } else { if (retcmd.Params.GetLength(0) > 1) { serr = retcmd.VerbString + " failed: " + retcmd.Params[1]; } else { serr = retcmd.VerbString + " failed (no params)"; } } } else { serr = "Unexpected command return: " + retcmd.VerbString; } } if (null != sErrorReturn) { sErrorReturn = serr; } m_sLastError = serr; log.WriteLog(serr); return false; }
private bool SendSetTypeCommand(Command cmd, ref string sErrorReturn) { log.Context = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name; if (m_conn == null) { return false; } if(!m_conn.Connected) { return false; } string serr = ""; m_sLastError = ""; Command retcmd = m_conn.SendCommandWaitForResponse(cmd); if (retcmd == null) { serr = "Command failed. Return value null"; } else { if (retcmd.VerbString == cmd.VerbString) { if (retcmd.Params.GetLength(0) >= 2) { if (retcmd.Params[0] == cmd.Params[0]) { // error if (retcmd.Params[1] == "ERROR") { if (retcmd.Params.GetLength(0) == 3) { serr = retcmd.Params[2]; } else { serr = "Invalid number of return ERROR params: " + retcmd.Params.GetLength(0); } } else { if (retcmd.Params[1] == "OK") { return true; } else { serr = "Invalid response to SET command: " + retcmd.Params[1]; } } } else { serr = "Unexpected SET command response KEY: " + retcmd.Params[0]; } } else { serr = "Invalid number of return params: " + retcmd.Params.GetLength(0); } } else { serr = "Unexpected command return: " + retcmd.VerbString; } } if (null != sErrorReturn) { sErrorReturn = serr; } m_sLastError = serr; log.WriteLog(serr); return false; }
/// <summary> /// Sends a Command object to the Oyster device and returns immediately. /// The response to this command must be received by using the MessageReceivedHandler /// for the particular type of message that was sent until an "End of Messages" indicator /// is received. /// </summary> /// <param name="cmd">The Command request object that is sent to the device.</param> /// <param name="sErrorReturn">A reference to a string that will either contain error /// information if there is an error, or will be blank if there is no error. This parameter /// may be null.</param> /// <returns></returns> private void SendEventResponseCommand(Command cmd) { log.Context = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name; if (m_conn == null) { return; } if(!m_conn.Connected) { return; } m_sLastError = ""; m_conn.SendCommand(cmd); }
internal void SendCommand(Command cmd) { string sLastErrorMessage = ""; try { cmd.Write(m_TCPClient.GetStream()); } catch (IOException ioex) { if (null != ioex.InnerException) { if (ioex.InnerException is SocketException) { int iErrorCode = ((SocketException) ioex.InnerException).ErrorCode; string sErrorMessage = ioex.InnerException.Message; sLastErrorMessage = "SendCommand: Error Code = " + iErrorCode.ToString() + " Description = " + sErrorMessage; } else { sLastErrorMessage = "SendCommand: Unknown Error detected. Description = " + ioex.Message; } } m_bDone = true; m_bConnected = false; } catch (Exception ex) { sLastErrorMessage = "SendCommand: Unknown Error detected. Description = " + ex.Message; m_bDone = true; m_bConnected = false; } if (!m_bConnected) { log.WriteLog(sLastErrorMessage); OnConnectionErrorReceived(this, sLastErrorMessage); } }
internal void ReceiveDataAndEnqueue() { log.Context = MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + MethodBase.GetCurrentMethod().Name; log.WriteLog("Received Data and Enqueue thread started"); string s; int iCRat, iCRcount; string[] sa; int length = 0; string sLastErrorMessage = ""; while (!m_bDone) { if (m_ConnectionErrorOccurred) { continue; } try { try { m_TCPClient.ReceiveTimeout = 100; m_NetworkStream = m_TCPClient.GetStream(); // m_NetworkStream.Read(m_bytesIncomingBuffer,m_iBufferLength,m_iBufferMaxLength - m_iBufferLength); IAsyncResult asr = m_NetworkStream.BeginRead(m_bytesIncomingBuffer, m_iBufferLength, m_iBufferMaxLength - m_iBufferLength, null, null); while (!asr.IsCompleted && !m_bDone) { Thread.Sleep(100); } if (m_bDone) { continue; } length = m_NetworkStream.EndRead(asr); } catch (IOException ioex) { if (null != ioex.InnerException) { if (ioex.InnerException is SocketException) { int iErrorCode = ((SocketException) ioex.InnerException).ErrorCode; // if (iErrorCode == 10060) // { // read timeout... just continue // //TODO: This doesn't work in .NET 2.0!!! Must change implementation! // continue; // } string sErrorMessage = ioex.InnerException.Message; sLastErrorMessage = "ReceiveDataAndEnqueue: Error Code = " + iErrorCode.ToString() + " Description = " + sErrorMessage; } else { sLastErrorMessage = "ReceiveDataAndEnqueue: Unknown Error detected. Description = " + ioex.Message; } } log.WriteLog(sLastErrorMessage); m_bDone = true; m_bConnected = false; } catch (Exception ex) { sLastErrorMessage = "ReceiveDataAndEnqueue: Unknown Error detected. Description = " + ex.Message; log.WriteLog(sLastErrorMessage); m_bDone = true; m_bConnected = false; } if (!m_bConnected) { OnConnectionErrorReceived(this, sLastErrorMessage); continue; } if (m_bDone) continue; if (0 == length) { // connection must have been reset m_bDone = true; continue; } m_iBufferLength += length; // find all the messages in the stream iCRcount = 0; for (int i = 0; i < m_iBufferLength; i++) { if (m_bytesIncomingBuffer[i] == 13) iCRcount++; } // for // post all messages from the stream for (int i = 0; i < iCRcount; i++) { s = Encoding.ASCII.GetString(m_bytesIncomingBuffer); s = s.Substring(0, m_iBufferLength); iCRat = s.IndexOf((char) 13); if (-1 != iCRat) { int iLen; s = s.Substring(0, iCRat); iLen = s.Length + 1; m_iBufferLength -= iLen; //m_sLastResponse = s; lock (m_qCommands.SyncRoot) { Command cmd; string[] saParams; sa = Command.DecodeCommand(Encoding.ASCII.GetBytes(s), s.Length); if (sa.Length > 1) { saParams = new string[sa.Length - 1]; Array.Copy(sa, 1, saParams, 0, sa.Length - 1); cmd = new Command(sa[0], saParams); } else { cmd = new Command(sa[0], null); } m_qCommands.Enqueue(cmd); log.WriteLog("Command Enqueued: " + cmd.VerbString); } byte[] bytes = new byte[m_iBufferLength]; Array.Copy(m_bytesIncomingBuffer, 0, bytes, 0, m_iBufferLength); OnMessageReceived(new CommandEventArgs(bytes, ref m_qCommands)); Array.Copy(m_bytesIncomingBuffer, iLen, m_bytesIncomingBuffer, 0, m_iBufferLength); Array.Clear(m_bytesIncomingBuffer, m_iBufferLength, m_iBufferMaxLength - m_iBufferLength); } // if } // for } catch (Exception ex) { log.WriteLog("Unknown Error detected. Description = " + ex.Message); } } // while m_EventThreadComplete[0].Set(); }