我正在尝试创建一个可以调用的可执行文件,并从 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 来自这里:http://nuget.org/packages/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);
}
}
}
Scale.cs
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;
}
}
}
}
最佳答案
你所拥有的不会编译,因为你并不总是返回一些东西。根据错误消息,这就是您真正需要的。我找不到任何关于 HidDevice
或 HidDevices
的引用,所以我不能说这将绝对确定地工作。
public HidDevice[] GetDevices()
{
return HidDevices.Enumerate(0x0B67, 0x555E).Cast<HidDevice>().ToArray();
}
你必须添加这一行才能编译:
using System.Linq;
作为旁注,获取 ReSharper。
关于c# - 从 Fairbanks SCB-9000 USB 秤读取重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11961412/