我使用称重传感器,HX711放大器和树莓派pi3。 我正在尝试使用此处的库https://github.com/ms-iot/hx711在c#uwp中创建一个具有多个窗口的应用程序 但是每次我回到读取HX711放大器传输的值的页面时,都会收到此错误:
引发的异常:Microsoft.Maker.Devices.Hx711.winmd中的“ System.IO.FileLoadException” WinRT信息:Pin'当前以不兼容的共享模式打开。确保此应用程序或其他应用程序尚未使用此引脚。
我知道此错误是由于引脚在先前实例中打开的事实所致。我不知道如何检查它们是否打开,如果它们打开,我将关闭它们,否则将帮助我继续执行该应用程序。
下面是我需要读取值的页面:
using System;
using System.Linq;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.System;
using Windows.UI;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Automation.Peers;
using Windows.Storage;
using Microsoft.Maker.Devices.Hx711;
namespace push_csharp_universal
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
ApplicationView view = ApplicationView.GetForCurrentView();
//view.TryEnterFullScreenMode();
MainPage_Loaded();
}
public string epn = "";
public string op = "";
public string f1 = null;
public string f2 = null;
public double r = 0;
public float s = 0;
public int x = 1;
Hx711 device;
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
Main_page.Background = new SolidColorBrush(Colors.Green);
device = null;
device = new Hx711();
op_text_view.Text = "";
pn_text_view.Text = "";
op_text_box_scan.Text = "";
pn_text_box_scan.Text = "";
op_text_box_scan.Opacity = 1;
op_text_box_scan.Focus(FocusState.Programmatic);
op_text_view.Opacity = 0;
op_view_lbl.Opacity = 0;
pn_text_box_scan.Opacity = 0;
pn_view_lbl.Opacity = 0;
pn_text_view.Opacity = 0;
}
private async void MainPage_Loaded()
{
Main_page.Background = new SolidColorBrush(Colors.Green);
op_text_view.Text = "";
pn_text_view.Text = "";
op_text_box_scan.Text = "";
pn_text_box_scan.Text = "";
op_text_box_scan.Opacity=1;
op_text_box_scan.Focus(FocusState.Programmatic);
op_text_view.Opacity = 0;
op_view_lbl.Opacity = 0;
pn_text_box_scan.Opacity = 0;
pn_view_lbl.Opacity = 0;
pn_text_view.Opacity = 0;
f_citit.Text = s.ToString();
device = new Hx711();
device.Begin();
while (r < 9.00)
{
double g = device.Grams;
r = ((g / 5546704.667) - 0.03) / 0.029078887;
s = (float)(Math.Round((double)r, 2));
f_citit.Text = s.ToString();
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(.2));
x++;
}
}
这是我使用的hx711库代码:
using Windows.Devices.Gpio;
namespace Microsoft.Maker.Devices.Hx711
{
public sealed class Hx711
{
int clockPinNumber=23;
int dataPinNumber=24;
GpioPin clockPin;
GpioPin dataPin;
bool available = false;
public bool Begin()
{
GpioController gpio = GpioController.GetDefault();
if (gpio == null)
{
available = false;
return false;
}
clockPin = gpio.OpenPin(23, GpioSharingMode.Exclusive);
clockPin.SetDriveMode(GpioPinDriveMode.Output);
clockPin.Write(GpioPinValue.Low);
dataPin = gpio.OpenPin(24, GpioSharingMode.Exclusive);
dataPin.SetDriveMode(GpioPinDriveMode.Input);
available = true;
return true;
}
public double Grams
{
private set { }
get
{
if (!available) { return 0.0f; }
lock (this)
{
//TODO: Figure out how mystic ADC units converts to Grams
return ReadData();
}
}
}
private int ReadData()
{
uint value = 0;
byte[] data = new byte[4];
for (; GpioPinValue.Low != dataPin.Read() ;);
data[1] = ShiftInByte();
data[2] = ShiftInByte();
data[3] = ShiftInByte();
clockPin.Write(GpioPinValue.High);
clockPin.Write(GpioPinValue.Low);
if (0x80 == (data[1] & 0x80))
{
data[0] = 0xFF;
} else {
data[0] = 0x00;
}
value = (uint)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]);
value = ~value;
return (int)(++value);
}
private byte ShiftInByte()
{
byte value = 0x00;
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 7);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 6);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 5);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 4);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 3);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 2);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)((byte)(dataPin.Read()) << 1);
clockPin.Write(GpioPinValue.Low);
clockPin.Write(GpioPinValue.High);
value |= (byte)dataPin.Read();
clockPin.Write(GpioPinValue.Low);
return value;
}
}
}
答案 0 :(得分:1)
首先,您应该检查与某个引脚的连接是否尝试建立两次。其次,您可以尝试在HX711库中添加以下方法,然后在离开当前窗口时调用此方法。
object PrintEntries= "";
string sMegaportsLog = "";
sMegaportsLog = PrintEntries.(new OutGateRepository().
LogMegaportHold(lblContainerNo.Text, sData, GUser));