在 C# 中通过串行端口访问蓝牙数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15532244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Bluetooth data via Serialport in C#
提问by Rice_Crisp
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.
所以我在 Unity3D 中工作,在 C# 中编程,我听说可以通过 SerialPort 从蓝牙适配器读取数据。我有几个蓝牙 USB 适配器,我尝试使用这种方法将它们连接到我的 PC 上。但是,当我尝试打开 SerialPort 时,收到一条错误消息,指出端口不存在。我只包含了与问题相关的代码,但 portI 是一个字符串(“COM11”或“COM12”),而 PortIn 是 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;
}
}
}
采纳答案by KrisCode
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 端口(在我的情况下为 COM9)与蓝牙设备相同,它就会从蓝牙连接获取数据到独立的 PC 版本(或在编辑器中)它。
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.
配对后,转到蓝牙设置 > COM 端口,然后查看带有设备名称的端口。它可能会说 COM8 或 COM9 或其他什么。如果设备已配对并且代码中的 COM 端口与蓝牙设置中的相同,并且超时数和波特率与您从中发送数据的应用程序中的相同...那么您将获得运行此代码时的某些内容。这只是为了帮助通过蓝牙连接与串行连接。
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();
}
}
回答by Eric Smekens
It should be possible. The bluetooth rfcomm/spp services emulate a serial port. A COM port if it's on Windows. Baudrate doesn't matter in this emulation, it will always go as fast as possible.
应该是可以的。蓝牙 rfcomm/spp 服务模拟串行端口。如果在 Windows 上,则为 COM 端口。在这个仿真中波特率无关紧要,它总是尽可能快地运行。
You need to have the devices paired and connected though. To what device are you connecting? Try to make a connection first with Putty or some terminal application.
不过,您需要配对并连接设备。你连接到什么设备?尝试先与 Putty 或某些终端应用程序建立连接。