2012-05-02 52 views

回答

2

我不確定這是否適用於您的特定型號,但有一篇文章位於http://nicholas.piasecki.name/blog/2008/11/reading-a-stamps-com-usb-scale-from-c-sharp/,作者編寫了C#代碼以讀取比例尺,因爲它符合基本的USB HID(人機輸入設備)標準。筆者利用邁克·奧布萊恩的HID庫https://github.com/mikeobrien/HidLibrary

他們開始得到的原始字節:

HidDeviceData inData; 
HidDevice[] hidDeviceList; 
HidDevice scale; 

hidDeviceList = HidDevices.Enumerate(0x1446, 0x6A73); 

if (hidDeviceList.Length > 0) 
{ 
    int waitTries; 

    scale = hidDeviceList[0]; 
    waitTries = 0; 

    scale.Open(); 

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

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

    scale.Close(); 
    scale.Dispose(); 
} 

然後繼續反向工程的有效載荷和構造函數來獲取重量盎司:

private void GetStampsComModel2500iScaleWeight(out decimal? ounces, out bool? isStable) 
{ 
    HidDeviceData inData; 
    HidDevice[] hidDeviceList; 
    HidDevice scale; 

    isStable = null; 
    ounces = null; 

    hidDeviceList = HidDevices.Enumerate(0x1446, 0x6A73); 

    if (hidDeviceList.Length > 0) 
    { 
     int waitTries; 

     scale = hidDeviceList[0]; 
     waitTries = 0; 

     scale.Open(); 
     // For some reason, the scale isn't always immediately available 
     // after calling Open(). Let's wait for a few milliseconds before 
     // giving up. 
     while (!scale.IsConnected && waitTries < 10) 
     { 
      Thread.Sleep(50); 
      waitTries++; 
     } 

     if (scale.IsConnected) 
     { 
      inData = scale.Read(250); 
      ounces = (Convert.ToDecimal(inData.Data[4]) + 
       Convert.ToDecimal(inData.Data[5]) * 256)/10; 
      isStable = inData.Data[1] == 0x4; 
     } 

     scale.Close(); 
     scale.Dispose(); 
    } 
} 

爲了從傳統的ASP頁面/ VBScript中讀取權重(在服務器上,對不對?),最簡單的解決方案似乎是將工作的C#類轉換爲COM組件。有一些教程可以用來創建C#COM組件並在服務器上註冊它,然後你可以從VBScript中調用它,例如:

Dim app 
Set app = Server.CreateObject("MyScaleComponent") 
+0

感謝您的回答,但是這是針對其他型號的。 –