[0.6.0.rc2] Keyboard.begin() - closes serial USB port [solved]

@bdub, we have a problem with 0.6.0.rc2.

void setup() {
Serial.begin(9600);
delay(1000L);
Serial.println(“Exiting setup”);
}

void loop() {
delay(500L);
Serial.println(".");
}

Outputs:

Exiting setup
.
.

Whereas

void setup() {
Serial.begin(9600);
Keyboard.begin();
delay(1000L);
Serial.println(“Exiting setup”);
}

void loop() {
delay(500L);
Serial.println(".");
}

Outputs nothing to the serial port…

Changing the order in setup() does not help:

Keyboard.begin();
Serial.begin(9600);

The PC it is connected to runs Windows 7 and am using a 8 port USB hub.

Next tested on a Macbook Pro. It then became obvious what was happening:

Calling Keyboard.begin() closes the usb Serial (ie screen /dev/cu.usbmodem621 returns)

BUT, I can re-rerun screen /dev/cu.usbmodem621, and it opens okay.

Back to Windows 7, if you open PuTTy upon startup, nothing received, but after closing PuTTy you can’t connect again. But if you start the Photon, wait a little bit and then open PuTTy, it will see output.

Obviously this a problem. Will raise a Github issue.

1 Like

PS - I have the old Photon driver installed on the Windows machine.

Hmm… it’s very important to follow the driver notes to the letter for Windows. But I see you say this is happening on Mac as well. We’ll look into this!

Driver Notes

Follow all steps here, including clearing the cache with the required tools. Only after then connect your device https://github.com/avtolstoy/particle-usb-testing#windows

We will be making an installer for this process, so don’t think it will always be so difficult. For now though, I do appreciate your help testing this, thanks :smile:

This is expected. By the time setup() is called, the device has already been enumerated to the host. By default, only Serial is enabled in order to keep compatibility with “old” Windows drivers.

When Keyboard.begin() is called, the device needs to change its USB descriptors that are presented during enumeration, open additional endpoints etc etc. There is no way to do all that without performing a reattach (i.e. disconnect/reconnect).

If that behavior is undesirable, you can always call Keyboard.begin() in STARTUP(), which will make the device attach for the first time after boot with both Serial and Keyboard:

STARTUP(Keyboard.begin());

void setup() { 
Serial.begin(9600);
delay(1000L);
Serial.println("Exiting setup");
}

void loop() {
delay(500L);
Serial.println(".");
}
2 Likes

@avtolstoy and @bdub you are scholars!

Implemented your suggestion @avtolstoy :

             STARTUP(Keyboard.begin());

Now works as expected on both Windows 7 and Macbook Pro, ie serial USB and keyboard can operate at the same time.

@bdub AND it was with the old Photon Windows driver…

Case closed with thanks!

1 Like

Just to clarify: STARTUP(Keyboard.begin()); doesn’t cause the Serial and Keyboard to operate at the same time. They can still operate at the same time even if you call Keyboard.begin() in setup(), the only difference being that in this case the device will first appear as just having Serial, then detach, reattach, and only then it will have both Serial and USB HID.

1 Like