Exemplo n.º 1
0
        /*
         *  § Prefix [0x7a, 0x00]
         *              § Response? [0x02, 0x80]
         *              § Extension Length? [0xLL, 0xHH]
         *              § Packet Number [0xLL, 0XHH]
         *              § Zero Reserved [0x00, 0x00]
         *              § CheckSum [FFFF - Sum(Prefix…ZeroReserve) % FFFF]
         *              § Extensions --- (Number of Bytes referenced by Extension Length
         *                      □ Response Type
         *                              ® [0x00, 0x08] Return Data ReadZero
         *                                      ◊ Reserved [0x00, 0x00]
         *                                      ◊ 0c00 Reserved? [0x0c, 0x00]
         *                                      ◊ Reserved [0x00, 0x00]
         *                                      ◊ 0c00 Reserved? [0xLL, 0xHH] {ambient}
         *                                      ◊ Reserved [0x00, 0x00]
         *                                      ◊ 0c00 Reserved? [0xLL, 0xHH] {accumulated}
         *                                      ◊ Reserved [0x00, 0x00]
         *                                      ◊ 0c00 Reserved? [0xLL, 0xHH] {cpm}
         *                                      ◊ Reserved [0x00, 0x00]
         *                                      ◊ CheckSum [FFFF - Sum(Request type…ZeroReserve) % FFFF]
         *                              ® [0x01, 0x00] Return Serial Number/Version Read
         *                                      ◊ 0c00 Reserved? [0x0c, 0x00]
         *                                      ◊ CheckSum [FFFF - Sum(Request type…ZeroReserve) % FFFF]
         *                              ® [0x02, 0x08] Acknowledge Write Settings
         *                                      ◊ Zero Reserved [0x00, 0x00]
         *                                      ◊ CheckSum [FFFF - Sum(Request type…ZeroReserve) % FFFF]
         *                              ® [0x01, 0x08] Return Read Settings
         *                                      ◊ Zero Reserved [0x00, 0x00]
         *                                      ◊ Target Value [0x05, 0x00]
         *                                      ◊ Zero Reserved [0x00, 0x00]
         *                                      ◊ Alarm Setting [0x03] {0x01 = Vibration, 0x02 = 0 Audio}
         *                                      ◊ Threshold [0xLL, 0xHH]
         *                                      ◊ Zero Reserved [0x00, 0x00, 0x00]
         *                                      ◊ CheckSum [FFFF - Sum(Request type…ZeroReserve) % FFFF]
         */
        public IRadexObject Decode(ReadOnlySequence <byte> sequence)
        {
            Span <byte> data = new byte[sequence.Length];

            sequence.CopyTo(data);
            var values = MemoryMarshal.Cast <byte, ushort>(data);

            if (values[2] == 0)
            {
                var          read   = MemoryMarshal.Cast <byte, DevicePing>(data);
                IRadexObject result = read[0];
                return(result);
            }
            else
            {
                switch (values[6])
                {
                case 0x00_01:
                {
                    var          read   = MemoryMarshal.Cast <byte, ReadSerialNumberResponse>(data);
                    IRadexObject result = read[0];
                    return(result);
                }

                case 0x08_00:
                {
                    var          read   = MemoryMarshal.Cast <byte, ReadValuesResponse>(data);
                    IRadexObject result = read[0];
                    return(result);
                }

                case 0x08_01:
                {
                    var          read   = MemoryMarshal.Cast <byte, ReadSettingsResponse>(data);
                    IRadexObject result = read[0];
                    return(result);
                }

                case 0x08_02:
                {
                    var          read   = MemoryMarshal.Cast <byte, WriteSettingsResponse>(data);
                    IRadexObject result = read[0];
                    return(result);
                }

                case 0x08_03:
                {
                    var          read   = MemoryMarshal.Cast <byte, ResetAccumulatedResponse>(data);
                    IRadexObject result = read[0];
                    return(result);
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Exemplo n.º 2
0
        public static void Execute()
        {
            //var ws = Enumerable.Range(0, 100)
            //                   .Select(i => new WriteSettingsRequest((ushort)i, (AlarmSettings)(i % 4), (ushort)(i * 10)))
            //                   .ToArray();
            //var sws = ws.AsSpan();
            //var swsd = MemoryMarshal.Cast<WriteSettingsRequest, byte>(sws);
            //var swsa = swsd.ToArray();

            var factory = new RadexOneFactory();
            var decoder = new RadexOneDecoder();

            var segmenter = factory.GetSegmenter(data =>
            {
                var result = decoder.Decode(data);
                Console.WriteLine(result);

                return(Task.FromResult(0));
            });

            var ports = SerialPort.GetPortNames().OrderBy(s => s);

            foreach (var port in ports)
            {
                Console.WriteLine(port);
            }

            Console.WriteLine($"Enter Port: (Default { ports.FirstOrDefault()})");
            var portName   = Console.ReadLine();
            var serialPort = new PortProvider().GetRadexOnePort(string.IsNullOrWhiteSpace(portName) ? ports.FirstOrDefault() : portName);

            using (var port = serialPort)
                using (var cts = new CancellationTokenSource())
                {
                    port.Open();

                    Console.Write("Enter to exit");

                    Task.WaitAll(
                        Task.Run(async() => await Program.ReadLineAsync().ContinueWith(t => cts.Cancel(false))),
                        Task.Run(async() =>
                    {
                        while (!cts.IsCancellationRequested)
                        {
                            try
                            {
                                await port.BaseStream.Follow().With(segmenter).RunAsync(cts.Token);
                                cts.Cancel(true);
                            }
                            catch (Exception ex)
                            {
                                Console.Error.WriteLine(ex.Message);
                            }
                        }
                    }),
                        Task.Run(async() =>
                    {
                        uint x = 0;
                        while (!cts.IsCancellationRequested)
                        {
                            x++;
                            try
                            {
                                IRadexObject requestObject = (x % 10) switch
                                {
                                    1 => new ReadSerialNumberRequest(x),
                                    // 2 => new ReadSerialNumberRequest(x),

                                    // 3 => new DevicePing(x),
                                    // 0 => new DevicePing(x),

                                    // 4 => new WriteSettingsRequest(x, AlarmSettings.Audio | AlarmSettings.Vibrate, 10),
                                    // 5 => new WriteSettingsRequest(x, AlarmSettings.Audio | AlarmSettings.Vibrate, 10),
                                    // 6 => new WriteSettingsRequest(x, AlarmSettings.Audio | AlarmSettings.Vibrate, 10),
                                    // 4 => new WriteSettingsRequest(x, AlarmSettings.Audio, 30),
                                    7 => new ReadSettingsRequest(x),

                                    //8 => new ResetAccumulatedRequest(x),

                                    _ => new ReadValuesRequest(x)
                                };
                                var requestBuffer = new byte[Marshal.SizeOf(requestObject)];
                                IntPtr ptr        = Marshal.AllocHGlobal(requestBuffer.Length);
                                Marshal.StructureToPtr(requestObject, ptr, true);
                                Marshal.Copy(ptr, requestBuffer, 0, requestBuffer.Length);
                                Marshal.FreeHGlobal(ptr);

                                var hex = requestBuffer.ToHexString();

                                //7BFF 2000 _600 1800 ____ 4600 __08 _C00 F3F7
                                await port.BaseStream.WriteAsync(requestBuffer, 0, requestBuffer.Length, cts.Token);
                            }
                            catch (Exception ex)
                            {
                                Console.Error.WriteLine(ex.Message);
                            }
                            if (!cts.IsCancellationRequested)
                            {
                                await Task.Delay(1000);
                            }
                        }
                    })
                        );
                }
        }