有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过USB将Arduino中显示的java错误数据传输到Android

我正试图通过USB主机模式将Arduino UNO R3上的数据以数字的形式发送到Nexus 7平板电脑。单击按钮后,将在MainActivity java文件中调用CheckStatus()函数:

public void CheckStatus(View v)
{
    handler = new Handler();    //allocate memory for handler
    MyThread2 checkthread = new MyThread2();
    checkthread.start();

    synchronized (this)
    {
        if (usbDeviceConnection != null)
        {
            byte[] message = new byte[1];
            message[0] = (byte) (101);  //Sening any no. to Arduino, so that it responds.

            usbDeviceConnection.bulkTransfer(endpointOut, message, message.length, 0);

        }
    }

}

class MyThread2 extends Thread
{
    @Override
    public void run()
    {
        byte x;
        String string=null;
        final ByteBuffer buffer = ByteBuffer.allocate(1);

        UsbRequest request = new UsbRequest();
        request.initialize(usbDeviceConnection, endpointIn);
        while (true)
        {
            request.queue(buffer, 1);
            if (usbDeviceConnection.requestWait() == request)
            {
                try
                {
                    x = buffer.get(0);
                    string = Byte.toString(x);
                    handler.post(new newthread(string));

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
            else break;
        }

    }

}

class newthread implements Runnable
{
    String str;
    public newthread(String STR)
    {
        str=STR;
    }
    @Override
    public void run()
    {
        tv2.setText(str);           
    }
}

这是Arduino的部分:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available())
  Serial.write(7);  //Sending no. 7...
}

这里的问题是,无论我试图从Arduino端发送什么数字(无论是7还是其他),都会显示数字0。我认为这可能是数据类型的问题。我能得到一个提示,我可能会错在哪里吗


共 (0) 个答案