从串口Arduino到Android的不完整和奇怪的单词

时间:2013-04-08 12:15:04

标签: android arduino

我使用教程 Data transfer between Android and Arduino via Bluetooth 从Arduino接收代码。

我使用id txtArduino创建了一个textview,并使用.append,但为什么Arduino发送时会出现不完整的文本?

例如:

  

LED亮起,并且:w-。 IIN:13

类似这样的奇怪文字。

如何解决此问题?

PS:如果需要,我可以提供更多代码。

Android标题

h = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case RECIEVE_MESSAGE:                                     // If receive massage
                byte[] readBuf = (byte[]) msg.obj;
                String strIncom = new String(readBuf, 0, msg.arg1);   // Create string from bytes array
                sb.append(strIncom);                                  // Append string
                int endOfLineIndex = sb.indexOf("\r\n");              // Determine the end-of-line
                if (endOfLineIndex > 0) {                             // If end-of-line,
                    String sbprint = sb.substring(0, endOfLineIndex); // Extract string
                    sb.delete(0, sb.length());                        // And clear
                    txtArduino.append(sbprint + "\n");                // Update TextView

                    //toggleDOOR.setEnabled(true);
                }
                Log.d(TAG, "...Mesaj:"+ sb.toString() +  " Byte:" + msg.arg1 + "...");
                break;
            }
    };
};

Arduino代码:

int cnt = 0;
int pinLed = 13;

String myKey = "0000001";

//Pin Geam Masina 2 si 3
int pinGeamInchis = 2;
int pinGeamDeschis = 3;

char incomingByte;
void setup(){
    Serial.begin(9600);

    //INITIALIZARE
    pinMode(pinLed,OUTPUT);
    pinMode(pinGeamInchis,OUTPUT);
    pinMode(pinGeamDeschis,OUTPUT);

    digitalWrite(pinLed,LOW);

    Serial.print("Modul ON( KEY:");
    Serial.print(myKey);
    Serial.print(" )\r\n");
}

void loop(){
    if(Serial.available()> 0){
        incomingByte = Serial.read();
        if(incomingByte == '0'){
            //Opreste LED
            digitalWrite(pinLed,LOW);
            Serial.print("Pin 13: LED OFF\r\n");

            //Inchide Geam
            digitalWrite(pinGeamInchis,HIGH);
            Serial.print("Command:W-C\r\n");
            delay(500);
            digitalWrite(pinGeamInchis,LOW);
            Serial.print("Pin 2:Window Down\r\n");
        }
        if(incomingByte == '1'){
            //Aprinde LED
            digitalWrite(pinLed,HIGH);
            Serial.print("LED ON\r\n");

            //Deschide Geam
            digitalWrite(pinGeamDeschis,HIGH);
            Serial.print("Command:W-O\r\n");
            delay(500);
            digitalWrite(pinGeamDeschis,LOW);
            Serial.print("Pin 3:Window Up\r\n");
        }
    }
}

我知道,如果我只发送一个“命令”,它就可以了。我该怎么做才能在串口和Android上接收更多命令?

PS:我使用Arduino Nano

2 个答案:

答案 0 :(得分:0)

分析

查看您的Android代码:

String sbprint = sb.substring(0, endOfLineIndex); // Extract string
sb.delete(0, sb.length());                        // And clear
txtArduino.append(sbprint + "\n");                // Update TextView

您将收到的消息提取到(第一个)换行符,然后删除整个消息。我想,人们无法确定收到的任何“消息”中会有多少数据。

例如,当Arduino执行时:

Serial.print("Pin 13: LED OFF\r\n");
// [...]
Serial.print("Command:W-C\r\n");

这将被发送到BT模块,就像:

Serial.print("Pin 13: LED OFF\r\nCommand:W-C\r\n");

然后“某人”(BT模块?Android操作系统?......?)可能决定将其放入两个“消息”(或“数据包”)中,例如

Message1: "Pin 13: LED OFF\r\nComm"
Message2: "and:W-C\r\n"

在这种情况下,您只能看到

"Pin 13: LED OFF"

"and:W-C"

显示,因为您删除了第一条消息的"Comm"部分。

建议

尝试将sb.delete(0, sb.length());替换为sb.delete(0, endOfLineIndex + "\r\n".length());,这样就不会删除整个字符串,只会删除实际显示的部分,并在下一条消息中完成其余部分。< / p>

然后,您还应该添加一个循环,以确保当邮件中有多个换行符时,您的代码不会失败。为此,只需替换:

int endOfLineIndex = sb.indexOf("\r\n"); // Determine the end-of-line
if (endOfLineIndex > 0) {                // If end-of-line,

int endOfLineIndex; 
while ( (endOfLineIndex = sb.indexOf("\r\n")) > 0) { // While there's at least one complete line in buffer,

答案 1 :(得分:0)

而不是在Arduino端发送print(“/ r / n”)而只是放一个空的System.println();

像这样:

void taskTransmitData(void)
{
  // start the xml
  Serial.print("HE");
  Serial.print(pMyMemory->currentHeading);
  Serial.print("/HE");

---- 

  Serial.print("CS");
  Serial.print(currentState);
  Serial.print("/CS");

  Serial.println();

}

在android方面,这应该有效:

bytes = mmInStream.read(buffer);
byte[] readBuf = (byte[]) buffer;
String strIncom = new String(readBuf, 0, bytes); // create string from bytes array
sb.append(strIncom);      // append string
int endOfLineIndex = sb.indexOf("\r\n");  // determine the end-of-line
if (endOfLineIndex > 0) {  
    // add the current string to eol to a local string
    String sbprint = sb.substring(0, endOfLineIndex);

    // get the start and end indexes of the heading
    int startHeading = sb.indexOf("HE");
    int endHeading = sb.indexOf("/HE");

    // set the heading
    Henry.this.setCurrentHeading(sb.substring((startHeading + 2), endHeading));

    ......

    // get the start and end indexes of the current state
    int startCS = sb.indexOf("CS");
    int endCS = sb.indexOf("/CS");

    // set the current state
    Henry.this.currentState = sb.substring((startCS + 2), endCS);

    // output what we have
    //System.out.println("recv: " + sbprint);
    sb.delete(0, sb.length());   
}