Bluetooth 串行端口连接问题

Bluetooth 串行端口连接问题,bluetooth,serial-port,cycle,Bluetooth,Serial Port,Cycle,我买了一个能够获取物理参数的传感器;它通过蓝牙协议连接到我的电脑,因此当它连接时,会分配一个名为COM10的串行端口。 现在我正在编写一个简单的例程来实时读取数据: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syste

我买了一个能够获取物理参数的传感器;它通过蓝牙协议连接到我的电脑,因此当它连接时,会分配一个名为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端口的非静态过程:我的想法是在我的电脑检测到的所有端口之间循环切换,只选择带有蓝牙适配器的端口

你有什么想法吗

谢谢


Andrea

自动检测您的串行设备是一个危险的过程。如果您一定要这样做,那么我将创建一个新的帮助器类,它看起来像这样未经测试

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,请在此处回答: