Exemplo n.º 1
0
 public SerialPressureGauge()
 {
     m_SerialBase = new SerialBase();
     m_SerialBase.DataReceived += OnDataReceived;
     m_SerialBase.ReadCount     = FRESHCMDCHECKBYTELENGTH;
     m_FreshCmd = new byte[5] {
         0x40, 0x30, 0x30, 0x31, 0x21
     };                                                        //@001!
 }
        protected void CheckPlugged(object com)
        {
            SerialBase serialPort = com as SerialBase;

            serialPort.ReadCount = FRESHCMDCHECKBYTELENGTH;
            bool bOpen = serialPort.Open();

            if (!bOpen)
            {
                return;
            }
            SendFreshCmd(serialPort);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 刷新已连接串口,并返回该串口号
        /// </summary>
        /// <returns>串口号</returns>
        public string FreshCom(string portName = "")
        {
            m_PluggedPortName = "";
            m_FreshEvent.Reset();
            string connectedCom = string.Empty;

            string[]          portNames      = new string[] { portName };
            List <Thread>     threadPool     = new List <Thread>();
            List <SerialBase> serialPortPool = new List <SerialBase>();

            bufferByCom.Clear();
            foreach (string port in portNames)
            {
                if (m_OccupancyComList.FindIndex((x) => { return(string.Compare(x, port, true) == 0); }) >= 0)
                {
                    continue;
                }
                //开启多线程,每个串口开一个
                bufferByCom.Add(port, new List <byte>());
                Thread     freshThread = new Thread(new ParameterizedThreadStart(CheckPlugged));
                SerialBase serialPort  = new SerialBase(port,
                                                        m_BaudRate,
                                                        m_DataBits,
                                                        m_StopBits,
                                                        m_Parity
                                                        );
                serialPort.DataReceived += OnFreshDataReceived;
                serialPortPool.Add(serialPort);
                freshThread.Start(serialPort);
                threadPool.Add(freshThread);
            }
            for (int i = 0; i < threadPool.Count; i++)
            {
                threadPool[i].Join();
            }
            if (m_FreshEvent.WaitOne(2000 /*WAITFOREVENTTIMEOUT*/))
            {
            }
            for (int i = 0; i < serialPortPool.Count; i++)
            {
                serialPortPool[i].Close();
            }
            for (int i = 0; i < threadPool.Count; i++)
            {
                threadPool[i].Abort();
            }
            return(connectedCom = m_PluggedPortName);
        }
Exemplo n.º 4
0
        public SerialPressureGauge(int baudRate, int dataBits, StopBits stopBits, Parity parity, string portName = "")
        {
            m_PortName = portName;
            m_BaudRate = baudRate;
            m_DataBits = dataBits;
            m_StopBits = stopBits;
            m_Parity   = parity;
            if (!string.IsNullOrEmpty(portName))
            {
                m_SerialBase = new SerialBase(portName, baudRate, dataBits, stopBits, parity);
                m_SerialBase.DataReceived += OnDataReceived;
            }

            m_FreshCmd = new byte[5] {
                0x40, 0x30, 0x30, 0x31, 0x21
            };                                                        //@001!
            if (!string.IsNullOrEmpty(portName))
            {
                m_SerialBase.ReadCount = FRESHCMDCHECKBYTELENGTH;
            }
        }
 protected void SendFreshCmd(SerialBase serialPort)
 {
     serialPort.SendData(m_FreshCmd);
 }