小编Chr*_*uis的帖子

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

我正在尝试读取超声波传感器(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() == …
Run Code Online (Sandbox Code Playgroud)

c# sensor iot windows-10 raspberry-pi2

8
推荐指数
1
解决办法
3924
查看次数

标签 统计

c# ×1

iot ×1

raspberry-pi2 ×1

sensor ×1

windows-10 ×1