0

无法弄清楚如何格式化我发送到 OBD 设备的命令。首先,扭矩工作正常,所以我知道我的代码不是设备的问题。

这是我发送命令的方式:

public void run() {

            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    byte[] command = new byte[5];
                    command[0] = rawToByte(268);
                    //command[1] = rawToByte(269);

                    Log.i("gas", "Command: "+Byte.toString(command[0]));
                    write(command);
    /////////////////////////////////////////    problem with below line               
                    bytes = mmInStream.read(buffer);
                    Log.i("gas", "Response: "+Integer.toString(bytes));
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    break;
                }
            }
        }

这是基于我正在做的一些研究,如果有人能提出一个简单的方法,那将不胜感激。268 是“010C”的十进制等效值,根据 elm327 表,它是发动机转速的代码。

我的 write() 方法:

public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
                mmOutStream.flush();

            } catch (IOException e) { }
        }

我的 rawToByte() 方法:

public byte rawToByte(int b) {
            return (byte) (0xff & b);
        }

响应日志 cat 每次都读取“5”,如果我使用 269 来提高速度,则相同。这可能是我发送错误命令的错误代码。套接字 mmOutStream 上的 write() 方法只需要字节,而我从这里的一些问题中得到的 rawToByte 方法(它没有回答这个问题)只需要整数,所以我不能给它“010C”,但这不应该有区别.

我的应用程序需要不断获取车辆的速度和转速。

任何人都可以指定(简单地)如何正确地向 OBDII Elm327 设备发送命令吗?

4

1 回答 1

0

我不确定您是否仍然需要答案,但我会尝试这样做:

String message = "010D\r";
mmOutStream.write(message.getBytes());
byte[] buffer = new byte[1024];
mmInStream.read(buffer);

然后将缓冲区转换回字符串。您可能需要添加几个步骤来从数组中删除空字节以获得您正在寻找的结果。希望有帮助!

于 2014-01-06T20:40:32.093 回答