示例#1
0
        private static void Initialize()
        {
            mSerialPort = new SerialPort()
            {
                PortName        = PortName,
                BaudRate        = BaudRate,
                Parity          = Parity,
                DataBits        = DataBits,
                StopBits        = StopBits,
                DtrEnable       = DtrEnable,
                RtsEnable       = RtsEnable,
                Handshake       = Handshake,
                ReadTimeout     = ReadTimeOut,
                WriteTimeout    = WriteTimeOut,
                ReadBufferSize  = 4096,
                WriteBufferSize = 8192,
                //WriteBufferSize = 2,
                //ReceivedBytesThreshold = 1,
                Encoding = System.Text.Encoding.GetEncoding(28591),
            };

            mSerialPort.DataReceived += new SerialDataReceivedEventHandler(mSerialPort_DataReceived);

            //mWebClient = new WebClient();
            //_TcpClient = new TcpClient();
            _TcpClientHandler = new TcpClientHandler(mSerialPort);
        }
示例#2
0
        private static void Terminate()
        {
            if (mSerialPort != null)
            {
                if (mSerialPort.IsOpen == true)
                {
                    mSerialPort.DiscardOutBuffer();
                    mSerialPort.DiscardInBuffer();
                    mSerialPort.Close();
                }

                mSerialPort.Dispose();
                mSerialPort = null;
            }

            //if (mWebClient != null)
            //{
            //    if (mWebClient.IsBusy == true)
            //    {
            //        mWebClient.CancelAsync();
            //    }

            //    mWebClient.Dispose();
            //    mWebClient = null;
            //}

            //if (_TcpClient != null)
            //{
            //    try
            //    {
            //        _TcpClient.Close();
            //    }
            //    catch { }
            //    _TcpClient = null;
            //}


            if (_TcpClientHandler != null)
            {
                try
                {
                    _TcpClientHandler.Close();
                }
                catch { }
                _TcpClientHandler = null;
            }
        }
示例#3
0
        static void ProcessReceiveResults(IAsyncResult ar)

        {
            TcpClientHandler TCH = (TcpClientHandler)ar.AsyncState;



            try

            {
                int BytesRead = 0;

                BytesRead = TCH.TcpClient.GetStream().EndRead(ar);

                //Console.WriteLine("Connection received " + BytesRead.ToString() + " byte(s).");



                if (BytesRead == 0)

                {
                    //Console.WriteLine("Connection is closing.");

                    //NSH.NetworkStream.Close();
                    TCH.Close();

                    DateTime dtEnd = DateTime.Now;

                    TCH.SerialPort.Write(Environment.NewLine);
                    TCH.SerialPort.Write("NO CARRIER (" + (dtEnd - TCH.dtStart).ToString(@"hh\:mm\:ss") + ")");
                    TCH.SerialPort.Write(Environment.NewLine);

                    return;
                }

                else
                {
                    //string Data = Encoding.ASCII.GetString(TCH.Buffer);
                    string Data = System.Text.Encoding.GetEncoding(28591).GetString(TCH.Buffer);

                    //switch (Data)
                    //{
                    //    case "ABC":
                    //        byte[] Buffer = Encoding.ASCII.GetBytes("mostly sunny");
                    //        NSH.NetworkStream.Write(Buffer, 0, Buffer.Length);
                    //        break;
                    //}

                    Console.Write(Data);

                    TCH.SerialPort.Write(Data);
                }

                TCH.Buffer = new byte[BufferSize];
                TCH.TcpClient.GetStream().BeginRead(TCH.Buffer, 0, TCH.Buffer.Length, TCH.AsyncReceiveCallback, TCH);
            }

            catch (Exception e)

            {
                Console.WriteLine("Failed to read from a network stream with error: " + e.Message);

                //TCH.NetworkStream.Close();
                TCH.Close();

                DateTime dtEnd = DateTime.Now;

                TCH.SerialPort.Write(Environment.NewLine);
                TCH.SerialPort.Write("NO CARRIER (" + (dtEnd - TCH.dtStart).ToString(@"hh\:mm\:ss") + ")");
                TCH.SerialPort.Write(Environment.NewLine);
            }
        }