Ejemplo n.º 1
0
        protected virtual void CheckPlugged(object com)
        {
            SerialBase serialPort = com as SerialBase;
            bool       bOpen      = serialPort.Open();

            if (!bOpen)
            {
                return;
            }
            SendFreshCmd(serialPort);
        }
Ejemplo n.º 2
0
 public SerialPump(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;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 刷新已连接串口,并返回该串口号
        /// </summary>
        /// <returns>串口号</returns>
        public virtual string FreshCom()
        {
            m_FreshEvent.Reset();
            string connectedCom = string.Empty;

            string[]          portNames      = SerialPort.GetPortNames();
            List <Thread>     threadPool     = new List <Thread>();
            List <SerialBase> serialPortPool = new List <SerialBase>();

            bufferByCom.Clear();
            foreach (string port in portNames)
            {
                //开启多线程,每个串口开一个
                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(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);
        }
Ejemplo n.º 4
0
 public SerialPump(ProductID productID, int channel, int baudRate, int dataBits, StopBits stopBits, Parity parity, string portName = "")
 {
     m_ProductID = productID;
     m_ChannelNo = channel;
     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;
     }
     if (m_ProductID == ProductID.GrasebyF8)
     {
         if (m_ChannelNo == 1)
         {
             m_FreshCmd[3]          = 0x07;
             m_FreshCmd[7]          = 0x9b;
             m_FreshCmdCheckByte[3] = 0x07;
         }
         else if (m_ChannelNo == 2)
         {
             m_FreshCmd[3]          = 0x08;
             m_FreshCmd[7]          = 0x9a;
             m_FreshCmdCheckByte[3] = 0x08;
         }
         else
         {
             m_FreshCmd[3]          = 0x07;
             m_FreshCmd[7]          = 0x9b;
             m_FreshCmdCheckByte[3] = 0x07;
         }
     }
     else if (m_ProductID == ProductID.GrasebyC8)
     {
         m_FreshCmd[3]          = 0x00;
         m_FreshCmd[7]          = 0xa2;
         m_FreshCmdCheckByte[3] = 0x00;
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Build a minimal row from a class (key fields only)
 /// </summary>
 public static void BuildMinimalRow(ref DataRow row, Serial entity)
 {
     SerialBase.BuildMinimalRow(ref row, entity);
 }
Ejemplo n.º 6
0
        protected const int WAITFOREVENTTIMEOUT     = 2000;                                    //2秒
                                                                                               //protected


        public SerialPump()
        {
            m_SerialBase = new SerialBase();
            m_SerialBase.DataReceived += OnDataReceived;
        }
Ejemplo n.º 7
0
 protected virtual void SendFreshCmd(SerialBase serialPort)
 {
     serialPort.SendData(m_FreshCmd);
 }