Exemplo n.º 1
0
        public easycomMangager(PortParam portparam)
        {
            SerialPort           = new SerialPortListener(portparam.m_portName, portparam.m_baudRate);
            SerialPort.PortName  = portparam.m_portName;
            SerialPort.BaudRate  = portparam.m_baudRate;
            SerialPort.DataBits  = portparam.m_dataBits;
            SerialPort.StopBits  = portparam.m_stopbits;
            SerialPort.Parity    = portparam.m_parity;
            SerialPort.Handshake = portparam.m_handshake;
            SerialPort.ReceivedBytesThreshold = portparam.m_receivedBytesThreshold;
            SerialPort.ReceiveTimeout         = portparam.m_receiveTimeout;
            SerialPort.SendInterval           = portparam.m_sendInterval;
            SerialPort.ReadBufferSize         = portparam.m_readBufferSize;

            SerialPort.OnSerialPortReceived += new OnReceivedData(XJPLC_SerialPort_Received);

            m_buffer = new List <byte>();

            ErrorConnTimer = new System.Timers.Timer(Constant.XJConnectTimeOut);  //这里0.3 秒别改 加到常量里 工控机性能不行

            ErrorConnTimer.Enabled = false;

            ErrorConnTimer.AutoReset = true;

            ErrorConnTimer.Elapsed += new System.Timers.ElapsedEventHandler(ErrorConnTimerEvent);

            ErrorConnCount = 0;

            DataProcessEventArgs = new CommEventArgs();

            openPort();
        }
 /// <summary>
 /// Free the SerialPort object memory, for example when swapping to another device
 /// </summary>
 public void ReleaseResources()
 {
     if (_SerialPort != null)
     {
         _SerialPort.UARTDataReceived -= Event_UARTDataProcessing;
         _SerialPort.CloseDevice();
         _SerialPort = null;
     }
 }
Exemplo n.º 3
0
 private void But_OpenPort_Click(object sender, EventArgs e)
 {
     if (PortName.Text == "")
     {
         MessageBox.Show("选择串口", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;
     }
     if (m_SerialPort == null)
     {
         m_SerialPort                        = new SerialPortListener(PortName.Text, Convert.ToInt32(BaudRate.Text));
         m_SerialPort.Parity                 = m_parity;
         m_SerialPort.StopBits               = m_stopbits;
         m_SerialPort.DataBits               = Convert.ToInt32(Com_DataBit.Text);
         m_SerialPort.BaudRate               = Convert.ToInt32(BaudRate.Text);
         m_SerialPort.Handshake              = Handshake.None;
         m_SerialPort.ReadBufferSize         = 1024;
         m_SerialPort.ReceivedBytesThreshold = 1;
         m_SerialPort.BufferSize             = 1024;
         m_SerialPort.ReceiveTimeout         = Convert.ToInt32(TimeOut_Text.Text);
         m_SerialPort.WriteBufferSize        = 100;
         m_SerialPort.SendInterval           = 100;
         m_SerialPort.SerialPortResult      += new HandResult(SerialPort_Result);
         m_SerialPort.OnSerialPortReceived  += new OnReceivedData(SerialPort_Received);
         m_SerialPort.OnSeriaPortSend       += new OnSendData(SerialPort_Send);
     }
     if (!m_SerialPort.IsOpen)
     {
         try
         {
             m_SerialPort.Start();
             But_OpenPort.Text = "关闭串口";
             Console.WriteLine($"serialport1.PortName = {m_SerialPort.PortName}");
             Console.WriteLine($"serialport1.BaudRate = {m_SerialPort.BaudRate}");
             Console.WriteLine($"serialport1.DataBits = {m_SerialPort.DataBits}");
             Console.WriteLine($"serialport1.StopBits = {m_SerialPort.StopBits}");
             Console.WriteLine($"serialport1.Parity = {m_SerialPort.Parity}");
             state_Lable.Text = "串口已经打开";
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         ThreadDataHandle          = new Thread(new ThreadStart(Protocol_Resolution));
         ThreadDataHandle.Priority = ThreadPriority.Lowest;
         ThreadDataHandle.Start();
     }
     else
     {
         m_SerialPort.Stop();
         if (m_SerialPort.IsOpen == false)
         {
             But_OpenPort.Text = "打开串口";
         }
         state_Lable.Text = "串口已经打关闭";
     }
 }
        public HangbankModel()
        {
            Listener = new SerialPortListener();
            Training = new Training();

            LinksOnbelast  = 394;  // 15 kg op balk van gym.
            LinksBelast    = 748;  // 40 kg op balk van gym.
            RechtsOnbelast = 188;
            RechtsBelast   = 360;
        }
Exemplo n.º 5
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();
        }
 /// <summary>
 /// Initialize a new SerialPort object with a specific deviceId and baud value
 /// </summary>
 public void InitSerialPort()
 {
     _SerialPort = new SerialPortListener();
     _SerialPort.UARTDataReceived += Event_UARTDataProcessing;
     _SerialPort.InitSerialPort(FourByFourConstants.SERIAL_PORT_BAUDS);
 }
Exemplo n.º 7
0
        private bool PrepareTest()
        {
            SerialPortItem serialportMain    = null;
            SerialPortItem serialportAUX     = null;
            string         goodreadpatt      = "";
            string         noreadpatt        = "";
            string         partialreadpatt   = "";
            string         multiplereadpatt  = "";
            string         phaseanalysispatt = "";

            // setup log files
            if (logConfig.EnableLogging)
            {
                logger = new StreamWriter(logConfig.LoggingFolder + "\\" + logConfig.LogFileName, logConfig.AppendLogs);
            }

            if (configParams.TestGoodRead)
            {
                if (configParams.GoodRead_SerialMain_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportMain = configParams.serialportArray.Find(x => x.portName == configParams.GoodRead_SerialMain_comPort);
                    }
                    goodreadpatt = configParams.GoodRead_pattern;
                }
                if (configParams.GoodRead_SerialAUX_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportAUX = configParams.serialportArray.Find(x => x.portName == configParams.GoodRead_SerialAUX_comPort);
                    }
                    goodreadpatt = configParams.GoodRead_pattern;
                }
            }
            if (configParams.TestNoRead)
            {
                if (configParams.NoRead_SerialMain_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportMain = configParams.serialportArray.Find(x => x.portName == configParams.NoRead_SerialMain_comPort);
                    }
                    noreadpatt = configParams.NoRead_pattern;
                }
                if (configParams.NoRead_SerialAUX_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportAUX = configParams.serialportArray.Find(x => x.portName == configParams.NoRead_SerialAUX_comPort);
                    }
                    noreadpatt = configParams.NoRead_pattern;
                }
            }
            if (configParams.TestPartialRead)
            {
                if (configParams.PartialRead_SerialMain_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportMain = configParams.serialportArray.Find(x => x.portName == configParams.PartialRead_SerialMain_comPort);
                    }
                    partialreadpatt = configParams.PartialRead_pattern;
                }
                if (configParams.PartialRead_SerialAUX_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportAUX = configParams.serialportArray.Find(x => x.portName == configParams.PartialRead_SerialAUX_comPort);
                    }
                    partialreadpatt = configParams.PartialRead_pattern;
                }
            }
            if (configParams.TestMultipleRead)
            {
                if (configParams.MultipleRead_SerialMain_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportMain = configParams.serialportArray.Find(x => x.portName == configParams.MultipleRead_SerialMain_comPort);
                    }
                    multiplereadpatt = configParams.MultipleRead_pattern;
                }
                if (configParams.MultipleRead_SerialAUX_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportAUX = configParams.serialportArray.Find(x => x.portName == configParams.MultipleRead_SerialAUX_comPort);
                    }
                    multiplereadpatt = configParams.MultipleRead_pattern;
                }
            }
            if (configParams.TestPhaseAnalysis)
            {
                if (configParams.PhaseAnalysis_SerialMain_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportMain = configParams.serialportArray.Find(x => x.portName == configParams.PhaseAnalysis_SerialMain_comPort);
                    }
                    phaseanalysispatt = configParams.PhaseAnalysis_pattern;
                }
                if (configParams.PhaseAnalysis_SerialAUX_comPort != "Not used")
                {
                    if (configParams.serialportArray != null)
                    {
                        serialportAUX = configParams.serialportArray.Find(x => x.portName == configParams.PhaseAnalysis_SerialAUX_comPort);
                    }
                    phaseanalysispatt = configParams.PhaseAnalysis_pattern;
                }
            }

            if (serialportMain != null)
            {
                serialPortMain = new SerialPortListener(serialportMain.portName, serialportMain.baud, serialportMain.parity, serialportMain.databits, serialportMain.stopbits, serialportMain.hanshake, logger);
                serialPortMain.SetPatterns(goodreadpatt, noreadpatt, partialreadpatt, multiplereadpatt, phaseanalysispatt);
                serialPortMain.GoodReadPatternFound     += new EventHandler(OnSerialMainGoodReadPatternFound);
                serialPortMain.NoReadPatternFound       += new EventHandler(OnSerialMainNoReadPatternFound);
                serialPortMain.PartialReadPatternFound  += new EventHandler(OnSerialMainPartialReadPatternFound);
                serialPortMain.MultipleReadPatternFound += new EventHandler(OnSerialMainMultipleReadPatternFound);
                serialPortMain.OutOfOrderFound          += new EventHandler(OnSerialMainOutOfOrderPhaseFound);
                serialPortMain.MissingFound             += new EventHandler(OnSerialMainMissingPhaseFound);
                serialPortMainThread      = new Thread(serialPortMain.ManageSerialPort);
                serialPortMainThread.Name = "SerialPortMainThread";
            }

            if (serialportAUX != null)
            {
                serialPortAUX = new SerialPortListener(serialportAUX.portName, serialportAUX.baud, serialportAUX.parity, serialportAUX.databits, serialportAUX.stopbits, serialportAUX.hanshake, logger);
                serialPortAUX.SetPatterns(goodreadpatt, noreadpatt, partialreadpatt, multiplereadpatt, phaseanalysispatt);
                serialPortAUX.GoodReadPatternFound     += new EventHandler(OnSerialAuxGoodReadPatternFound);
                serialPortAUX.NoReadPatternFound       += new EventHandler(OnSerialAuxNoReadPatternFound);
                serialPortAUX.PartialReadPatternFound  += new EventHandler(OnSerialAuxPartialReadPatternFound);
                serialPortAUX.MultipleReadPatternFound += new EventHandler(OnSerialAuxMultipleReadPatternFound);
                serialPortMain.OutOfOrderFound         += new EventHandler(OnSerialAuxOutOfOrderPhaseFound);
                serialPortMain.MissingFound            += new EventHandler(OnSerialAuxMissingPhaseFound);
                serialPortAUXThread      = new Thread(serialPortAUX.ManageSerialPort);
                serialPortAUXThread.Name = "SerialPortAUXThread";
            }

            if (configParams.TestGoodRead)
            {
                if (configParams.GoodRead_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.GoodRead_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].GoodreadPattern       = configParams.GoodRead_pattern;
                        tcpClientArray[idx].GoodReadPatternFound += new EventHandler(OnTCPClientGoodReadPatternFound);
                    }
                }
                if (configParams.GoodRead_TCPServer_hostPort != 0)
                {
                    int idx = GetTCPServer(configParams.GoodRead_TCPServer_hostPort);
                    if (idx != -1)
                    {
                        tcpServerArray[idx].GoodreadPattern       = configParams.GoodRead_pattern;
                        tcpServerArray[idx].GoodReadPatternFound += new EventHandler(OnTCPServerGoodReadPatternFound);
                    }
                }
                if (configParams.GoodRead_UDPServer_hostPort != 0)
                {
                    int idx = GetUDPServer(configParams.GoodRead_UDPServer_hostPort);
                    if (idx != -1)
                    {
                        udpServerArray[idx].GoodreadPattern       = configParams.GoodRead_pattern;
                        udpServerArray[idx].GoodReadPatternFound += new EventHandler(OnUDPServerGoodReadPatternFound);
                    }
                }
            }
            if (configParams.TestNoRead)
            {
                if (configParams.NoRead_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.NoRead_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].NoreadPattern       = configParams.NoRead_pattern;
                        tcpClientArray[idx].NoReadPatternFound += new EventHandler(OnTCPClientNoReadPatternFound);
                    }
                }
                if (configParams.NoRead_TCPServer_hostPort != 0)
                {
                    int idx = GetTCPServer(configParams.NoRead_TCPServer_hostPort);
                    if (idx != -1)
                    {
                        tcpServerArray[idx].NoreadPattern       = configParams.NoRead_pattern;
                        tcpServerArray[idx].NoReadPatternFound += new EventHandler(OnTCPServerNoReadPatternFound);
                    }
                }
                if (configParams.NoRead_UDPServer_hostPort != 0)
                {
                    int idx = GetUDPServer(configParams.NoRead_UDPServer_hostPort);
                    if (idx != -1)
                    {
                        udpServerArray[idx].NoreadPattern       = configParams.NoRead_pattern;
                        udpServerArray[idx].NoReadPatternFound += new EventHandler(OnUDPServerNoReadPatternFound);
                    }
                }
            }
            if (configParams.TestPartialRead)
            {
                if (configParams.PartialRead_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.PartialRead_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].PartialreadPattern       = configParams.PartialRead_pattern;
                        tcpClientArray[idx].PartialReadPatternFound += new EventHandler(OnTCPClientPartialReadPatternFound);
                    }
                }
                if (configParams.PartialRead_TCPServer_hostPort != 0)
                {
                    int idx = GetTCPServer(configParams.PartialRead_TCPServer_hostPort);
                    if (idx != -1)
                    {
                        tcpServerArray[idx].PartialreadPattern       = configParams.PartialRead_pattern;
                        tcpServerArray[idx].PartialReadPatternFound += new EventHandler(OnTCPServerPartialReadPatternFound);
                    }
                }
                if (configParams.PartialRead_UDPServer_hostPort != 0)
                {
                    int idx = GetUDPServer(configParams.PartialRead_UDPServer_hostPort);
                    if (idx != -1)
                    {
                        udpServerArray[idx].PartialreadPattern       = configParams.PartialRead_pattern;
                        udpServerArray[idx].PartialReadPatternFound += new EventHandler(OnUDPServerPartialReadPatternFound);
                    }
                }
            }
            if (configParams.TestMultipleRead)
            {
                if (configParams.MultipleRead_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.MultipleRead_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].MultiplereadPattern       = configParams.MultipleRead_pattern;
                        tcpClientArray[idx].MultipleReadPatternFound += new EventHandler(OnTCPClientMultipleReadPatternFound);
                    }
                }
                if (configParams.MultipleRead_TCPServer_hostPort != 0)
                {
                    int idx = GetTCPServer(configParams.MultipleRead_TCPServer_hostPort);
                    if (idx != -1)
                    {
                        tcpServerArray[idx].MultiplereadPattern       = configParams.MultipleRead_pattern;
                        tcpServerArray[idx].MultipleReadPatternFound += new EventHandler(OnTCPServerMultipleReadPatternFound);
                    }
                }
                if (configParams.MultipleRead_UDPServer_hostPort != 0)
                {
                    int idx = GetUDPServer(configParams.MultipleRead_UDPServer_hostPort);
                    if (idx != -1)
                    {
                        udpServerArray[idx].MultiplereadPattern       = configParams.MultipleRead_pattern;
                        udpServerArray[idx].MultipleReadPatternFound += new EventHandler(OnUDPServerMultipleReadPatternFound);
                    }
                }
            }

            if (configParams.TestHeartBeat)
            {
                if (configParams.HeartBeat_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.HeartBeat_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].HeartBeatPattern       = configParams.HeartBeat_pattern;
                        tcpClientArray[idx].HeartBeatPatternFound += new EventHandler(OnTCPClientHeartBeatPatternFound);
                    }
                }
                if (configParams.HeartBeat_TCPServer_hostPort != 0)
                {
                    int idx = GetTCPServer(configParams.HeartBeat_TCPServer_hostPort);
                    if (idx != -1)
                    {
                        tcpServerArray[idx].HeartBeatPattern       = configParams.HeartBeat_pattern;
                        tcpServerArray[idx].HeartBeatPatternFound += new EventHandler(OnTCPServerHeartBeatPatternFound);
                    }
                }
                if (configParams.HeartBeat_UDPServer_hostPort != 0)
                {
                    int idx = GetUDPServer(configParams.HeartBeat_UDPServer_hostPort);
                    if (idx != -1)
                    {
                        udpServerArray[idx].HeartBeatPattern       = configParams.HeartBeat_pattern;
                        udpServerArray[idx].HeartBeatPatternFound += new EventHandler(OnUDPServerHeartBeatPatternFound);
                    }
                }
            }

            if (configParams.TestPhaseAnalysis)
            {
                if (configParams.PhaseAnalysis_TCPClient_hostPort != 0)
                {
                    int idx = GetTCPClient(configParams.PhaseAnalysis_TCPClient_hostPort);
                    if (idx != -1)
                    {
                        tcpClientArray[idx].GoodreadPattern = configParams.PhaseAnalysis_pattern;
                        tcpClientArray[idx].SearchForPhaseIndex(configParams.PhaseAnalysis_pattern);
                        tcpClientArray[idx].OutOfOrderPhaseFound += OnTCPClientOutOfOrderPhaseFound;
                        tcpClientArray[idx].MissingPhaseFound    += OnTCPClientMissingPhaseFound;
                    }
                }
            }

            if (serialPortMainThread != null)
            {
                serialPortMainThread.Start();
            }
            if (serialPortAUXThread != null)
            {
                serialPortAUXThread.Start();
            }

            if (tcpClientPhaseAnalysisThread != null)
            {
                tcpClientPhaseAnalysisThread.Start();
            }

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

            return(true);
        }
Exemplo n.º 8
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);
        }