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);
}
}