0

IDE:1.8.8 Board:Arduino UNO When I use the code below, I get result of print like this.

{ "result": [ { "ID": "01", "Value": [ "2", "1", "13" ] } ] }

But when I use SoftwareSerial and in the setup to being that the serializeJson result like this:

{}

I don't know what happens in the softserial with using json library, can someone tell me why?

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
int dir[3];
char  json[] ="{\"ID\":\"01\",\"Value\":[\"%d\",\"%d\",\"%d\"]}";
//SoftwareSerial Left(8,9);//左
/SoftwareSerial Right(6,7); //右 
//SoftwareSerial Up(10,11);//上
//SoftwareSerial Down(14,15);//下
//SoftwareSerial Global(8,9);  //建立軟體串列埠腳位 (RX, TX)左
void setup() {

  Serial.begin(9600);        //設定硬體串列埠速率
 // Up.begin(9600);
 // Right.begin(9600);
  //Global.begin(9600);   //設定軟體串列埠速率
  }

void loop() {

 dir[0]=2;
 dir[1]=1;
 dir[2]=13;
  if(Serial.read()=='j')
{
 StaticJsonDocument<256> docc;
  char b[35];
  sprintf(b, json, dir[0], dir[1],dir[2]);
  deserializeJson(docc, b);
  JsonObject object = docc.as<JsonObject>();

  DynamicJsonDocument doc(1024);
 JsonArray result = doc.createNestedArray("result");
  result.add(object);
  result.add(object);
  serializeJsonPretty(doc,Serial);
   }
  }

These are error code

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
int dir[3];
char  json[] ="{\"ID\":\"01\",\"Value\":[\"%d\",\"%d\",\"%d\"]}";
//SoftwareSerial Left(8,9);//左
SoftwareSerial Right(6,7); //右 
SoftwareSerial Up(10,11);//上
//SoftwareSerial Down(14,15);//下
SoftwareSerial Global(8,9);  //建立軟體串列埠腳位 (RX, TX)左
void setup() {

  Serial.begin(9600);        //設定硬體串列埠速率
  Up.begin(9600);
  Right.begin(9600);
  Global.begin(9600);   //設定軟體串列埠速率
  }
void loop() {
     Global.listen();
      while (!Serial.available()) {}  //等到PC傳送字元才到下一步
     Global.write(2);  //傳送給左邊的板子子id
      while (!Global.available()) {}  //等到左方板傳送字元才到下一步
        dir[0]=Global.read();
        Serial.println( dir[0]);  //左方板向PC傳送字串


       Up.listen();
      while (Serial.read()!='Y') {}  //等到PC傳送字元才到下一步
      Up.write(3);  //傳送給左邊的板子子id
      while (!Up.available()) {}  //等到左方板傳送字元才到下一步
      dir[1]=Up.read();
        Serial.println(dir[1]);  //左方板向PC傳送字串
       // }

     Right.listen();
     while (Serial.read()!='Y') {}  //等到PC傳送字元才到下一步
      Right.write(4);  //傳送給左邊的板子子id
      while (!Right.available()) {}  //等到左方板傳送字元才到下一步
       dir[2]=Right.read();
        Serial.println(dir[2]);  //左方板向PC傳送字串
 if(Serial.read()=='j')
{
 StaticJsonDocument<256> docc;
  char b[35];
  sprintf(b, json, dir[0], dir[1],dir[2]);
  deserializeJson(docc, b);
  JsonObject object = docc.as<JsonObject>();

  DynamicJsonDocument doc(1024);
 JsonArray result = doc.createNestedArray("result");
  result.add(object);
  result.add(object);
  serializeJsonPretty(doc,Serial);
   }
}
  • the code you posted doesn't even use SoftwareSerial – Jaromanda X Mar 4 at 0:14
  • Sorry, i edited. – Haseo Mar 4 at 4:26
  • Your code still isn't even trying to output json to any software serial – Jaromanda X Mar 4 at 4:58
  • And what does "These are error code" mean. There are no errors in the question – Jaromanda X Mar 4 at 4:58
  • Sorry, i didn't make myself clear, the error code is just mean test in software serial begin case return this '{}' result, then when comment out //Right.begin(9600); that can give me fine json print, i don't know what happen between these two library. – Haseo Mar 4 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.