Пример #1
0
 /// <summary>
 /// 处理串口传入的数据
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void OnDataReceived(object sender, DataTransEventArgs e)
 {
     byte[] temp = new byte[FRESHCMDCHECKBYTELENGTH];
     //-32768 ~ 32767在此范围的值,当传过来的值超过32767时一定是负值,此时要异或0xFFFF后再加1补码是为负数的绝对值
     // { DP D5 D4 D3 D2 D1 UNIT }\{ E R R O R 0 1 }\{ E R R O R 0 2 }\{ E R R O R 0 3 }
     try
     {
         m_ReadBuffer.AddRange(e.EventData);
         for (int iLoop = 0; iLoop < m_ReadBuffer.Count; iLoop++)
         {
             System.Diagnostics.Debug.Write(m_ReadBuffer[iLoop].ToString("X") + " ");
         }
         System.Diagnostics.Debug.WriteLine("");
         PressureGaugeDataEventArgs args = Analyze(m_ReadBuffer);
         if (args != null && PressureGaugeDataRecerived != null)
         {
             PressureGaugeDataRecerived(this, args);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
 }
Пример #2
0
 public PressureGaugeDataEventArgs(PressureGaugeDataEventArgs data)
 {
     this.Unit          = data.Unit;
     this.PressureValue = data.PressureValue;
     this.ErrorMessage  = data.ErrorMessage;
 }
Пример #3
0
        private PressureGaugeDataEventArgs Analyze(List <byte> eventData)
        {
            PressureGaugeDataEventArgs args = null;

            if (eventData.Count < FRESHCMDCHECKBYTELENGTH)
            {
                return(null);
            }
            byte[] buffer = new byte[FRESHCMDCHECKBYTELENGTH];
            bool   bFind  = false;

            while (eventData.Count >= FRESHCMDCHECKBYTELENGTH)
            {
                if (eventData[0] != 0x7B)
                {
                    eventData.RemoveAt(0);
                    continue;
                }
                else
                {
                    if (eventData[FRESHCMDCHECKBYTELENGTH - 1] != 0x7D)
                    {
                        eventData.RemoveAt(0);
                        continue;
                    }
                    else
                    {
                        bFind = true;
                        eventData.CopyTo(0, buffer, 0, FRESHCMDCHECKBYTELENGTH);
                        eventData.RemoveRange(0, FRESHCMDCHECKBYTELENGTH);
                    }
                }
            }

            if (bFind)
            {
                //小数点错误
                if (buffer[1] > 0x33 || buffer[1] < 0x30)
                {
                    return(null);
                }
                //单位错误
                if (buffer[7] > 0x37 || buffer[1] < 0x30)
                {
                    return(null);
                }

                for (int iLoop = 2; iLoop <= 6; iLoop++)
                {
                    if (buffer[iLoop] < 0x30 || buffer[iLoop] > 0x39)
                    {
                        return(null);
                    }
                }
                int D5    = (buffer[2] - 0x30) * 10000;
                int D4    = (buffer[3] - 0x30) * 1000;
                int D3    = (buffer[4] - 0x30) * 100;
                int D2    = (buffer[5] - 0x30) * 10;
                int D1    = (buffer[6] - 0x30);
                int total = D1 + D2 + D3 + D4 + D5;
                if (total > 0xFFFF) //错误数字
                {
                    return(null);
                }
                ushort num = 0;
                float  sum = 0;
                if (total > 32767) //负数
                {
                    num = (ushort)total;
                    num = (ushort)(num ^ 0xFFFF + 0x0001);
                    sum = -1 * num;
                }
                else
                {
                    num = (ushort)total;
                    sum = num;
                }
                switch (buffer[1])
                {
                case 0x31:
                    sum *= 0.1f;
                    break;

                case 0x32:
                    sum *= 0.01f;
                    break;

                case 0x33:
                    sum *= 0.001f;
                    break;
                }
                PressureUnit unit = (PressureUnit)(buffer[7] - 0x30);
                args = new PressureGaugeDataEventArgs(unit, sum);
                return(args);
            }
            else
            {
                return(null);
            }
        }
Пример #4
0
        private PressureGaugeDataEventArgs Analyze(byte[] eventData)
        {
            PressureGaugeDataEventArgs args = null;

            if (eventData.Length != FRESHCMDCHECKBYTELENGTH)
            {
                return(null);
            }
            //开始和结束字符错误
            if (eventData[0] != 0x7B || eventData[FRESHCMDCHECKBYTELENGTH - 1] != 0x7D)
            {
                return(null);
            }
            //小数点错误
            if (eventData[1] > 0x33 || eventData[1] < 0x30)
            {
                return(null);
            }
            //单位错误
            if (eventData[7] > 0x37 || eventData[1] < 0x30)
            {
                return(null);
            }

            for (int iLoop = 2; iLoop <= 6; iLoop++)
            {
                if (eventData[iLoop] < 0x30 || eventData[iLoop] > 0x39)
                {
                    return(null);
                }
            }
            int D5    = (eventData[2] - 0x30) * 10000;
            int D4    = (eventData[3] - 0x30) * 1000;
            int D3    = (eventData[4] - 0x30) * 100;
            int D2    = (eventData[5] - 0x30) * 10;
            int D1    = (eventData[6] - 0x30);
            int total = D1 + D2 + D3 + D4 + D5;

            if (total > 0xFFFF) //错误数字
            {
                return(null);
            }
            ushort num = 0;
            float  sum = 0;

            if (total > 32767) //负数
            {
                num = (ushort)total;
                num = (ushort)(num ^ 0xFFFF + 0x0001);
                sum = -1 * num;
            }
            else
            {
                num = (ushort)total;
                sum = num;
            }
            switch (eventData[1])
            {
            case 0x31:
                sum *= 0.1f;
                break;

            case 0x32:
                sum *= 0.01f;
                break;

            case 0x33:
                sum *= 0.001f;
                break;
            }
            PressureUnit unit = (PressureUnit)(eventData[7] - 0x30);

            args = new PressureGaugeDataEventArgs(unit, sum);
            return(args);
        }