SRF02读写问题(使用USB-I2C-USB转I2C和Java语言)

我正在尝试向我的Sensor命令寄存器写入一个1字节的注释,并且还尝试从结果寄存器取回2字节的结果,但是我总是得到FF FF。

如果我还尝试读取0x00寄存器,则意味着它返回软件版本,我也得到了我的字节FF。请问有什么解决办法吗?我的代码用于编写命令,红色结果如下。当我从寄存器中读取时,传感器红色LED闪烁,这意味着该代码可以与设备进行通信,但始终从任何寄存器或输入数据流中读取FF值。

private byte Write(byte address,byte register,byte data) throws IOException
{
    OutputStream os;
    InputStream is;

    os = port.getOutputStream();
    is = port.getInputStream();

    byte[] cmd = {0x55,0x70,0x00,0x01,0x51};

    os.write(cmd);
    os.flush();

    try
    {
        Thread.sleep(100);
    }
    catch(InterruptedException ex)
    {
        Thread.currentThread().interrupt();
    }

    byte[] buffer = new byte[is.available()];
    is.read(buffer);
    System.out.printf("length is %02d ",+buffer.length);
    int i=0;
    for (byte b: buffer)
    {
        if (i % 10 == 0)
        {
            System.out.println();
        }

        System.out.printf("%02x ",b);

        i++;
    }

    is.close();
    os.close();

    int result =0;      // READ RESULT (in case of write command,too!)

    return (byte)(result & 0x00FF);
}

private byte Read(byte address,byte register) throws IOException
{   
    OutputStream os;
    InputStream is;
    os = port.getOutputStream();
    is = port.getInputStream();

    byte[] cmd = {(byte)0x55,(byte)0x71,(byte)0x02,(byte)0x02};

    os.write(cmd);
    os.flush();

    try
    {
        Thread.sleep(100);
    }
    catch(InterruptedException ex)
    {
        Thread.currentThread().interrupt();
    }

    int result=0;

    byte[] buffer = new byte[is.available()];
    is.read(buffer);
    System.out.printf("length is %02d ",+buffer.length);
    int i = 0;

    for (byte b: buffer)
    {
        if (i % 10 == 0)
        {
            System.out.println();
        }

        System.out.printf("%02x ",b);

        i++;
    }

    os.close();
    is.close();

    return (byte)(result & 0x00FF);
}
hbxflihua 回答:SRF02读写问题(使用USB-I2C-USB转I2C和Java语言)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3089522.html

大家都在问