【问题标题】:Arduino receive data from software serialArduino 从软件串口接收数据
【发布时间】:2018-11-08 19:10:29
【问题描述】:

对不起我的英语不好。我正在尝试在我的 Arduino 上接收来自 Sim800 的 json 数据。要读取串行端口上的数据,我使用了以下代码:

while(serialSIM800.available()==0); //Wait until the data is received
    String content = "";
  while(serialSIM800.available()>0){ // When data is received
    content = content + char(char (serialSIM800.read()));
    }
Serial.print(content);

但收到不完整的数据。如下:

{"id":"1212","temp":"24","hum","4

为了获得更好的结果,我使用了以下代码:

 byte x;
char data[128];
void sim800Reply() {
  x=0;
  do{   
    while(serialSIM800.available()==0);
    data[x]=serialSIM800.read();
    Serial.print(data[x]);
    x++;
  } while(!(data[x-1]=='K'&&data[x-2]=='O'));
}

数据已完全接收。如下:

{"id":"1212","temp":"24","hum","45","date":"11.2018","status":"200"}

OK

但是我认为这段代码不好,有问题。例如如果没有连接sim800的时候serialSIM800不可用,下面的代码会导致crash while(serialSIM800.available()==0); 因为这个永远是真的 OR 如果有错误并且OK 没有收到,下面的代码会导致崩溃 while(!(data[x-1]=='K'&&data[x-2]=='O')); 因为这个永远是真的。最大数据长度是120字节,我该怎么办才能收到来自 Arduino 串行的 Json 数据?谢谢大家。

【问题讨论】:

    标签: json arduino avr sim800 software-serial


    【解决方案1】:

    开始尝试:

    if (serialSIM800.available()) {
      String content = serialSIM800.readString();
      Serial.print(content);
    }
    

    在 setup() 中添加serialSIM800.setTimeut(50);

    【讨论】:

    • tnx 我只是用serialSIM800.readString()而不是serialSIM800.read()
    • 我怀疑它真的有帮助。 while(serialSIM800.available()==0); 阻塞 loop() 怎么办?
    • 我用这个代码:while(serialSIM800.available()==0){ delay(2000); return;}如果串口两秒不可用,就会返回。
    • if (serialSIM800.available()) { 确保草图循环畅通,直到数据不可用。无需阻塞循环
    【解决方案2】:

    最后我把代码改成如下:

     String dump(bool printData) {
          byte while_cunt = 0;
          while (serialSIM800.available() == 0){
            while_cunt++;
            delay(15); // It can change for a proper delay
            if(while_cunt >=250){  // If the data from serial was not received past 3.75 seconds
              Serial.println("return");
              return "";   // Exit from dump function
            }
          }
          String content = "";
          while (serialSIM800.available() > 0) {
            content += serialSIM800.readString();
          }
    
          if (printData) {
            Serial.println(content);
          }
          return content;
        }
    

    它可以返回和打印串行数据而且它工作速度很快因为每15毫秒检查数据是否已收到,如果在一定时间内(本例为3.75秒)没有收到数据,它将被淘汰进程,不会崩溃。 此函数的示例:

         serialSIM800.println("AT");
         dump(true); 
    // print Received response from sim800
    
        serialSIM800.println("AT");
        String str =  dump(true); 
        // print Received response from sim800 and Saved in variable named 'str'
    
        serialSIM800.println("AT");
         String str =  dump(false); 
    //Save response  in variable named 'str' without print 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2016-07-26
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多