从串行梅特勒托利多数字秤读取重量 [英] Read Weight from a Serial Mettler Toledo Digital Scale
问题描述
我正在尝试从 c# 应用程序中的数字秤读取重量,发现 这个 问题这正是我想要做的但对我来说,下面的函数永远不会运行.
I am trying to read weight from digital scale in c# app, found this question this is exactly what I am trying to do but for me below function never runs.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}
我已在设备管理器中检查过秤,其位置设置为 Port_#0004.Hub_#0003
并且似乎工作正常我不确定秤的端口号,所以我做了
I have checked the scale in device manager, its location is set to Port_#0004.Hub_#0003
and appear to be working fine
I was not sure about port number of the scale so I did
var test = SerialPort.GetPortNames();
并且只有 COM1
被返回
Edit 1:
当我执行 int a = port.ReadByte();
我的应用程序挂起并且执行永远不会从这个语句向前移动.
Edit 1:
When I do int a = port.ReadByte();
my application hangs and execution never moves forward from this statement.
推荐答案
请参阅 this 帖子,我用 Mike 库连接.
Please see this post, I used Mike library to connect.
using System;
using System.Linq;
using System.Text;
using HidLibrary;
namespace MagtekCardReader
{
class Program
{
private const int VendorId = 0x0801;
private const int ProductId = 0x0002;
private static HidDevice _device;
static void Main()
{
_device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
if (_device != null)
{
_device.OpenDevice();
_device.Inserted += DeviceAttachedHandler;
_device.Removed += DeviceRemovedHandler;
_device.MonitorDeviceEvents = true;
_device.ReadReport(OnReport);
Console.WriteLine("Reader found, press any key to exit.");
Console.ReadKey();
_device.CloseDevice();
}
else
{
Console.WriteLine("Could not find reader.");
Console.ReadKey();
}
}
private static void OnReport(HidReport report)
{
if (!_device.IsConnected) { return; }
var cardData = new Data(report.Data);
Console.WriteLine(!cardData.Error ? Encoding.ASCII.GetString(cardData.CardData) : cardData.ErrorMessage);
_device.ReadReport(OnReport);
}
private static void DeviceAttachedHandler()
{
Console.WriteLine("Device attached.");
_device.ReadReport(OnReport);
}
private static void DeviceRemovedHandler()
{
Console.WriteLine("Device removed.");
}
}
}
这篇关于从串行梅特勒托利多数字秤读取重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!