如何在C#中读取通过USB端口传输的数据? [英] How can I read data coming via USB port in C# ?

查看:721
本文介绍了如何在C#中读取通过USB端口传输的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从通过USB端口连接的数字秤获得重量。



我尝试过:



我只获得了连接设备信息。

静态列表< usbdeviceinfo> GetUSBDevices()
{
List< usbdeviceinfo> devices = new List< usbdeviceinfo>();

var searcher = new ManagementObjectSearcher(@SELECT * FROM Win32_PnPEntity,其中DeviceID LikeUSB%);


foreach(searcher.Get()中的var设备)
{

devices.Add(新USBDeviceInfo(
$ b $) b(字符串)device.GetPropertyValue(DeviceID),
(字符串)device.GetPropertyValue(PNPDeviceID),
(字符串)device.GetPropertyValue(Description),
( string)device.GetPropertyValue(Name)

));
}

返回设备;
}



 public USBDeviceInfo(string deviceID,string pnpDeviceID,string description,string name)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
this.Name = name;
}
公共字符串DeviceID {get;私人集; }
public string PnpDeviceID {get;私人集; }
public string说明{get;私人集; }
public string Name {get;私人集; }

解决方案

当您通过USB连接到秤时,您需要使用SerialPort并从秤中读取数据为ASCII / Unicode数据和处理它 - USB端口的属性将告诉你没有用。



从规模制造商文档/网站开始他们经常有可能有用的示例代码你

I want to get weight from digital scale connected via USB port.

What I have tried:

I only got connected device information.

static List<usbdeviceinfo> GetUSBDevices()
    {
        List<usbdeviceinfo> devices = new List<usbdeviceinfo>();

        var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%""");


        foreach (var device in searcher.Get())
        {
            
            devices.Add(new USBDeviceInfo(
           
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description"),
            (string)device.GetPropertyValue("Name")
            
            ));
        }

        return devices;
    }


public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name)
    {
        this.DeviceID = deviceID;
        this.PnpDeviceID = pnpDeviceID;
        this.Description = description;
        this.Name = name;
    }
    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }
    public string Name { get; private set; }

解决方案

When you connect to a scale via USB you need to use a SerialPort and read the data from the scale as ASCII / Unicode data and process it yourself - the properties of the USB port will tell you nothing useful.

Start with the scale manufacturers documentation / website they often have sample code that may help you.


这篇关于如何在C#中读取通过USB端口传输的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆