C#控制树莓派GPIO?
用了Python控制GPIO,用了C控制GPIO,你是否也苦苦寻找c#控制GPIO的方法?
通过多天的实验,最终成功,并把这个帮助类贡献给大家,让C#在Linux自由飞扬!
(本人使用的框架是:Nancyfx + Jexus)
using System; using System.Collections.Generic; using System.Globalization; using System.IO; namespace Nancy.Linux { /// <summary> /// 控制树莓派GPIO类 /// 作者: Fang /// </summary> public static class FileGpio { private enum EnumDirection { In, Out }; private const string GpioPath = "/sys/class/gpio/"; private static readonly List<Gpio> OutExported = new List<Gpio>(); private static readonly List<Gpio> InExported = new List<Gpio>(); //设置Pin的输入或输出 private static void SetupPin(Gpio pin, EnumDirection direction) { //如果包含了,释放引脚 if (OutExported.Contains(pin) || InExported.Contains(pin)) UnexportPin(pin); //输出 File.WriteAllText(GpioPath + "export", pin.Value.ToString(CultureInfo.InvariantCulture)); //设置输入或输出方向 File.WriteAllText(GpioPath + pin.Name + "/direction", direction.ToString().ToLower()); //添加到列表 if (direction == EnumDirection.Out) OutExported.Add(pin); else InExported.Add(pin); } #region Output /// <summary> /// 输出信号 /// </summary> /// <param name="pin">Gpios</param> /// <param name="value">true:HIGH false:LOW</param> public static void OutputPin(Gpio pin, bool value) { if (!OutExported.Contains(pin) || InExported.Contains(pin)) SetupPin(pin, EnumDirection.Out); File.WriteAllText(GpioPath + pin.Name + "/value", value ? "1" : "0"); } /// <summary> /// 输出信号 /// </summary> /// <param name="bcm">BCM编码</param> /// <param name="value">true:HIGH false:LOW</param> public static void OutputPin(int bcm, bool value) { OutputPin(new Gpio(bcm), value); } #endregion #region Input /// <summary> /// 获取输入信号 布尔 /// </summary> /// <param name="pin">GPIO</param> /// <returns></returns> public static bool InputPinByBoolean(Gpio pin) { bool returnValue = false; if (!InExported.Contains(pin) || OutExported.Contains(pin)) SetupPin(pin, EnumDirection.In); string filename = GpioPath + pin.Name + "/value"; if (File.Exists(filename)) { string readValue = File.ReadAllText(filename); if (readValue.Length > 0 && readValue[0] == '1') returnValue = true; } else throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin.Name)); return returnValue; } /// <summary> /// 获取输入信号 布尔 /// </summary> /// <param name="bcm">BCM编码</param> /// <returns></returns> public static bool InputPinByBoolean(int bcm) { return InputPinByBoolean(new Gpio(bcm)); } /// <summary> /// 获取输入信号 double /// </summary> /// <param name="pin">GPIO</param> /// <returns></returns> public static double InputPinByDouble(Gpio pin) { if (!InExported.Contains(pin) || OutExported.Contains(pin)) SetupPin(pin, EnumDirection.In); string filename = GpioPath + pin.Name + "/value"; if (File.Exists(filename)) { string readValue = File.ReadAllText(filename); if (readValue.Length > 0) { double value; if (double.TryParse(readValue, out value)) { return value; } } } else throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin.Name)); return -1; } /// <summary> /// 获取输入信号 double /// </summary> /// <param name="bcm">BCM编码</param> /// <returns></returns> public static double InputPinByDouble(int bcm) { return InputPinByDouble(new Gpio(bcm)); } /// <summary> /// 获取输入信号 string /// </summary> /// <param name="pin">GPIO</param> /// <returns></returns> public static string InputPinByString(Gpio pin) { if (!InExported.Contains(pin) || OutExported.Contains(pin)) SetupPin(pin, EnumDirection.In); string filename = GpioPath + pin.Name + "/value"; if (File.Exists(filename)) { string readValue = File.ReadAllText(filename); if (readValue.Length > 0) { return readValue; } } else throw new Exception(string.Format("Cannot read from {0}. File does not exist", pin.Name)); return ""; } /// <summary> /// 获取输入信号 string /// </summary> /// <param name="bcm">BCM编码</param> /// <returns></returns> public static string InputPinByString(int bcm) { return InputPinByString(new Gpio(bcm)); } #endregion public static void UnexportPin(Gpio pin) { bool found = false; if (OutExported.Contains(pin)) { found = true; OutExported.Remove(pin); } if (InExported.Contains(pin)) { found = true; InExported.Remove(pin); } if (found) { File.WriteAllText(GpioPath + "unexport", pin.Value.ToString(CultureInfo.InvariantCulture)); } } public static void CleanUpAllPins() { for (int p = OutExported.Count - 1; p >= 0; p--) UnexportPin(OutExported[p]); for (int p = InExported.Count - 1; p >= 0; p--) UnexportPin(InExported[p]); } } public class Gpio { public string Name { get; private set; } public int Value { get; private set; } /// <summary> /// 创建一个GPIO /// </summary> /// <param name="value">BCM编码</param> public Gpio(int value) { Name = "gpio" + value; Value = value; } } public static class Gpios { public static readonly Gpio Gpio0 = new Gpio(17); public static readonly Gpio Gpio1 = new Gpio(18); public static readonly Gpio Gpio2 = new Gpio(27); public static readonly Gpio Gpio3 = new Gpio(22); public static readonly Gpio Gpio4 = new Gpio(23); public static readonly Gpio Gpio5 = new Gpio(24); public static readonly Gpio Gpio6 = new Gpio(25); public static readonly Gpio Gpio7 = new Gpio(4); public static readonly Gpio Gpio21 = new Gpio(5); public static readonly Gpio Gpio22 = new Gpio(6); public static readonly Gpio Gpio23 = new Gpio(13); public static readonly Gpio Gpio24 = new Gpio(19); public static readonly Gpio Gpio25 = new Gpio(26); public static readonly Gpio Gpio26 = new Gpio(12); public static readonly Gpio Gpio27 = new Gpio(16); public static readonly Gpio Gpio28 = new Gpio(20); public static readonly Gpio Gpio29 = new Gpio(21); } }
调用(小灯 亮2秒后 熄灭):
FileGpio.OutputPin(Gpios.Gpio0, true); Thread.Sleep(2000); FileGpio.UnexportPin(gpio);
代码改变世界