示例#1
0
        public override async Task <List <Device> > Scan()
        {
            // TODO: Handle mid-scan disconnects
            var output = new List <Device>();

            //if (timestampSize > 0b11) {
            //    throw new ArgumentException($"timestampSize cannot be higher than {0b11}.");
            //}

            //if (timestampResolution > 0b1111) {
            //    throw new ArgumentException($"timestampResolution cannot be higher than {0b1111}.");
            //}

            Console.Write("  Scan init");
            try {
                port.Flush(); // Flush previous output
                port.WriteByte((byte)((byte)Command.Scan | timestampSize));
                port.WriteByte((byte)(timestampResolution & 0xF));
                //await Task.Delay(200);
                uint index_counter = 0;
                byte moreDevs;
                do
                {
                    Console.Write("  1111");
                    byte devClass = moreDevs = await port.ReadByte();

                    devClass &= 1;

                    if ((moreDevs & 0x80) != 0)
                    {
                        var productID = await port.Read(4);

                        var uniqueID = await port.Read(4);

                        Console.WriteLine(BitConverter.ToUInt32(productID, 0));

                        if (devClass == (byte)DeviceClass.Buffer)
                        {
                            var shlAmount = await port.ReadByte();

                            var bufferSize = await port.ReadByte() | (await port.ReadByte() << 8);

                            output.Add(new BufferDevice(
                                           this,
                                           BitConverter.ToUInt32(productID, 0),
                                           BitConverter.ToUInt32(uniqueID, 0),
                                           index_counter++,
                                           (ulong)bufferSize << shlAmount
                                           ));

                            throw new NotImplementedException("Buffer devices are not implemented yet");
                        }
                        else
                        {
                            Console.Write("  2222");
                            var fieldCount = await port.ReadByte();

                            //Console.WriteLine($"> There are {fieldCount} fields");
                            var fields = new List <FieldType>();
                            for (int i = 0; i < fieldCount; i += 4)
                            {
                                var currentFieldTypes = await port.ReadByte();

                                for (int j = i; j < Math.Min(i + 4, fieldCount); j++)
                                {
                                    //Console.WriteLine($"> New field {currentFieldTypes & 0b11}");
                                    fields.Add((FieldType)(currentFieldTypes & 0b11));
                                    currentFieldTypes >>= 2;
                                }
                            }
                            Console.Write("  3333");

                            output.Add(new PeripheralDevice(
                                           this,
                                           BitConverter.ToUInt32(productID, 0),
                                           BitConverter.ToUInt32(uniqueID, 0),
                                           index_counter++,
                                           fields
                                           ));
                        }
                    }
                } while (moreDevs != 0);
            } catch (InvalidOperationException) {
                throw new PortClosedException();
            };
            return(output);
        }