Example #1
0
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="b"></param>
        public void readData(byte[] tcpByte, int count)
        {
            ////写入返回值
            int writeRet = -1;

            for (int i = 0; i < count; i++)
            {
                byte b = tcpByte[i];

                switch (currentPosition)
                {
                case 0:
                    if (b == 0x55)
                    {
                        data.Add(b);
                        currentPosition++;
                        isReading?.Invoke(this, true);
                    }
                    else
                    {
                    }
                    break;

                case 1:
                    if (b == 0xAA)
                    {
                        data.Add(b);
                        currentPosition++;
                    }
                    else
                    {
                        errorData();
                    }
                    break;

                case 2:

                    BLEcommand b1;
                    if (!Enum.TryParse(b.ToString(), out b1))
                    {
                        errorData();
                        break;
                    }
                    if (!b.ToString().Equals("11") && !b.ToString().Equals("12"))
                    {
                        errorData();
                        break;
                    }

                    data.Add(b);
                    ble = BLEData.CreateBle(b1);
                    currentPosition++;

                    break;

                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    data.Add(b);
                    currentPosition++;

                    break;

                case 10:
                    data.Add(b);
                    try
                    {
                        dataLength = BLEData.byteToInt64(data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10]);
                    }
                    catch (Exception ex)
                    {
                        // tools.log.writeLog("readData:第{0}次,错误:{1}", i.ToString(), ex.Message);
                        errorData();
                        break;
                    }

                    foreach (var d in data)
                    {
                        writeRet = ble.writeByte(d);
                    }
                    currentPosition++;
                    //  dataLength = BLE.BLEData.getInt16(data[2], data[3]);
                    break;


                default:
                    writeRet = ble.writeByte(b);
                    currentPosition++;

                    break;
                }
                if (writeRet == 0)
                {
                    successData();
                    writeRet = -1;
                }
            }
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="b"></param>
        public void readData(byte[] tcpByte, int count)
        {
            ////写入返回值
            int writeRet = -1;

            for (int i = 0; i < count; i++)
            {
                byte b = tcpByte[i];

                switch (currentPosition)
                {
                case 0:
                    if (b == 0x55)
                    {
                        data.Add(b);
                        currentPosition++;
                    }
                    else
                    {
                    }
                    break;

                case 1:
                    if (b == 0xAA)
                    {
                        data.Add(b);
                        currentPosition++;
                    }
                    else
                    {
                        errorData();
                    }
                    break;

                case 2:

                    BLEcommand b1;
                    if (!Enum.TryParse(b.ToString(), out b1))
                    {
                        errorData();
                        break;
                    }
                    data.Add(b);
                    ble = BLEData.CreateBle(b1);
                    currentPosition++;

                    break;

                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    data.Add(b);
                    currentPosition++;

                    break;

                case 10:
                    data.Add(b);
                    try
                    {
                        dataLength = BLEData.byteToInt64(data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10]);
                    }
                    catch
                    {
                        errorData();
                        break;
                    }

                    foreach (var d in data)
                    {
                        writeRet = ble.writeByte(d);
                    }
                    currentPosition++;
                    //  dataLength = BLE.BLEData.getInt16(data[2], data[3]);
                    break;


                default:
                    writeRet = ble.writeByte(b);
                    currentPosition++;

                    break;
                }
                if (writeRet == 0)
                {
                    successData();
                    writeRet = -1;
                }
            }
        }