Accessing Bluetooth data via Serialport in C#(在 C# 中通过串口访问蓝牙数据)
问题描述
所以我在 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配对时,端口(在我的情况下为 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# 中通过串口访问蓝牙数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中通过串口访问蓝牙数据
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01