-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathScale.cs
128 lines (120 loc) · 3.1 KB
/
Scale.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Created by SharpDevelop.
* User: nricciar
* Date: 10/8/2010
* Time: 3:27 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
namespace ScaleInterface
{
using HidLibrary;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
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 GetDevice ()
{
HidDevice hidDevice;
// Stamps.com Scale
hidDevice = HidDevices.Enumerate(0x1446, 0x6A73).FirstOrDefault();
if (hidDevice != null)
return hidDevice;
// Metler Toledo
hidDevice = HidDevices.Enumerate(0x0eb8).FirstOrDefault();
if (hidDevice != null)
return hidDevice;
return null;
}
public bool Connect ()
{
// Find a Scale
HidDevice device = GetDevice();
if (device != null)
return Connect(device);
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 (!this.IsConnected && waitTries < 10)
{
Thread.Sleep(50);
waitTries++;
}
return this.IsConnected;
}
public void Disconnect ()
{
if (this.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 (this.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) - signed byte is power of 10
// Byte 4 == Weight LSB
// Byte 5 == Weight MSB
weight = (decimal?)BitConverter.ToInt16(new byte[] { inData.Data[4], inData.Data[5] }, 0) *
Convert.ToDecimal(Math.Pow(10, (sbyte)inData.Data[3]));
switch (Convert.ToInt16(inData.Data[2]))
{
case 3: // Kilos
weight = weight * (decimal?)2.2;
break;
case 11: // Ounces
weight = weight * (decimal?)0.0625;
break;
case 12: // Pounds
// already in pounds, do nothing
break;
}
isStable = inData.Data[1] == 0x4;
}
}
}
}