我买了一个能够获取生理参数的传感器;它通过蓝牙协议连接到我的pc,所以当它连接时,会分配一个串行端口(例如命名为COM10)。现在,我正在编写一个简单的例程来实时读取数据:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SewLib;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SewDevice myDevice;
SerialPort comPort;
public Form1()
{
InitializeComponent();
comPort = new SerialPort("COM10");
myDevice = new SewDevice(ref comPort);
myDevice.OnSamplesReceived += new ReceivedSamplesHandler(ReceiveData);
}
private void btstart_Click(object sender, EventArgs e)
{
eErrorClassOpcodes reply = myDevice.StartStreaming();
if (reply == eErrorClassOpcodes.Ok)
{
// ok device is connected and streaming is started
}
}这对我来说是可行的,但现在我会有一个非静态的过程来选择com端口:我的想法是在我的pc检测到的所有端口之间循环,只选择带有蓝牙适配器的端口。
你有什么想法吗?
谢谢
安德里亚
发布于 2014-09-27 04:28:53
自动检测您的串行设备是一个滑坡道。如果您一定要这样做,那么我将创建一个新的helper类,如下所示(未经测试)
class MyAutoDetector
{
public MyAutoDetector(){}
public SewDevice AutoDetect()
{
SerialPort comPort;
SewDevice myDevice = null;
eErrorClassOpcodes reply;
foreach(string portName in SerialPort.GetPortNames().ToList())
{
comPort = new SerialPort("COM10");
myDevice = new SewDevice(ref comPort);
reply = myDevice.StartStreaming();
if (reply == eErrorClassOpcodes.Ok)
{
// ok device is connected and streaming is started
break;
}
}
return myDevice;
}我不知道SewLib,但你应该验证一下波特率,比特,编码等等。通常你用这些来配置你的串口……但也许SewLib可以为您做到这一点。
如果您碰巧知道唯一的PID/VID,那么另一个选择就是:How do I get serial Port device id?
https://stackoverflow.com/questions/26061064
复制相似问题