I'm trying to use SerialUSB (DigiCDC.h) on a Digispark with Linux. It works (and works better when I tell ModemManager to go away and leave the device alone!), but I find that if I connect to the device after it has output something, I don't get any output.
If I disconnect from the device and re-connect within a short time (or small amount of output), it's fine, but after a longer amount of output, I stop receiving data.
Here's a simple code example. This uses pin 1 as a light to show what it's doing (not connected to anything, just using the onboard LED) - the light comes on when we call SerialUSB.begin(), flicks off and back on when done, waits 5s, outputs "Hello, World!" and then prints 1, 2, 3, et c every second, toggling the light at each output line.
If I connect (with a terminal program, or just "cat /dev/ttyACM0") during the 5s wait, all is happy. If I wait until the light has started toggling, I get no output.
If I disconnect after output has started, it's OK if I reconnect quickly. But if I leave it a few seconds, again I get no further output.
If I insert "SerialUSB.end(); SerialUSB.begin();" before the SerialUSB.println() I get reliable output, but at the expense of delays while SerialUSB.begin() does its thing, and the ttyACM device is temporarily destroyed during that time (which of course also disconnects whatever is listening to it).
Any ideas? Ideally I'd like it to just sit there being a "serial" device that I can connect to any time to listen to its output.
-- don
Example code:
#include <DigiCDC.h>
void setup() {
pinMode(1, OUTPUT);
digitalWrite(1, 1); // Light on during USB init
SerialUSB.begin();
digitalWrite(1, 0); // Flick light when USB ready
SerialUSB.delay(100);
digitalWrite(1, 1);
SerialUSB.delay(5000); // Pause to allow connect
SerialUSB.println("Hello, world!");
}
long c = 0;
int heartbeat = 1;
void loop() {
heartbeat = !heartbeat; // Toggle light
digitalWrite(1, heartbeat);
SerialUSB.println(++c); // And output count
SerialUSB.delay(1000);
}