【问题标题】:how to read value from GPIO5 pin of Raspberry PI 3 using C#?如何使用 C# 从 Raspberry PI 3 的 GPIO5 引脚读取值?
【发布时间】:2018-03-05 23:46:36
【问题描述】:

我为 Raspberry PI 3 配置了 Q4XTBLAF300-Q8 这个传感器,它连接到 GPIO5 以根据传感器范围内的任何东西读取值,输入将是高电平。当超出范围时,传感器将处于低电平。但是我不知道如何编写代码来根据 Q4XTBLAF300-Q8 这个传感器状态从 GPIO5 引脚读取值。

那么,您能告诉我如何从 Raspberry PI 3 的 GPIO5 引脚读取值吗?

【问题讨论】:

  • Win10 IoT 还是 Raspian + Mono?
  • @ManfredRadlwimmer,我只使用了 Windows 10 IoT 核心。

标签: c# iot raspberry-pi3 gpio windows-10-iot-core


【解决方案1】:
using Windows.Devices.Gpio;

public void GPIO()

{

       // Get the default GPIO controller on the system

       GpioController gpio = GpioController.GetDefault();

       if (gpio == null)
           return; // GPIO not available on this system


      // Open GPIO 5

      using (GpioPin pin = gpio.OpenPin(5))

      {
        // Latch HIGH value first. This ensures a default value when the pin is set as output

         pin.Write(GpioPinValue.High);

        // Set the IO direction as output
        pin.SetDriveMode(GpioPinDriveMode.Output);

     } // Close pin - will revert to its power-on state

}

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
【解决方案2】:

这里有一段可以参考的代码sn-p:

using Windows.Devices.Gpio;

private const int GPIO_PIN_NUM = 5;

//Initialize gpio    
pin = GpioController.GetDefault().OpenPin(GPIO_PIN_NUM);
pin.SetDriveMode(GpioPinDriveMode.Input);

//Read gpio value    
var pinValue = pin.Read();

要在带有 windows 10 iot 核心的树莓派上控制 GPIO,您可以查看this tutorial

更多示例是here

【讨论】:

  • 丽塔,我使用了相同的代码,它工作正常,但它给出的值将是高或低。但实际上这个 Q4XTBLAF300-Q8 传感器以毫米为单位提供距离输出。你能告诉我如何从这个 Q4XTBLAF300-Q8 传感器读取确切的值。
  • 您的传感器数据表是什么?
  • 此链接用于我的传感器数据表info.bannerengineering.com/cs/groups/public/documents/…
  • 但是它的输出正在发送到显示器,对吗?
  • 是的,丽塔。每当物体靠近传感器时,它就会显示与物体的距离。
猜你喜欢
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2016-12-02
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
相关资源
最近更新 更多