首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串行端口连接问题

串行端口连接问题
EN

Stack Overflow用户
提问于 2014-09-26 21:43:02
回答 1查看 64关注 0票数 0

我买了一个能够获取生理参数的传感器;它通过蓝牙协议连接到我的pc,所以当它连接时,会分配一个串行端口(例如命名为COM10)。现在,我正在编写一个简单的例程来实时读取数据:

代码语言:javascript
复制
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检测到的所有端口之间循环,只选择带有蓝牙适配器的端口。

你有什么想法吗?

谢谢

安德里亚

EN

回答 1

Stack Overflow用户

发布于 2014-09-27 04:28:53

自动检测您的串行设备是一个滑坡道。如果您一定要这样做,那么我将创建一个新的helper类,如下所示(未经测试)

代码语言:javascript
复制
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?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26061064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档