【问题标题】:Serial Port connection problems串口连接问题
【发布时间】:2014-09-27 04:28:53
【问题描述】:

我买了一个能够获取生理参数的传感器;它通过蓝牙协议连接到我的电脑,所以当它连接时,会分配一个串行端口(例如 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 端口:我的想法是在我的电脑检测到的所有端口之间循环,并只选择带有蓝牙适配器的端口。

你有什么想法吗?

谢谢

安德烈亚

【问题讨论】:

    标签: bluetooth serial-port cycle


    【解决方案1】:

    自动检测您的串行设备是一个滑坡。如果你绝对必须,那么我会创建一个看起来像这样的新帮助类(未经测试)

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-20
      • 2011-09-20
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多