Serial port issue

Status
Not open for further replies.
So, I'm attempting to use a VBscript to find the com port number of my teensy automatically, and this is a challenge on it's own, but it's made worse with this teensy issue I keep running into. Currently the teensy prints "Hello" after a few seconds:

Code:
void setup()
{
delay(6000);
Serial.begin(9600);
Serial.write("Hello");
Serial.close
}

void loop()
{
}

So this works the first time, but for some reason it does not work on subsequent attempts. So the very first time I plug this in after the computer starts it prints "Hello" into the serial buffer like it should, but when I unplug it and plug it back in to run the test again it does not work. The Arduino IDE doesn't acknowledge that it it's connected (and won't open the serial monitor), also in the command line mode.com doesn't show the device even though it's plugged in.

I have used this Teensy before on other projects and it hasn't given me trouble till now. Also of note, I'm running it as a USB Keyboard/Mouse/Joystick/Serial device as the final project requires keyboard and serial functionality.
 
This only sends "Hello" once, so it will only work if you run your VBscript program within the first 5.999 seconds of Teensy powering up or starting your program after an upload from Arduino. If you run your VBscript 6.001 seconds after connecting Teensy, it'll be too late.

You could move the Serial.write("Hello") line and delay into loop().

If you only want to send "Hello" once, you could structure your program like this:

Code:
void setup()
{
  Serial.begin(9600);
  Serial.write("Hello");
}

void loop()
{
  while (!Serial) ; // wait for a program on the PC to open the serial port
  delay(500);
  Serial.write("Hello");

  delay(6000);  // do whatever the program will do while the PC is talking

  while (Serial) ; // wait for the PC's program to close the serial port
}
 
This only sends "Hello" once, so it will only work if you run your VBscript program within the first 5.999 seconds of Teensy powering up or starting your program after an upload from Arduino. If you run your VBscript 6.001 seconds after connecting Teensy, it'll be too late.

You could move the Serial.write("Hello") line and delay into loop().

If you only want to send "Hello" once, you could structure your program like this:

Code:
void setup()
{
  Serial.begin(9600);
  Serial.write("Hello");
}

void loop()
{
  while (!Serial) ; // wait for a program on the PC to open the serial port
  delay(500);
  Serial.write("Hello");

  delay(6000);  // do whatever the program will do while the PC is talking

  while (Serial) ; // wait for the PC's program to close the serial port
}

Thanks for the updated code, but I don't think this addresses the issue. The issue I'm having has nothing to do with the VBscripts timing, but it's more of windows just isn't recognizing the serial connection after the first time it's plugged in. The keyboard portion of the connection still works, I can make the teensy type things all day long without trouble. It's something to do with the serial connection that's just not being reset.
 
Thanks for the updated code, but I don't think this addresses the issue. The issue I'm having has nothing to do with the VBscripts timing, but it's more of windows just isn't recognizing the serial connection after the first time it's plugged in. The keyboard portion of the connection still works, I can make the teensy type things all day long without trouble. It's something to do with the serial connection that's just not being reset.

serial.begin - in windows, I always use a delay(2000); after that due to how long it takes Windows to enumerate, etc. And it varies.
Your setup() uses the serial port before it is ready.

Sometimes, but not always it seems, you use instead of the delay, this:
while (!Serial)
;

But sometimes that hangs.
 
Status
Not open for further replies.
Back
Top