在Arduino上接收不正确的串行数据

时间:2015-03-18 02:50:41

标签: android serialization arduino-uno

我需要从平板电脑中获取我的x / y坐标,对它们进行一些数学计算,然后在每个字母前面加上一个字母(下面的代码中为Z),然后通过串行链接发送它们。一切都有效,除了通过串行链接发送和接收它们。 我的例子中的toast告诉我正在计算WRP,例如Z10.0。但是在我的接收端串行监视器上我得到50(换行)46。50是稳定的,46变化,但不是一致的,不等于A.

如果我尝试“sendData(”S“);”我在串口监视器上正确接收到S.

为了简单起见,我把问题归结为以下几点;

在传输方面:

btnOff.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                A=A+1;
                String WRP = (Double.toString(A));
                WRP = "Z"+ WRP;
                sendData(WRP);
                Toast msg = Toast.makeText(getBaseContext(),
                        WRP, Toast.LENGTH_SHORT);
                msg.show();
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendData("S");
            }
        });

 private void sendData(String message) {
    byte[] msgBuffer = message.getBytes();

    Log.d(TAG, "...Sending data: " + message + "...");

    try {
        outStream.write(msgBuffer);
    } catch (IOException e) {
        String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
        if (address.equals("00:00:00:00:00:00"))
            msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
        msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

        errorExit("Fatal Error", msg);
    }
}

在接收方:

    void loop() {
  if (mySerial.available())
    dataFromBT = mySerial.read();
    Serial.println(mySerial.read());
} 

提前致谢!

0 个答案:

没有答案