Exemplo n.º 1
0
        /// <summary>
        /// 端口监听,解析数据并触发相应事件
        /// </summary>
        public void ReadPort()
        {
            double  Bst100TLNum = 0;
            double  Bst100MVNum = 0;
            while (_isReading)
            {
                try
                {
                    // 读累计量
                    SendMsg("TL");
                    byte[] d = this._devComm.Read(_CommHandle, 19);
                    if (d.Length == 19)
                    {
                        Console.WriteLine("接收:" + System.Text.Encoding.ASCII.GetString(d));
                        //检查LRC校验码
                        byte[] LrcResult = new byte[] { 0 };
                        LrcResult = this._devComm.LrcCheck(d);
                        if (d[d.Length - 4] == LrcResult[0] && d[d.Length - 3] == LrcResult[1])
                        {
                            //解析成BST-100仪表数据
                            string Bst100ResultStr = "";
                            try
                            {
                                Bst100ResultStr = System.Text.Encoding.ASCII.GetString(d);
                                Bst100TLNum = (Convert.ToInt32(Bst100ResultStr.Substring(7, 8), 16)) / 1000.0;
                            }
                            catch (Exception ex)
                            {
                                WriteLog("读累计量" + ex.Message);
                            }
                        }

                    }

                    // 读瞬时量
                    SendMsg("PV");
                    byte[] PV = this._devComm.Read(_CommHandle, 19);
                    if (PV.Length == 19)
                    {
                        Console.WriteLine("接收:" + System.Text.Encoding.ASCII.GetString(PV));
                        //检查LRC校验码
                        byte[] LrcResult = new byte[] { 0 };
                        LrcResult = this._devComm.LrcCheck(PV);
                        if (PV[PV.Length - 4] == LrcResult[0] && PV[PV.Length - 3] == LrcResult[1])
                        {
                            //解析成BST-100仪表数据
                            string Bst100ResultStr = "";
                            try
                            {
                                Bst100ResultStr = System.Text.Encoding.ASCII.GetString(PV);
                                string MvNum = (Convert.ToInt32(Bst100ResultStr.Substring(7, 8), 16)).ToString().Remove(0, 2);
                                Bst100MVNum = (Convert.ToDouble(MvNum)) / 100000.0;
                            }
                            catch (Exception ex)
                            {
                                WriteLog("读瞬时量出错"+ex.Message);
                            }
                        }

                    }
                    // 触发事件
                    if (BstNumReceived != null)
                    {
                        BstNumReceivedEventArgs e = new BstNumReceivedEventArgs(Bst100TLNum.ToString("0.000"), Bst100MVNum.ToString("0.000"));
                        BstNumReceived(null, e);
                    }
                }
                catch (Exception ex)
                {
                    WriteLog("端口监听的线程出错" + ex.Message);
                }

            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 端口监听,解析数据并触发相应事件
        /// </summary>
        public void ReadPort()
        {
            double Bst100TLNum = 0;
            double Bst100MVNum = 0;

            while (_isReading)
            {
                try
                {
                    // 读累计量
                    SendMsg("TL");
                    byte[] d = this._devComm.Read(_CommHandle, 19);
                    if (d.Length == 19)
                    {
                        Console.WriteLine("接收:" + System.Text.Encoding.ASCII.GetString(d));
                        //检查LRC校验码
                        byte[] LrcResult = new byte[] { 0 };
                        LrcResult = this._devComm.LrcCheck(d);
                        if (d[d.Length - 4] == LrcResult[0] && d[d.Length - 3] == LrcResult[1])
                        {
                            //解析成BST-100仪表数据
                            string Bst100ResultStr = "";
                            try
                            {
                                Bst100ResultStr = System.Text.Encoding.ASCII.GetString(d);
                                Bst100TLNum     = (Convert.ToInt32(Bst100ResultStr.Substring(7, 8), 16)) / 1000.0;
                            }
                            catch (Exception ex)
                            {
                                WriteLog("读累计量" + ex.Message);
                            }
                        }
                    }

                    // 读瞬时量
                    SendMsg("PV");
                    byte[] PV = this._devComm.Read(_CommHandle, 19);
                    if (PV.Length == 19)
                    {
                        Console.WriteLine("接收:" + System.Text.Encoding.ASCII.GetString(PV));
                        //检查LRC校验码
                        byte[] LrcResult = new byte[] { 0 };
                        LrcResult = this._devComm.LrcCheck(PV);
                        if (PV[PV.Length - 4] == LrcResult[0] && PV[PV.Length - 3] == LrcResult[1])
                        {
                            //解析成BST-100仪表数据
                            string Bst100ResultStr = "";
                            try
                            {
                                Bst100ResultStr = System.Text.Encoding.ASCII.GetString(PV);
                                string MvNum = (Convert.ToInt32(Bst100ResultStr.Substring(7, 8), 16)).ToString().Remove(0, 2);
                                Bst100MVNum = (Convert.ToDouble(MvNum)) / 100000.0;
                            }
                            catch (Exception ex)
                            {
                                WriteLog("读瞬时量出错" + ex.Message);
                            }
                        }
                    }
                    // 触发事件
                    if (BstNumReceived != null)
                    {
                        BstNumReceivedEventArgs e = new BstNumReceivedEventArgs(Bst100TLNum.ToString("0.000"), Bst100MVNum.ToString("0.000"));
                        BstNumReceived(null, e);
                    }
                }
                catch (Exception ex)
                {
                    WriteLog("端口监听的线程出错" + ex.Message);
                }
            }
        }