"serialUSB.print()" didn't work in "setup()" on The Seeeduino XIAO

I tested the example recommended here: - Seeeduino-XIAO/USB-to-Serial-Port.md , it didn’t work when I tried to use “serialUSB.print()” in Arduino “setup()”, and, when I tried the tricks recommended here: SerialUSB.print() in Output, it works after I added " ```
while (!SerialUSB.available());

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
}


I tested this on Arduino DUE, my questions are:

1. Is this the regular method for this situation?
2. Is that true, the boards based on ARM now integrated the 2 ports on DUE into one USB plug?
3. 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?

Different MCU board have different drivers to drive them. The “seriralUSB.print()” function may not be added in the board drivers by the developers I think.

1 Like