C# Decoding X10 Code

235 views Asked by At

I like to decode X10 Code on the Raspberry Pi using C# on Windows 10 IoT but I haven no experience with RF decoding, so this is new territory for me.

I came across this post and I tried to convert this into C# code, but I had no success. Does anyone know how to decode this X10 Code correctly using C#, or can someone point me to the right Protocol specifications.

Here is the code I am currently using, however the ValueChanged Event is not called.

public static void Sniff(uint gpioPinNumb)
        {
            Task.Run(() => {
                using (GpioPin pin = GpioController.GetDefault().OpenPin((int)gpioPinNumb, GpioSharingMode.Exclusive))
                {
                    pin.SetDriveMode(GpioPinDriveMode.Input);

                    Stopwatch sw = Stopwatch.StartNew();
                    long elapsedMicrons = 0;
                    int[] states = new int[67];
                    int[] durations = new int[67];
                    uint changeCount = 0;
                    bool lockPassed = false;
                    bool isLock = false;

                    pin.ValueChanged += (GpioPin sender, GpioPinValueChangedEventArgs args) =>
                    {
                        elapsedMicrons = sw.ElapsedTicks / 10;
                        sw.Restart();

                        //Debug.WriteLine(elapsedMicrons);

                        if (elapsedMicrons > 25000 && !lockPassed && !isLock)
                        {
                            //X10 lock started
                            changeCount = 0;
                            durations[changeCount++] = (int)elapsedMicrons;
                            isLock = true;
                            Debug.WriteLine("Lock Started");
                            Debug.WriteLine("");
                        }
                        else if (isLock)
                        {
                            if (changeCount >= durations.Length)
                            {
                                isLock = false;
                                changeCount = 0;
                                Debug.WriteLine("===============================");
                                for (int i = 0; i < durations.Length; i++)
                                {
                                    Debug.Write(durations[i++]);
                                    Debug.Write(" ");
                                }
                                Debug.WriteLine("");
                            }
                            else
                            {
                                durations[changeCount++] = (int)elapsedMicrons;
                            }
                        }
                    };
                }
            });
        }
1

There are 1 answers

2
Rita Han On

Here is the code I am currently using, however the ValueChanged Event is not called.

This issue caused by using statement that the C# "using" statement results in a call to Dispose(). This is the same as Close(). It also causes the object itself to go out of scope as soon as Dispose is called.

For safety you can move pin out of Sniff method like this:

    private static GpioPin pin;
    public static void Sniff(uint gpioPinNumb)
    {
        Task.Run(() => {

                pin = GpioController.GetDefault().OpenPin((int)gpioPinNumb, GpioSharingMode.Exclusive);
                pin.SetDriveMode(GpioPinDriveMode.Input);

                    ...
                    ...