I use a load-cell bar, an HX711 amplifier, and raspberry pi3. I'm trying to make an application with more than one window in c # uwp using the library here https://github.com/ms-iot/hx711 But every time I go back to the page where I read the values transmitted by the HX711 amplifier I am getting this error:
Exception thrown: 'System.IO.FileLoadException' in Microsoft.Maker.Devices.Hx711.winmd WinRT information: Pin 'is currently opened in an incompatible sharing mode. Make sure this pin is not already in use by this application or other application.
I know this error is due to the fact that the pins were opened in a previous instance. What I do not know is how to check if they are open, and if they are open I will close them, or else to help me in continuing the application.
Below is the page where I need to read the values:
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++;
}
}
and here is the hx711 library code that I use:
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;
}
}
}
Firstly,you should check if the connection to one of your pins tries to build twice. Secondly, you can try to add following method in the HX711 library, and then call this method when leaving current window.
public void Stop()
{
if(clockPin != null )
{
clockPin.Dispose();
}
if(dataPin != null)
{
dataPin.Dispose();
}
}