通过C#中的Serialport访问Bluetooth数据 [英] Accessing Bluetooth data via Serialport in C#

查看:247
本文介绍了通过C#中的Serialport访问Bluetooth数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Unity3D中工作,用C#编程,听说有人可以通过SerialPort从蓝牙适配器读取数据.我已经尝试使用这种方法在PC上连接多个蓝牙USB适配器.但是,当我尝试打开SerialPort时,出现一条错误消息,指出端口不存在.我只包含了与该问题相关的代码,但是portI是一个字符串("COM11"或"COM12"),而PortIn的类型为SerialPort.

So I'm working in Unity3D, programming in C#, and I heard that one can read data from a Bluetooth adaptor via SerialPort. I have several Bluetooth USB adaptors that I've tried to connect on my PC using this method. However, when I try to open the SerialPort, I get an error message that says port does not exist. I only included the code relevant to the question, but portI is a string ("COM11" or "COM12") and PortIn is of type SerialPort.

void OnGUI() {
    GUI.Label(new Rect(btnX, btnY, btnW, btnH), "PortIn = " + portI);
    if(!connected) {
        for (int i = 0; i<ports.Length; i++) {
            if(GUI.Button(new Rect(btnX, btnY + btnH + (btnH * i), btnW, btnH), ports[i])) {
                portI = ports[i];
            }
        }           
    }       
    if(GUI.Button(new Rect(btnX + (btnW * 2 + 20), btnY, btnW, btnH), "Connect")) {
        portIn = new SerialPort(portI, 9600);               
        portIn.ReadTimeout = 1000;
        if (!portIn.IsOpen) {
            portIn.Open();
        }
        connected = true;
        }
    }       
}

推荐答案

这是我正在处理的一些代码,只要从COM连接到独立的PC版本(或在编辑器中),它就会从蓝牙连接获取数据.配对时,端口(在我的情况下为COM9)与蓝牙设备相同.

Here is some code I'm working on and it gets data from the bluetooth connection to a standalone pc build (or in the editor) as long as the COM port (in my case COM9) is the same as the bluetooth device when you pair it.

配对后,转到蓝牙设置">"COM端口",然后查看带有您设备名称的端口.它可能会说COM8或COM9或其他.如果设备已配对,并且代码中的COM端口与您的蓝牙设置中的相同,并且超时号和波特率与您要从中发送数据的应用程序中的相同,那么您将得到:在运行时从此代码中获取一些信息.这只是为了帮助建立通过蓝牙连接的串行连接.

After you pair it go to Bluetooth Settings > COM Ports and see what port is there with the name of your device. It might say COM8 or COM9 or whatever. If the device is paired and the COM Port is the same in the code as it is in your Bluetooth Settings, AND the timeout number and baud rate are the same as in the application you are sending the data from... then you will get something from this code when you run it. This is just meant to help make a connection to the serial over bluetooth connection.

希望它可以帮助某人.通过阅读这些论坛,我得到了很多很好的建议;)

Hope it helps someone. I've gotten a lot of great advice from reading these forums ;)

using System.Collections;
using System.IO.Ports;

public class checker : MonoBehaviour {

    public static SerialPort sp = new SerialPort("COM9", 9600, Parity.None, 8, StopBits.One);
    public string message, message1;
    public string message2;

    void Start() {
        OpenConnection();   
    }

    void Update() { 
        message2 = sp.ReadLine(); 
    } 

    void OnGUI()    {
        GUI.Label(new Rect(10, 180, 100, 220), "Sensor1: " + message2);
    }

    public void OpenConnection() {
        if (sp != null) 
        {
            if (sp.IsOpen) 
            {
                sp.Close();
                message = "Closing port, because it was already open!";
            }
            else 
            {
                sp.Open(); 
                sp.ReadTimeout = 1000;  
                message = "Port Opened!";
            }
        }
        else 
        {
            if (sp.IsOpen)
            {
                print("Port is already open");
            }
            else 
            {
                print("Port == null");
            }
        }
    }

    void OnApplicationQuit() {
        sp.Close();
    }

}

这篇关于通过C#中的Serialport访问Bluetooth数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆