C# 从Fairbanks SCB-9000 USB秤读取重量

C# 从Fairbanks SCB-9000 USB秤读取重量,c#,usb,hid,C#,Usb,Hid,我正在尝试创建一个可执行文件,它可以被调用,并从Fairbanks SCB-9000 USB磅秤上传回一个重量。我发现了一些用于不同模型比例的代码,但它对我来说不太管用。我特别对以下方法有问题。它给了我一个错误:“无法隐式地将类型'System.Collections.Generic.IEnumerable'转换为'HidLibrary.HidDevice[]”。我已经尝试了几种方法来强制转换它,但我无法让它工作。这里有什么建议,或者有没有人为这个特定的规模写过代码 谢谢, 抢劫 以下是所讨论的

我正在尝试创建一个可执行文件,它可以被调用,并从Fairbanks SCB-9000 USB磅秤上传回一个重量。我发现了一些用于不同模型比例的代码,但它对我来说不太管用。我特别对以下方法有问题。它给了我一个错误:“无法隐式地将类型'System.Collections.Generic.IEnumerable'转换为'HidLibrary.HidDevice[]”。我已经尝试了几种方法来强制转换它,但我无法让它工作。这里有什么建议,或者有没有人为这个特定的规模写过代码

谢谢,

抢劫

以下是所讨论的方法:

    public HidDevice[] GetDevices()
    {
        HidDevice[] hidDeviceList;

        // Fairbanks Scale
        hidDeviceList = HidDevices.Enumerate(0x0B67, 0x555E);

        if (hidDeviceList.Length > 0)

            return hidDeviceList;

    }
抱歉,我应该补充说我正在从这里使用Mike Obrien的HidLibrary:

用完整代码更新

这是我正在使用的代码

program.cs

using System;
using System.Threading;
using HidLibrary;
using Scale;

namespace ScaleReader
{
    class Program
    {
        public static void Main(string[] args)
        {
            decimal? weight;
            bool? isStable;

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

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

            s.Disconnect();
            Thread.Sleep(500);
        }
    }
}
using HidLibrary;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Text;

namespace Scale
{
    class USBScale
    {
        public bool IsConnected
        {
            get
            {
                return scale == null ? false : scale.IsConnected;
            }
        }
        public decimal ScaleStatus
        {
            get
            {
                return inData.Data[1];
            }
        }
        public decimal ScaleWeightUnits
        {
            get
            {
                return inData.Data[2];
            }
        }
        private HidDevice scale;
        private HidDeviceData inData;

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

        public bool Connect()
        {
            // Find a 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 immedietly after
            // Open() so wait till its ready
            while (!scale.IsConnected && waitTries < 10)
            {
                Thread.Sleep(50);
                waitTries++;
            }
            return scale.IsConnected;
        }
        public void Disconnect()
        {
            if (scale.IsConnected)
            {
                scale.CloseDevice();
                scale.Dispose();
            }
        }
        public void DebugScaleData()
        {
            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);
                // Byte 0 == Report ID?
                // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing)
                // Byte 2 == Weight Unit
                // Byte 3 == Data Scaling (decimal placement)
                // Byte 4 == Weight LSB
                // Byte 5 == Weight MSB

                // FIXME: dividing by 100 probably wont work with
                // every scale, need to figure out what to do with
                // Byte 3
                weight = (Convert.ToDecimal(inData.Data[4]) +
                    Convert.ToDecimal(inData.Data[5]) * 256) / 100;

                switch (Convert.ToInt16(inData.Data[2]))
                {
                    case 3:  // Kilos
                        weight = weight * (decimal?)2.2;
                        break;
                    case 11: // Ounces
                        weight = weight * (decimal?)0.625;
                        break;
                    case 12: // Pounds
                        // already in pounds, do nothing
                        break;
                }
                isStable = inData.Data[1] == 0x4;
            }
        }
    }
}
Scale.cs

using System;
using System.Threading;
using HidLibrary;
using Scale;

namespace ScaleReader
{
    class Program
    {
        public static void Main(string[] args)
        {
            decimal? weight;
            bool? isStable;

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

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

            s.Disconnect();
            Thread.Sleep(500);
        }
    }
}
using HidLibrary;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Text;

namespace Scale
{
    class USBScale
    {
        public bool IsConnected
        {
            get
            {
                return scale == null ? false : scale.IsConnected;
            }
        }
        public decimal ScaleStatus
        {
            get
            {
                return inData.Data[1];
            }
        }
        public decimal ScaleWeightUnits
        {
            get
            {
                return inData.Data[2];
            }
        }
        private HidDevice scale;
        private HidDeviceData inData;

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

        public bool Connect()
        {
            // Find a 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 immedietly after
            // Open() so wait till its ready
            while (!scale.IsConnected && waitTries < 10)
            {
                Thread.Sleep(50);
                waitTries++;
            }
            return scale.IsConnected;
        }
        public void Disconnect()
        {
            if (scale.IsConnected)
            {
                scale.CloseDevice();
                scale.Dispose();
            }
        }
        public void DebugScaleData()
        {
            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);
                // Byte 0 == Report ID?
                // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing)
                // Byte 2 == Weight Unit
                // Byte 3 == Data Scaling (decimal placement)
                // Byte 4 == Weight LSB
                // Byte 5 == Weight MSB

                // FIXME: dividing by 100 probably wont work with
                // every scale, need to figure out what to do with
                // Byte 3
                weight = (Convert.ToDecimal(inData.Data[4]) +
                    Convert.ToDecimal(inData.Data[5]) * 256) / 100;

                switch (Convert.ToInt16(inData.Data[2]))
                {
                    case 3:  // Kilos
                        weight = weight * (decimal?)2.2;
                        break;
                    case 11: // Ounces
                        weight = weight * (decimal?)0.625;
                        break;
                    case 12: // Pounds
                        // already in pounds, do nothing
                        break;
                }
                isStable = inData.Data[1] == 0x4;
            }
        }
    }
}
使用HidLibrary;
使用系统线程;
使用制度;
使用System.Collections.Generic;
使用系统文本;
名称空间比例
{
USBScale类
{
公共图书馆断开了
{
得到
{
返回scale==null?false:scale.IsConnected;
}
}
公共小数比例表
{
得到
{
返回inData.Data[1];
}
}
公共十进制比例加权单位
{
得到
{
返回inData.Data[2];
}
}
专用设备规模;
私人藏红花;
公共设备[]获取设备()
{ 
返回HidDevices.Enumerate(0x0B67,0x555E).Cast().ToArray();
} 
公共布尔连接()
{
//找一个天平
HidDevice[]deviceList=GetDevices();
如果(deviceList.Length>0)
返回连接(deviceList[0]);
其他的
返回false;
}
公共布尔连接(HIDL设备)
{
刻度=装置;
int=0;
scale.OpenDevice();
//有时秤在使用后没有立即准备好
//所以等它准备好
而(!scale.IsConnected&&waittrys<10)
{
睡眠(50);
waits++;
}
返回刻度。断开连接;
}
公共空间断开连接()
{
if(刻度断开)
{
scale.CloseDevice();
scale.Dispose();
}
}
public void DebugScaleData()
{
对于(int i=0;i
您所拥有的不会被编译,因为您并不总是返回某些内容。根据错误消息,这就是您真正需要的。我找不到任何有关
HidDevice
HidDevices
的参考资料,因此我不能说这将绝对有效

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


作为旁注,获取ReSharper。

想在此添加一些注释以帮助其他人。我特别关注代码中关于除法问题的FIXME注释

我一直在与梅特勒·托莱多PS系列天平、PS-60、PS-15和PS-90合作。在HID数据返回中,第三个元素为PS-15返回255,PS-60和PS-90都返回254


使用校准砝码,我发现PS-15需要除以10才能得到正确的砝码,PS-60/90需要除以100。

Close。如果错误确实在抱怨
Enumerate
返回
IEnumerable
(不是
IEnumerable
),那么在将
转换为数组之前,您需要一个
Cast()
,实际上您还必须定义类型;Enumerate显然创建了一个非泛型IEnumerable,您必须使用Cast()或OfType()强键入该IEnumerable。感谢您的快速响应。不幸的是,上述建议不起作用。错误为HidDevice不包含Cast()的定义。还有其他建议吗?你们需要看更多的代码吗?我在这里使用HidLibrary:@user1599271:我安装了包并运行了此代码。
.Cast
在这里是多余的。您描述的错误不能来自此代码,因为它是现在编写的。请用您正在使用的代码更新您的问题。对不起。拿出我是怎么得到的,没有放回去,我