【发布时间】:2016-12-04 18:45:39
【问题描述】:
我的汽车有一个蓝牙 OBDII 加密狗(品牌是 Veepeak),我正在尝试编写一个可以与之通信的 Windows 应用程序。到目前为止,我似乎能够从我的笔记本电脑连接到设备、发送命令并接收某种响应,但我收到的响应并不是我所期望的。我正在使用 32feet 通信库来处理蓝牙的东西。
这是我用来连接的代码以及我用来发送消息的函数:
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(这些方法是从我找到的一个 OBDII 库中借用代码 here)。
当我发送 AT 命令时,我似乎只会收到我发送的任何内容的回声,而当我发送 PID 时,我会收到一个问号的响应,据我了解,这意味着该命令无效。
我的加密狗可能没有 ELM327 吗?我的蓝牙通信有问题还是我的 UUID 有问题?谢谢。
【问题讨论】:
-
我建议您在您的 PC 上使用诸如 realterm(或任何其他 com 端口终端)之类的工具来首先验证设备是否正常运行。并验证您的设备是什么。有elm327命令查询设备描述AT@1
-
谢谢,我现在使用 com 终端来确定我拥有的是 ELM327v1.5,它似乎工作正常。可以直接通过蓝牙流发送消息还是必须打开串口才能通信?