我的车上有一个蓝牙OBDII加密狗(品牌为Veepeak),并且我正在尝试编写一个可以与其通信的Windows应用程序。到目前为止,看来我已经能够从笔记本电脑连接到设备,发送命令并接收某种响应,但是我收到的响应不是我所期望的。我正在使用32feet通信库来处理Bluetooth内容。
这是我用来连接的代码,以及我用来发送消息的功能:
BluetoothClient client;
Stream stream;
client = new BluetoothClient();
Guid uuid = new Guid("00001101-0000-1000-8000-00805f9b34fb");
client.BeginConnect(SelectedDevice.DeviceAddress, uuid, bluetoothClientConnectCallback, client);
private void bluetoothClientConnectCallback(IAsyncResult result)
{
client = (BluetoothClient)result.AsyncState;
client.EndConnect(result);
clientConnected = true;
stream = client.GetStream();
UIWriteLine("Client connected");
}
private string sendMessage(string message)
{
byte[] encodedMessage = Encoding.ASCII.GetBytes(message);
stream.Write(encodedMessage, 0, encodedMessage.Length);
Thread.Sleep(100);
int count = 0;
byte[] buffer = new byte[1024];
string retVal = string.Empty;
count = stream.Read(buffer, 0, buffer.Length);
retVal += Encoding.ASCII.GetString(buffer, 0, count);
return retVal.Replace("\n", "");
}
private string getValue(string pid)
{
byte[] encodedMessage = Encoding.ASCII.GetBytes(pid + "\r");
stream.Write(encodedMessage, 0, encodedMessage.Length);
Thread.Sleep(100);
bool cont = true;
int count = 0;
byte[] buffer = new byte[1024];
string retVal = string.Empty;
while (cont)
{
count = stream.Read(buffer, 0, buffer.Length);
retVal += Encoding.ASCII.GetString(buffer, 0, count);
if (retVal.Contains(">"))
{
cont = false;
}
}
return retVal.Replace("\n", "");
}
我使用sendMessage方法发送AT命令,并使用getValue方法获取特定的PID(这些方法是从我发现here的OBDII库中借用代码)。
当我发送AT命令时,我似乎只会得到我发送的任何内容的回声,而当我发送PID时,我得到的是单个问号的响应,据我所知,这意味着该命令无效。
我的加密狗可能没有ELM327吗?我的蓝牙通信有问题吗?还是UUID错误?谢谢。
最佳答案
我也有一个Veepeak。在相关评论中强烈推荐它来使用,并且可以与可用的试用应用程序和我的android手机一起很好地工作。但是,我在自己的python应用程序中努力进行初始化。
在我在raspi上开发的基于python的应用程序上,我必须打开一个“虚拟”串行端口,该端口通过其mac地址映射到先前配对的设备。 Mac地址的配对和注释是在操作系统级别使用通用蓝牙工具完成的。
关于c# - 与蓝牙OBDII加密狗进行通讯(我认为是ELM327),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38669163/