无法引用HidLibrary和Scale namesapce

时间:2016-03-22 15:08:12

标签: c#

我确实导入了HidLibrary 3.2.33.0并为应用程序安装了它。问题是可以找到命名空间HidLibrary,其中包括Scale命名空间。当我使用程序包管理器控制台时,它告诉我已经安装了“hidLibrary” 我的代码中有两个类。我的问题是如何知道它是否已正确安装。如果已安装,我如何阅读FairBanks Scale的usb输入。提前致谢。 这是我到目前为止所尝试的......

ReadScaleClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HidLibrary; // The type or namespace name 'HidLibrary' could not be found(are you missing a using directive or an assembly reference?)
using Scale;  // The Type or namespace name 'Sccale' could not be found (are you missing a using directive or an assembley reference?)

namespace SDDAPP
{
    class ReadScaleClass
    {
        public static void Main(string[] args)
        {
            decimal? myweight;
            bool? mybooleanvalue;

            USBScale s = new USBScale();
            s.Connect();

            {
                if (s.IsConnected)
                {
                    s.GetWeight(out myweight, out mybooleanvalue);
                    s.DebugScaleDate();
                    Console.WriteLine("Weight: {0:0.00} LBS", myweight);
                }
                else
                {
                    Console.WriteLine("No Scale Connected");
                }

                s.Disconnected();
                Thread.Sleep(500);
            }
        }
    }
}

The other class is as follows

USBScale.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HidLibrary;
using System.Linq;//The using directive 'System.Linq' appeared previously in this namespace

namespace SDDAPP
{
    class USBScale
    {
        public bool IsConnected
        {
            get
            {
                return USBScale == null ? false : USBScale.IsConnected;
            }

        }

        public decimal myscaleStatus
        {
            get
            {
                return inData.Data[1];
            }
        }

        public decimal myscaleWeightunts
        {
            get
            {
                return inData.Data[2];
            }
        }

        private HidDevice scale;
        private HidDeviceData inData;

        public HidDevice[] GetDevices()
        {
            //FairBanksScale
            return HidDevices.Enumerate(0x0B67, 0x555E).Cast<HidDevice>().ToArray();
        }

        public bool Connect()
        {
            //Find the scale
            HidDevice[] deviceList = GetDevices();

            if (deviceList.Length > 0)
            {
                return Connect(deviceList[0]);
            }
            else
            {
                return false;
            }
        }

        public bool Connect(HidDevice device)
        {
            scale = device;
            int waitTries = 0;

            scale.OpenDevice();

            //sometimes the scale is not ready so wait after
            //Open() wait till is ready

            while (!scale.IsConnected && waitTries < 10)
            {
                Thread.Sleep(50);
                waitTries++;
            }

            return scale.IsConnected;
        }

        public void Disconnected()
        {
            if (scale.IsConnected)
            {
                scale.CloseDevice();
                scale.Dispose();
            }
        }

        public void DebugScaleDate()
        {
            for (int i = 0; i < inData.Data.Length; i++)
            {
                Console.WriteLine("Byte {0}: {1}", i, inData.Data[i]);
            }
        }

        public void GetWeight(out Decimal? weight, out bool? isStable)
        {
            weight = null;
            isStable = false;

            if (scale.IsConnected)
            {
                inData = scale.Read(250);

                weight = (Convert.ToDecimal(inData.Data[4]) + Convert.ToDecimal(inData.Data[5]) * 256) / 100;

                switch (Convert.ToInt16(inData.Data[2]))
                {
                    case 1: // in Kilo
                        weight = weight * (decimal?)2.2;
                        break;
                    case 2: // Ounces
                        weight = weight * (decimal?)0.625;
                        break;
                    case 3: // Pounds
                        //Doing nothing
                        break;

                }

                isStable = inData.Data[1] == 0x4;
            }
        }

        public HidDevice[] GetDevices()
        {
            HidDevice[] hidDeviceList;
           //FairBanks Scale
            hidDeviceList = HidDevices.Enumerate("0x0B67, 0x555E");
            if (hidDeviceList.Length > 0)
                return hidDeviceList;
        }
    }
}

0 个答案:

没有答案