Exemplo n.º 1
0
        static void Main(string[] args)
        {
            SerialPortListener listener = new SerialPortListener("COM2");

            listener.RawMessageRecieved += new EventHandler <RecievedMessageEventArgs>(listener_OnMessageRecieved);
            listener.Start();
            Console.WriteLine(listener.ToString());

            Console.WriteLine("Press ESC to exit");
            do
            {
                while (!Console.KeyAvailable)
                {
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            listener.Dispose();
        }
Exemplo n.º 2
0
        private void CloseTest()
        {
            //close host threads
            for (int i = 0; i < tcpClientArray.Length; i++)
            {
                if (tcpClientArray[i] != null)
                {
                    tcpClientArray[i].Close();
                    tcpClientArray[i].GoodReadPatternFound     -= OnTCPClientGoodReadPatternFound;
                    tcpClientArray[i].NoReadPatternFound       -= OnTCPClientNoReadPatternFound;
                    tcpClientArray[i].PartialReadPatternFound  -= OnTCPClientPartialReadPatternFound;
                    tcpClientArray[i].MultipleReadPatternFound -= OnTCPClientMultipleReadPatternFound;
                    tcpClientArray[i].HeartBeatPatternFound    -= OnTCPClientHeartBeatPatternFound;
                    tcpClientArray[i].OutOfOrderPhaseFound     -= OnTCPClientOutOfOrderPhaseFound;
                    tcpClientArray[i].MissingPhaseFound        -= OnTCPClientMissingPhaseFound;
                    tcpClientArray[i] = null;
                }
            }
            for (int i = 0; i < tcpServerArray.Length; i++)
            {
                if (tcpServerArray[i] != null)
                {
                    tcpServerArray[i].Close();
                    tcpServerArray[i].GoodReadPatternFound     -= OnTCPServerGoodReadPatternFound;
                    tcpServerArray[i].NoReadPatternFound       -= OnTCPServerNoReadPatternFound;
                    tcpServerArray[i].PartialReadPatternFound  -= OnTCPServerPartialReadPatternFound;
                    tcpServerArray[i].MultipleReadPatternFound -= OnTCPServerMultipleReadPatternFound;
                    tcpServerArray[i].HeartBeatPatternFound    -= OnTCPServerHeartBeatPatternFound;
                    tcpServerArray[i] = null;
                }
            }
            for (int i = 0; i < udpServerArray.Length; i++)
            {
                if (udpServerArray[i] != null)
                {
                    udpServerArray[i].Close();
                    udpServerArray[i].GoodReadPatternFound     -= OnUDPServerGoodReadPatternFound;
                    udpServerArray[i].NoReadPatternFound       -= OnUDPServerNoReadPatternFound;
                    udpServerArray[i].PartialReadPatternFound  -= OnUDPServerPartialReadPatternFound;
                    udpServerArray[i].MultipleReadPatternFound -= OnUDPServerMultipleReadPatternFound;
                    udpServerArray[i].HeartBeatPatternFound    -= OnUDPServerHeartBeatPatternFound;
                    udpServerArray[i] = null;
                }
            }

            System.Threading.Thread.Sleep(500);

            for (int i = 0; i < tcpClientArray.Length; i++)
            {
                if (tcpClientArray[i] != null)
                {
                    tcpClientArray[i] = null;
                }
            }
            for (int i = 0; i < tcpServerArray.Length; i++)
            {
                if (tcpServerArray[i] != null)
                {
                    tcpServerArray[i] = null;
                }
            }
            for (int i = 0; i < udpServerArray.Length; i++)
            {
                if (udpServerArray[i] != null)
                {
                    udpServerArray[i] = null;
                }
            }

            if (serialPortMain != null)
            {
                serialPortMain.Close();
                System.Threading.Thread.Sleep(100);
                serialPortMainThread.Join();
                serialPortMainThread = null;
                serialPortMain.Dispose();
                serialPortMain = null;
            }
            if (serialPortAUX != null)
            {
                serialPortAUX.Close();
                System.Threading.Thread.Sleep(100);
                serialPortAUXThread.Join();
                serialPortAUXThread = null;
                serialPortAUX.Dispose();
                serialPortAUX = null;
            }

            // setup log files
            if (logConfig.EnableLogging)
            {
                if (logger != null)
                {
                    logger.Close();
                    logger.Dispose();
                    logger = null;
                }
            }

            OnEnded(EventArgs.Empty);
        }