2

I have an Arduino M0 connected to a peripheral device.

A PC is connected to the arduino via USB. The mode of operation is that the Arduino receives commands via USB that instruct it to do something with the peripheral device. The data from the peripheral device (a struct) is converted to a JSON string and sent to the PC over USB.

I am using SerialUSB.print();, SerialUSB.println(); to write the JSON data, and then after each part of the JSON message is printed, I call SerialUSB.flush();.

I am noticing that sometimes the next JSON message will "interrupt" the last (the last message will not finish printing before the next starts).

I have already increased my SerialUSB baudrate to the max supported. What are some strategies I can use to ensure that the JSON message is fully printed printing the next?

Here is the relevant code:

typedef struct __attribute__((packed))
{
  uint16_t inner_member1, inner_member2, inner_member3;
  uint16_t inner_member4, inner_member5, inner_member6, inner_member7, inner_member8, inner_member9;
} inner_struct_t;


typedef struct __attribute__((packed))
{
  inner_struct_t i1;
  inner_struct_t i2;
  unsigned long outer_member1;
  uint16_t outer_member2;
  uint16_t outer_member3;
  uint16_t outer_member4;
  uint16_t outer_member5;
  uint16_t outer_member6;
  uint16_t outer_member7;
  uint16_t outer_member8;
  uint16_t outer_member9;
  uint8_t outer_member10;
} outer_struct_t;

void print_inner(inner_struct_t *i)
{
    SerialUSB.print(F("{"));
    SerialUSB.print(F("\"inner_member1\": "));
    SerialUSB.print(i->inner_member1);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member2\": "));
    SerialUSB.print(i->inner_member2);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member3\": "));
    SerialUSB.print(i->inner_member3);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member4\": "));
    SerialUSB.print(i->inner_member4);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member5\": "));
    SerialUSB.print(i->inner_member5);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member6\": "));
    SerialUSB.print(i->inner_member6);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member7\": "));
    SerialUSB.print(i->inner_member7);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member8\": "));
    SerialUSB.print(i->inner_member8);
    SerialUSB.print(F(", "));
    SerialUSB.print(F("\"inner_member9\": "));
    SerialUSB.print(i->inner_member9);
    SerialUSB.print(F("}"));
    SerialUSB.flush();
}

void print_outer(outer_struct_t *o)
{
    SerialUSB.print(F("{\"outer_member10\": "));
    SerialUSB.print(o->outer_member10);
    SerialUSB.print(F(", \"outer_member2\": "));
    SerialUSB.print(o->outer_member2);
    SerialUSB.print(F(", \"outer_member1\": "));
    SerialUSB.print(o->outer_member1);
    SerialUSB.print(F(", \"i1\": "));
    print_inner(&(o->i1));
    SerialUSB.print(F(", \"i2\": "));
    print_inner(&(o->i2));
    SerialUSB.print(F(", \"outer_member3\": "));
    SerialUSB.print(o->outer_member3);
    SerialUSB.print(F(", \"outer_member4\": "));
    SerialUSB.print(o->outer_member4);
    SerialUSB.print(F(", \"outer_member5\": "));
    SerialUSB.print(o->outer_member5);
    SerialUSB.print(F(", \"outer_member6\": "));
    SerialUSB.print(o->outer_member6);
    SerialUSB.print(F(", \"outer_member7\": "));
    SerialUSB.print(o->outer_member7);
    SerialUSB.print(F(", \"outer_member8\": "));
    SerialUSB.print(o->outer_member8);
    SerialUSB.print(F(", \"outer_member9\": "));
    SerialUSB.print(o->outer_member9);
    SerialUSB.print("}");
    SerialUSB.flush();
}
  • Could you show the code you use (edited into question?) – ocrdu Oct 28 at 18:24
  • And if you don't flush() does that improve things? – Majenko Oct 28 at 18:37
  • I see the same symptoms when I remove SerialUSB.flush() – Tyler P Nov 9 at 15:10

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.