示例#1
0
        /// <summary>
        /// Send data to device and read data from device
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="MaxReceive"></param>
        /// <param name="TermChars"></param>
        /// <param name="ReturnTerm"></param>
        /// <returns></returns>
        public string SendAndReceive(string Message, int MaxReceive, string TermChars, bool ReturnTerm)
        {
            if (((Message.Length == 0) || (MaxReceive <= 0)) || !IsConnected)
            {
                return("");
            }
            Flush();

            StringBuilder builder   = new StringBuilder();
            int           termIndex = 0;

            if (IPSocket.WriteSocket(Message) && ReturnsStatus)
            {
                isWriteSocket = true;
                System.Threading.Thread.Sleep(50);

                bool flag2 = false;
                while (!flag2)
                {
                    if (IPSocket.WaitForSocketData())
                    {
                        builder.Append(IPSocket.GetReceiveBuffer().Replace("\0", ""));
                        IPSocket.ClearReceiveBuffer();

                        termIndex = builder.ToString().IndexOf(TermChars);
                        if (termIndex > 0)
                        {
                            flag2 = true;
                        }
                    }
                    else
                    {
                        break;
                    }

                    if (TermChars.Length == 0 && builder.Length >= MaxReceive)
                    {
                        break;
                    }
                }
            }

            if (TermChars.Length == 0 &&
                MaxReceive > 1 &&
                builder.Length > MaxReceive)
            {
                return(builder.ToString().Substring(0, MaxReceive - 1));
            }
            else
            {
                return(termIndex > 0 ? builder.ToString().Substring(0, termIndex - TermChars.Length) : builder.ToString());
            }
        }