A serialUSB.Print() problem with "Seeeduino XIAO"

I used a " Seeeduino XIAO" board as it was said here:

  1. Meet the Seeeduino XIAO
  2. Getting Started with Seeed Studio XIAO SAMD21

I found "serialUSB.print()" didn't work in "setup()", and as it was told here: SerialUSB.print() in Output

I added "while (!SerialUSB.available());", then when I plug from programming port to native USB port and "Enter" in serial monitor, I found the "serialUSB.print()" works in "setup()".

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  //Serial.print("Hello, World!");
  
  SerialUSB.begin(115200);
  while (!SerialUSB.available());
  SerialUSB.print("USB - Hello, World!");
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  //SerialUSB.println("USB - Hello, World! Again!");
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

my questions are:

  1. Is that true, the boards based on ARM now integrated the 2 ports on DUE into one USB plug?
  2. The seeed recommended a test example Seeeduino-XIAO/USB-to-Serial-Port.md :
/*
   This example is used for USB to ttl.
   update file Arduino15\packages\Seeeduino\hardware\samd\1.6.0\cores\arduino\USB\USBAPI.h

    class Serial_ : public Stream
    {
    public:
      ......
      ......
      //add
      uint32_t getBaud(void);
      ......
    }

   update file Arduino15\packages\Seeeduino\hardware\samd\1.6.0\cores\arduino\USB\CDC.cpp
   //add
    uint32_t Serial_::getBaud(void)
    {
     return _usbLineInfo.dwDTERate;
    }
*/

uint32_t baud;
uint32_t old_baud;
void setup() {

  // put your setup code here, to run once:
  SerialUSB.begin(115200);
  baud = SerialUSB.getBaud();
  old_baud = baud;
  Serial1.begin(baud);
  while (!Serial);
  while (!SerialUSB);
}

void loop() {
  // put your main code here, to run repeatedly:
  baud = SerialUSB.getBaud();
  if (baud != old_baud) {
    Serial1.begin(baud);
    while (!Serial);
    old_baud = baud;
    //     SerialUSB.println(baud);
  }
  if (SerialUSB.available() > 0)
  {
    char c = SerialUSB.read();
    Serial1.write(c);
  }
  if (Serial1.available() > 0) {
    char c = Serial1.read();
    SerialUSB.write(c);
  }
}

who can explain it, why so much "while***" here?

Warning: "SerialUSB" and "Serial" are the same USB device. For the UART port (pins 6 and 7), use "Serial1"

Should be:

  SerialUSB.begin(115200);
  while (!SerialUSB);

Should be:

  // put your setup code here, to run once:
  SerialUSB.begin(115200);
  while (!SerialUSB);
  baud = SerialUSB.getBaud();
  old_baud = baud;
  Serial1.begin(baud);
  while (!Serial1);

What does the "!" in "!Serial" mean? - disable, disconnected, not ready, or not completed.

It means "while Serial not ready"; you'll have to open a communication channel from the PC to the board (e.g. open serial monitor).

Same thing it mean in any other boolen expression: logical negations or 'not'. The Serial object returns 'false' when the USB connection has not been established and 'true' when it is ready. The whole expression can be read as:

While it is not true that the Serial connection is ready, repeat nothing,

1 Like

In UNO "Serial" always means "RX0/TX0", I found there was also "RX0/TX0" on DUE, and it seemed that the Programming Port used it as it was on UNO, so my operation was: downloaded by Programming Port, then plug out the USB cable from Programming Port and plug into USB Port, then open the serial monitor, now that the serial monitor window was feed by SerialUSB, why need I verified the "Serial"(i.e. RX0/TX0) was OK or not?

You can run the following 2 examples and observe the difference.

  1. Close serial monitor
  2. Upload the sketch.
  3. Wait a few seconds and open serial monitor\
uint32_t counter;

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

void loop()
{
  Serial.println(counter++);
  delay(100);
}
uint32_t counter;

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

void loop()
{
  Serial.println(counter++);
  delay(100);
}

The second sketch will show you the counter from 0, the first one will not.

while(!Serial)` is required if you do not want to miss the first prints. Disadvantage: you will always have to use a terminal program (e.g. serial monitor) before your sketch will get past it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.