Arduino 没有正确发送整数 [英] Arduino not sending integers properly

查看:28
本文介绍了Arduino 没有正确发送整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 0 到 10 的整数从我的 Arduino Uno 发送到我的 Android 设备.但是,Arduino 并没有单独发送整数,而是将其作为一个集群发送(有时一次发送 2 个).我希望能够每 5 毫秒发送一个整数,并且不会延迟更长的时间.有什么想法吗?

Arduino 代码:

#include const int RX_PIN = 8;const int TX_PIN = 9;软件串口蓝牙(RX_PIN, TX_PIN);字符命令字符;无效设置(){蓝牙.开始(9600);Serial.begin(9600);}无效循环(){如果(蓝牙.可用()){commandChar = bluetooth.read();开关(命令字符){案件 '*':for(int i = 0; i <11; i++){蓝牙.print(i);延迟(5);}休息;}}}

安卓代码:

public void run() {初始化连接();字节[]缓冲区=新字节[256];//流的缓冲区存储整数字节;//从 read() 返回的字节而(真){尝试 {if(mmSocket!=null) {字节 = mmInStream.read(buffer);String readMessage = new String(buffer, 0, bytes);Log.e("收到消息", readMessage);}}} catch (IOException e) {Log.e("错误", "从 btInputStream 读取");休息;}}}

Android 监视器/控制台输出:

08-18 19:46:32.319 6720-6749/?E/收到的消息:008-18 19:46:32.324 6720-6749/?E/收到的消息:108-18 19:46:32.324 6720-6749/?E/收到的消息:2308-18 19:46:32.324 6720-6749/?E/收到的消息:408-18 19:46:32.379 6720-6749/?E/收到的消息:5608-18 19:46:32.379 6720-6749/?E/收到的消息:7808-18 19:46:32.379 6720-6749/?E/收到的消息:9108-18 19:46:32.384 6720-6749/?E/收到的消息:0

解决方案

似乎串行通信是作为流(不是数据报)工作的,并且没有保持任何数据边界.

因此,您似乎应该在发送数据中添加数据分隔符(例如:换行符)并在接收器中对其进行处理(例如:使用BufferedReader)以保持数据边界.

I am trying to send integers 0 through 10 from my Arduino Uno to my Android device. However, the Arduino is not sending the integers separately, but rather it is sending it as a cluster (sometimes 2 at a time). I want to be able to send an integer every 5 milliseconds and not delay any longer than that. Any ideas?

Arduino code:

#include <SoftwareSerial.h>

const int RX_PIN = 8;
const int TX_PIN = 9;
SoftwareSerial bluetooth(RX_PIN, TX_PIN); 
char commandChar;

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

void loop () {
    if(bluetooth.available()){
        commandChar = bluetooth.read();
        switch(commandChar){
            case '*':
            for(int i = 0; i < 11; i++){
                bluetooth.print(i);
                delay(5);
            }
        break;
       }
    }   
 }

Android code:

public void run() {
    initializeConnection();
    byte[] buffer = new byte[256]; // buffer store for the stream
    int bytes; // bytes returned from read()
    while (true) {
        try {
            if(mmSocket!=null) {
                bytes = mmInStream.read(buffer);
                String readMessage = new String(buffer, 0, bytes);
                Log.e("Received Message ", readMessage);
                }
            }
        } catch (IOException e) {
            Log.e("ERROR ", "reading from btInputStream");
            break;
        }
    }
}

Android Monitor/Console output:

08-18 19:46:32.319 6720-6749/? E/Received Message: 0
08-18 19:46:32.324 6720-6749/? E/Received Message: 1
08-18 19:46:32.324 6720-6749/? E/Received Message: 23
08-18 19:46:32.324 6720-6749/? E/Received Message: 4
08-18 19:46:32.379 6720-6749/? E/Received Message: 56
08-18 19:46:32.379 6720-6749/? E/Received Message: 78
08-18 19:46:32.379 6720-6749/? E/Received Message: 91
08-18 19:46:32.384 6720-6749/? E/Received Message: 0

解决方案

It seems that the serial communication is working as stream (not datagram) and isn't keeping any data boundary.

Therefore, it seems you should add data separator (for example: newline) to your sending data and process it in the receiver (for example: use BufferedReader) to keep the data boundary.

这篇关于Arduino 没有正确发送整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆