超声波传感器raspberry pi 2 c#.net

Chr*_*uis 8 c# sensor iot windows-10 raspberry-pi2

我正在尝试读取超声波传感器(HC-SR04)的距离,但我得到的唯一值是0和265.xx.

我正在使用安装了Windows 10 IoT Core的Raspberry Pi 2.

我用C#编写了代码.

这是超声波传感器类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;

namespace RaspberryPi
{
    class UcSensor
    {
        GpioController gpio = GpioController.GetDefault();

        GpioPin TriggerPin;
        GpioPin EchoPin;

        //Contructor
        public UcSensor(int TriggerPin, int EchoPin)
        {
            //Setting up gpio pin's
            this.TriggerPin = gpio.OpenPin(TriggerPin);
            this.EchoPin = gpio.OpenPin(EchoPin);

            this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
            this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);

            this.TriggerPin.Write(GpioPinValue.Low);
        }

        public double GetDistance()
        {
            ManualResetEvent mre = new ManualResetEvent(false);
            mre.WaitOne(500);

            //Send pulse
            this.TriggerPin.Write(GpioPinValue.High);
            mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
            this.TriggerPin.Write(GpioPinValue.Low);

            //Recieve pusle
            while (this.EchoPin.Read() == GpioPinValue.Low)
            {
            }
            DateTime start = DateTime.Now;

            while (this.EchoPin.Read() == GpioPinValue.High)
            {
            }
            DateTime stop = DateTime.Now;

            //Calculating distance
            double timeBetween = (stop - start).TotalSeconds;
            double distance = timeBetween * 17000;

            return distance;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我还在python中编写了一个脚本来读取超声波传感器的值,然后它可以正常工作但是在c#中我无法正常工作.

在底部,您可以找到调试日志:

'BACKGROUNDTASKHOST.EXE'(CoreCLR:DefaultDomain):已加载'C:\ Program Files\WindowsApps\Microsoft.NET.CoreRuntime.1.0_1.0.22816.1_arm__8wekyb3d8bbwe\mscorlib.ni.dll'.跳过加载符号.模块已经过优化,调试器选项"Just My Code"已启用.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\RaspiCar.winmd'.符号已加载.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.dll'.跳过加载符号.模块已经过优化,调试器选项"Just My Code"已启用.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\WinMetadata\Windows.winmd'.模块没有符号.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.InteropServices.WindowsRuntime.dll'.模块没有符号.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Threading.dll'.模块没有符号.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Diagnostics.Debug.dll'.跳过加载符号.模块已经过优化,调试器选项"Just My Code"已启用.'BACKGROUNDTASKHOST.EXE'(CoreCLR:CoreCLR_UAP_Domain):已加载'C:\ Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.WindowsRuntime.dll'.跳过加载符号.模块已经过优化,调试器选项"Just My Code"已启用.距离:265.7457距离:0距离:0距离:0节目'[2508] BACKGROUNDTASKHOST.EXE'已退出,代码为0(0x0).

Chr*_*uis 7

谢谢你的反应.DateTime是我现在使用秒表类的问题,现在它可以工作了.非常感谢!

工人阶级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;

namespace RaspberryPi
{
    class UcSensor
    {
        GpioController gpio = GpioController.GetDefault();

        GpioPin TriggerPin;
        GpioPin EchoPin;

        public UcSensor(int TriggerPin, int EchoPin)
        {
            this.TriggerPin = gpio.OpenPin(TriggerPin);
            this.EchoPin = gpio.OpenPin(EchoPin);

            this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
            this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);

            this.TriggerPin.Write(GpioPinValue.Low);
        }

        public double GetDistance()
        {
            ManualResetEvent mre = new ManualResetEvent(false);
            mre.WaitOne(500);
            Stopwatch pulseLength = new Stopwatch();

            //Send pulse
            this.TriggerPin.Write(GpioPinValue.High);
            mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
            this.TriggerPin.Write(GpioPinValue.Low);

            //Recieve pusle
            while (this.EchoPin.Read() == GpioPinValue.Low)
            {
            }
            pulseLength.Start();


            while (this.EchoPin.Read() == GpioPinValue.High)
            {
            }
            pulseLength.Stop();

            //Calculating distance
            TimeSpan timeBetween = pulseLength.Elapsed;
            Debug.WriteLine(timeBetween.ToString());
            double distance = timeBetween.TotalSeconds * 17000;

            return distance;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)