【问题标题】:receive garbage data while sending string from arduino to android using HC-05使用 HC-05 将字符串从 arduino 发送到 android 时接收垃圾数据
【发布时间】:2019-02-26 16:18:37
【问题描述】:

一切都在标题中。当我将字符串数据从 Arduino 发送到 android 时,我会收到这个 ������������������。我尽一切努力获得真正的价值,但没有。请帮忙。编辑 Arduino 部分在这里:

#include <SoftwareSerial.h>
#define rxPin 19
#define txPin 18
SoftwareSerial BTserial(rxPin, txPin);

void setup() {
  // put your setup code here, to run once:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  BTserial.begin(38400);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  BTserial.println("ROGER AIME LES POMMES. HEIN LE SALAUD");
  delay(5000);
}

Android part :

public void run() {
        byte[] buffer = new byte[1024];
        int bytes;
        while (true) {
            try {
                bytes = mmInStream.read(buffer);//read bytes from input buffer
                bluetoothIn.obtainMessage(handlerState, bytes, -1, buffer).sendToTarget();
            }

            catch (IOException e) {
                break;
            }
        }
    }

在 onCreate 中

bluetoothIn = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == handlerState) {
                byte[] readBuff = (byte[]) msg.obj;
                String readMessage = new String(readBuff,0,msg.arg1);
                blue_tv2.setText("Data Received = " + readMessage);
                Log.d("", "handleMessage: "+readMessage);
            }
        }
    };

我运行时的控制台。

D/: handleMessage: z
D/: handleMessage: z_�~�
D/: handleMessage: z
D/: handleMessage: z_�~�
D/: handleMessage: 7
D/: handleMessage: z_�~�
D/: handleMessage: 7
D/: handleMessage: z_�~�

【问题讨论】:

  • 很确定这是传输层问题;你确定 mmInStream 是按原样流式传输数据吗?
  • 我怎么知道 mmInStream 不是流数据?我将发布它的代码部分。
  • 如果我没有在 OnResume 中设置 socket.connect 和 thread.start 但在 onCreate 中它可能是错误的来源?我才意识到这一点。
  • 我更新了解决传输层问题的答案

标签: android bluetooth arduino hc-05


【解决方案1】:

mminStream.read() 读取并返回一个字节; 您永远不会真正读取字节到缓冲区并且正在解码“全 0 字节”缓冲区;

了解String 构造函数和InputStream

我只会改正你错的地方,其他什么都不要碰!;

public void run() {
    byte[] buffer = new byte[1024];
    int bytes;//rename this to something else; the name doesn't explain what this variable actually holds and might cause confusion
    String temp_msg="";
    while (true) {
        try {
            bytes = mmInStream.read(buffer);//bytes now holds the number of read bytes which are writen to buffer;
            String readMessage = new String(buffer, 0, bytes);//now this decodes buffer bytes residing in indexes from 0 to bytes value
            //now readmessage holds the message
            //rest of the code

关于传输层问题(废话数据): 这可能是因为波特率设置错误; read thisthis(据我了解,115200 可能有效!)

【讨论】:

  • 谢谢,但是当我打印 readMessage 并且代码从不输入我的 if 时,我总是收到这个 O。怎么了?
  • 所以在您将代码更改为我建议您仅在 readmessage 中收到 O� 之后?没有别的吗?
  • 我收到了类似 z_�~�的东西。总是有这个 - 我没有收到我真正发送的内容。
  • 那么您将数据流式传输到 mmInStream 的传输层(代码和协议)不会按原样传输数据。
  • 好的。我要去看看。我编辑了代码。你可以看到我收到的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2022-11-09
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多