小编pra*_*rat的帖子

在Python中通过串口发送JSON到Arduino

我一直在尝试设置此示例片段以通过串行方式与 Python 进行通信。我打算发送一个简单的 JSON 数据包,并在另一端使用 Arduino 对其进行反序列化。

但是从这里获取的 JSON 库会抛出无效输入的错误。

#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200); 
  while(!Serial) {
  }
}

void loop() {
  int     size_ = 0;
  char    payload[256];
  String  string = "";
  if (size_ = Serial.available()) {
  for(int i =0; i < size_; i++) {
     payload[i] = Serial.read();
     string += payload[i];   
  }

  const size_t capacity = 1024;
  DynamicJsonDocument doc(capacity);
  DeserializationError error = deserializeJson(doc, payload);
  if (error) {
    Serial.println(error.c_str());
    return;
  }
    if(doc["operation"] == "ACK") {
      Serial.println("ACK has been received"); …
Run Code Online (Sandbox Code Playgroud)

c++ python json arduino pyserial

3
推荐指数
1
解决办法
8674
查看次数

标签 统计

arduino ×1

c++ ×1

json ×1

pyserial ×1

python ×1