使用覆盆子pi和python,我们使用输入引脚从旋转编码器获取数据。将会有第二个程序在单声道上运行c#,并将在某些时间间隔从python代码读取这些数据。&#39 ;只要编码器旋转,创建一个不断读取io端口的守护进程的最佳方法是什么? 编码器读取器使用while while while循环来读取数据,例如:
while True:
encoderPinA=GPIO.input(8)
encoderPinB=GPIO.input(9)
if (encoderPinA == True) and (oldPinA == False):
if (encoderPinB == False):
Position=Position+inc
else:
Position=Position-inc
print Position ..so on...
还有什么是将这些数据从python传输到c#的最佳方法?我打算在某些时间间隔从python写入文件并从c#中读取数据,但它不会非常敏感。
答案 0 :(得分:1)
我担心这可能为时已晚,但我最近使用Raspberry-Sharp-IO库将一些旋转编码器代码从PigPio库(http://abyz.co.uk/rpi/pigpio/ex_rotary_encoder.html)移植到C#:https://github.com/raspberry-sharp/raspberry-sharp-io。这将消除对Python的要求,并将所有内容保持在一个屋檐下。
旋转编码器针脚连接到连接器针脚24(由库引用为Pin08),接地和连接器针脚26(由库引用为Pin7):
using System;
using Raspberry.IO.GeneralPurpose;
namespace GPIOTesting
{
class Program
{
//State of Pin08
private static bool levA = false;
//State of Pin7
private static bool levB = false;
//The name of the last GPIO pin to fire a PinStatusChanged event
private static string lastGpio = String.Empty;
static void Main(string[] args)
{
//Declare our pins (connector 24 and 26 / processor 08 and 7) as INPUT pins, and apply pull-up resistors
var pin1 = ConnectorPin.P1Pin24.Input().PullUp();
var pin2 = ConnectorPin.P1Pin26.Input().PullUp();
//Create the settings for the connection
var settings = new GpioConnectionSettings();
//Interval between pin checks. This is *really* important - higher values lead to missed values/borking. Lower
//values are apparently possible, but may have 'severe' performance impact. Further testing needed.
settings.PollInterval = TimeSpan.FromMilliseconds(1);
//Create a new GpioConnection with the settings per above, and including pin1 (24) and pin2 (26).
var connection = new GpioConnection(settings, pin1, pin2);
//Integer storing the number of detents turned - clockwise turns should increase this and vice versa.
var encoderPos = 0;
//Add an event handler to the connection. If either pin1 or pin2's value changes this will fire.
connection.PinStatusChanged += (sender, eventArgs) =>
{
//If pin 24 / Pin08 / pin1 has changed value...
if (eventArgs.Configuration.Pin == ProcessorPin.Pin08)
{
//Set levA to this pin's value
levA = eventArgs.Enabled;
}
//If any other pin (i.e. pin 26 / Pin7 / pin2) has changed value...
else
{
//Set levB to this pin's value
levB = eventArgs.Enabled;
}
//If the pin whose value changed is different to the *last* pin whose value changed...
if (eventArgs.Configuration.Pin.ToString() != lastGpio)
{
//Update the last changed pin
lastGpio = eventArgs.Configuration.Pin.ToString();
//If pin 24 / Pin08 / pin1's value changed and its value is now 0...
if ((eventArgs.Configuration.Pin == ProcessorPin.Pin08) && (!eventArgs.Enabled))
{
//If levB = 0
if (!levB)
{
//Encoder has turned 1 detent clockwise. Update the counter:
encoderPos++;
Console.WriteLine("UP: " + encoderPos);
}
}
//Else if pin 26 / Pin7 / pin2's value changed and its value is now 1...
else if ((eventArgs.Configuration.Pin == ProcessorPin.Pin7) && (eventArgs.Enabled))
{
//If levA = 1
if (levA)
{
//Encoder has turned 1 detent anti-clockwise. Update the counter:
encoderPos--;
Console.WriteLine("DOWN: " + encoderPos);
}
}
}
};
}
}
}