我正在尝试创建一个可执行文件,该可执行文件可以被调用并从 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
使用完整代码更新...
这是我正在使用的代码...
程序.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);
}
}
}
比例尺.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;
}
}
}
}