示例#1
0
        private async void SetupScalesStream(BluetoothLEDevice device)
        {
            try
            {
                var service = device.GetGattService(_scalesServiceGuid);

                var weightCharac = service.GetAllCharacteristics().Single(c => c.Uuid == _weightGuid);
                await weightCharac.WriteClientCharacteristicConfigurationDescriptorAsync(
                    GattClientCharacteristicConfigurationDescriptorValue.Notify);

                weightCharac.ValueChanged += (sender, args) =>
                {
                    Debug.WriteLine(args.CharacteristicValue);

                    double weight = (BitConverter.ToInt32(args.CharacteristicValue.ToArray().Reverse().ToArray(), 0) / 10.0) -
                                    500;
                    _scalesStreamer.Send(new ScalesData
                    {
                        DeviceId  = 1,
                        Timestamp = DateTime.Now,
                        Weight    = (float)weight
                    });
                };
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Unable to connect to Scales: {e}");
            }
        }
        private async void SetupWaxStream(BluetoothLEDevice device)
        {
            try
            {
                var sampleRateService = device.GattServices.Single(s => s.Uuid == _sampleRateServiceGuid);

                byte[] sampleRate    = { 50 };
                var    sampleRateCtx = sampleRateService.GetAllCharacteristics().Single(c => c.Uuid == _sampleRateGuid);

                await sampleRateCtx.WriteValueAsync(sampleRate.AsBuffer());

                var characteristics =
                    device.GattServices.Single(s => s.Uuid == _streamServiceGuid).GetAllCharacteristics();

                byte[] stream       = { 1 };
                var    streamCharac = characteristics.Single(c => c.Uuid == _streamGuid);
                await streamCharac.WriteValueAsync(stream.AsBuffer());

                var notifyCharac = characteristics.Single(c => c.Uuid == _notifyGuid);
                await notifyCharac.WriteClientCharacteristicConfigurationDescriptorAsync(
                    GattClientCharacteristicConfigurationDescriptorValue.Notify);

                notifyCharac.ValueChanged += (sender, args) =>
                {
                    _wax9Streamer.Send(ProcessWaxData(device.Name, args.CharacteristicValue.ToArray()));
                };
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Unable to connect to WAX: {e}");
            }
        }
示例#3
0
        void ReadDevice(int deviceId, FedmIscReader device)
        {
            device.SetData(FedmIscReaderID.FEDM_ISC_TMP_B0_CMD, 0x01);
            device.SetData(FedmIscReaderID.FEDM_ISC_TMP_B0_MODE, 0x00);

            try
            {
                device.ResetTable(FedmIscReaderConst.ISO_TABLE);

                device.SendProtocol(0x69);
                device.SendProtocol(0xB0);

                while (device.GetLastStatus() == 0x94)
                {
                    device.SetData(FedmIscReaderID.FEDM_ISC_TMP_B0_MODE_MORE, 0x01);
                    device.SendProtocol(0xB0);
                }

                var transponders = new string[device.GetTableLength(FedmIscReaderConst.ISO_TABLE)];

                if (transponders.Length > 0)
                {
                    for (var i = 0; i < transponders.Length; i++)
                    {
                        device.GetTableData(i, FedmIscReaderConst.ISO_TABLE, FedmIscReaderConst.DATA_SNR,
                                            out transponders[i]);
                    }
                }

                _rfidSendRepository.Send(new Service.Models.RfidData {
                    DeviceId = deviceId.ToString(), Timestamp = DateTime.Now, Transponders = transponders
                });
                Console.WriteLine($"Device {deviceId} read tags: {string.Join(",", transponders)}");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
        }
示例#4
0
 private void StateChanged(object sender, S state)
 {
     _sendRepository.Send(state);
 }
        private async void ConnectToSerialPort()
        {
            DeviceInformationCollection serialDevices;

            while ((serialDevices = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelectorFromUsbVidPid(0x04D8, 0x000A))).Count < 1)
            {
                Debug.WriteLine("Unable to locate...");
            }

            ReceiverConnected.IsChecked = true;

            SerialDevice serialPort;

            while ((serialPort = await SerialDevice.FromIdAsync(serialDevices[0].Id)) == null)
            {
                Debug.WriteLine("Failed to open serial port...");
            }

            SerialPortOpen.IsChecked = true;

            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate     = 9600;
            serialPort.Parity       = SerialParity.None;
            serialPort.StopBits     = SerialStopBitCount.One;
            serialPort.DataBits     = 8;
            serialPort.Handshake    = SerialHandshake.None;

            var dataReader = new DataReader(serialPort.InputStream);
            var buffer     = new byte[1024];

            while (true)
            {
                var bytesRead = await dataReader.LoadAsync((uint)buffer.Length);

                if (bytesRead > 0)
                {
                    try
                    {
                        dataReader.ReadBytes(buffer);
                        Bytes.AddRange(buffer.Take((int)bytesRead));

                        byte[] slipPacket;
                        while ((slipPacket = Slip.ExtractSlipPacket(Bytes)) != null)
                        {
                            var waxPacket = WaxPacketConverter.FromBinary(slipPacket, DateTime.Now);
                            if (waxPacket != null)
                            {
                                if (!Devices.Contains(waxPacket.DeviceId.ToString()))
                                {
                                    Devices.Add(waxPacket.DeviceId.ToString());
                                }
                                foreach (var sample in waxPacket.Samples)
                                {
                                    _waxSendRepository.Send(new Wax3Data
                                    {
                                        DeviceId  = waxPacket.DeviceId,
                                        Timestamp = sample.Timestamp,
                                        AccX      = sample.X,
                                        AccY      = sample.Y,
                                        AccZ      = sample.Z
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                }
            }
        }