0

I bought a sensor that is able to acquire phisiological parameters; it is connected to my pc with bluetooth protocol, so when it is connected a serial port (named for example COM10) is assigned. Now I'm writing a simple routine to read the data in real time:

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
}
}

This works for me, but now I would to have a non static procedure for the selection of com port: my idea is to cycle beetween all the ports that my pc detect and select only the port with the bluetooth adapter.

Have you any ideas?

Thanks

Andrea

1 Answers1

0

Auto-detecting your serial devices is a slippery slope. If you absolutely must, then I would just make a new helper class that looks something like this (untested)

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;
    }

I don't know about SewLib but you should verify that baud rate, bits, encoding, etc. Usually you configure your serial port with these.... but perhaps SewLib does that for you.

Another option, if you happen to know the unique PID/VID is answered here: How do I get serial Port device id?

Community
  • 1
  • 1
cory.todd
  • 516
  • 5
  • 14