Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: "Serial" object is not available in Teensy4.0

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    13

    "Serial" object is not available in Teensy4.0

    Hello. I'm new to Teensy. Previously used Arduino UNO without problems.

    I was trying to troubleshoot some code and I noticed that there was no output to my Serial Monitor (Windows7)

    Therefore I modified the BLINK example and what happens is the LED blinks once every 2 seconds, indicating there is no Serial object available to write to.

    How should I tackle this one?

    Thanks

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,352
    sorry, I am not sure what you are saying...
    Teensy for sure has Serial object...

    Note: Different than Arduino Uno. Arduino UNO which has no native USB support, relies on another chip to do it's communications, and as it only has one Hardware UART, it talks on Pins 0 and 1, which again is it's only Hardware Serial port.

    Teensy boards like the 4 have Native USB support built in... Actually it has to USB ports built in.
    And it does not need to use external IO pins to communicate.
    So pins 0 and 1 are not used for the Serial object... Note: T4 has something like 7 hardware Uarts which correspond to objects Serial1 - Serial7.
    Serial1 is on pins 0 and 1.

    The Blink program uses IO pin 13 to do blinking... So not related at all to if you can do Serial communications to either USB (Serial) nor Serial1

    Also note: The hardware USB, can be configured to work in different ways, most often as Serial, but could be a Mouse, or Keyboard, and can be turned off...
    Controlled by the Tools->USB ... menu item. At times USB will be configured for HID (Human Interface Device) and not Serial and as such Windows will not assign a comm port to it...
    But most of the time it is typically configured as Serial and everything you read/write to the Serial object will go to the host computer...

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,238
    Other possible problems...

    If your program prints once without first waiting for the Arduino Serial Monitor to open, you might miss it. Printing every time loop() runs is best.

    Windows 7 has a known driver bug where the COM port doesn't work the *next* time Teensy (or any other CDC serial device) connects, if any program had the port open when Teensy rebooted. Normally not an issue with Arduino IDE, because it closes the port before attempting upload, but a real pain for other software. Microsoft fixed this in Windows 10, but never ported the fix back to Windows 7 or 8.

    But this is just blind guesswork, since we don't know what code you're running on Teensy or which software you're using on Windows 7 machine.

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    13
    Sorry. I forgot to insert my code:

    The LED blinks once every 2 seconds, meaning the if(Serial) is evaluated as FALSE, meaning there is no Serial object to begin with.

    Code:
    /* LED Blink, Teensyduino Tutorial #1
       http://www.pjrc.com/teensy/tutorial.html
     
       This example code is in the public domain.
    */
    
    // Teensy 2.0 has the LED on pin 11
    // Teensy++ 2.0 has the LED on pin 6
    // Teensy 3.x / Teensy LC have the LED on pin 13
    const int ledPin = 13;
    
    // the setup() method runs once, when the sketch starts
    
    void setup() {
      Serial.begin(9600);
      Serial.println("START"); 
      
      // initialize the digital pin as an output.
      pinMode(ledPin, OUTPUT);
    }
    
    // the loop() methor runs over and over again,
    // as long as the board has power
    
    void loop() {
      
      digitalWrite(ledPin, HIGH);   // set the LED on
      if(Serial)
      {
      Serial.println("B"); 
      delay(500);                  // wait for a second
      digitalWrite(ledPin, LOW);    // set the LED off
      delay(500);                  // wait for a second
      }
      else
      {
      delay(2000);                  // wait for a second   
      digitalWrite(ledPin, LOW);    // set the LED off
      delay(2000);                  // wait for a second
        }
    }
    Last edited by KurtE; 11-02-2021 at 02:10 AM. Reason: code tags

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,352
    Note, I ran this on T4, using Beta Teensyduino...

    Code:
    START
    B
    B
    B
    B
    B
    B
    B
    B
    B
    ...
    Again for Serial to be active you have to have a terminal window opened on and connected up on that Serial port in my case on COM105

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    13
    Quote Originally Posted by KurtE View Post
    Note, I ran this on T4, using Beta Teensyduino...

    Code:
    START
    B
    B
    B
    B
    B
    B
    B
    B
    B
    ...
    Again for Serial to be active you have to have a terminal window opened on and connected up on that Serial port in my case on COM105


    I'm using Arduino IDE 1.8.16.

    It is configured to COM4. That can't be wrong as otherwise it would not be able to upload the sketch.

    Even the caption of the Serial Monitor window is changing from "Offline (Teensy)" to "COM4 (Teensy) Serial)" after I push the button to upload a sketch. Therefore, the IDE notices the Teensy.

    So the problem I see now is that the Serial object does not get created runtime. Can i do something to prompt the Teensy to create the Serial object at runtime?

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,890
    Starting the Teensy - built witrh USB type : Serial is all that is needed.

    Is there another program active stealing the Serial?

    Close the Serial Monitor , and click OPEN again - it doesn't always discover and reconnect as it should.

    When looking at the 'Ports' list - does it show from there? It could be programmed without having the correct port depending on what else is going on.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,238
    Quote Originally Posted by bavareze View Post
    Even the caption of the Serial Monitor window is changing from "Offline (Teensy)" to "COM4 (Teensy) Serial)" after I push the button to upload a sketch. Therefore, the IDE notices the Teensy.
    Yup, that sounds like the Windows 7 driver bug.

    On Windows 7, first reboot your PC. Then when you use Arduino, use Upload in the toolbar to start the sketch upload. Or if you will press the pushbutton on Teensy, always close the serial monitor window before pressing the button if running a pre-10 version of Windows! If you press the button while the serial monitor or any other program has the COM port open, you will trigger the USBSER.SYS driver bug, which causes the COM port to not work the *next* time.

    This is absolutely a bug in Windows. Microsoft fixed it in Windows 10, but they never ported the fix back to Windows 7 or 8.

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,238
    Quote Originally Posted by bavareze View Post
    It is configured to COM4. That can't be wrong as otherwise it would not be able to upload the sketch.
    All uploading is done using HID protocol. There is never a COM port during code uploading. Teensy uses HID protocol for upload, never serial. It only uses serial after you have uploaded a program, and only if the program you uploaded implements USB serial.

    To cause Teensy to implement something else, click the Tools > USB Type menu. Select RawHID or MIDI or any of the others which do not have Serial. Then when you upload, Teensy will not be a COM port at all and you can completely avoid the buggy USBSER.SYS in Windows 7.

    For those non-serial modes, a HID interface is used, so you can still print to the serial monitor. You'll see HID interfaces appear in the Tools > Ports menu.

  10. #10
    Junior Member
    Join Date
    Sep 2021
    Posts
    13
    Quote Originally Posted by PaulStoffregen View Post
    All uploading is done using HID protocol. There is never a COM port during code uploading. Teensy uses HID protocol for upload, never serial. It only uses serial after you have uploaded a program, and only if the program you uploaded implements USB serial.

    To cause Teensy to implement something else, click the Tools > USB Type menu. Select RawHID or MIDI or any of the others which do not have Serial. Then when you upload, Teensy will not be a COM port at all and you can completely avoid the buggy USBSER.SYS in Windows 7.

    For those non-serial modes, a HID interface is used, so you can still print to the serial monitor. You'll see HID interfaces appear in the Tools > Ports menu.
    Let's try something else:

    I uploaded the sketch above to my Teensy, then removed to my computer and plugged it to a generic 5V power source. So the winwows has nothing to do anymore. The led still flashes every 2 seconds showing that the if (Serial) is evaluated as false. Am I supposed to have a "Serial" object created while connected to something else than a PC that is listening or not?

    I'm just trying to distinguish between a possible PC problem and a Teensy issue. So i know where to dig further.

    Thanks

  11. #11
    Junior Member
    Join Date
    Sep 2021
    Posts
    13

    solved

    Incredible solution: I bought another Teensy.

    The first one opened COM4, blinked slowly, even though the Serial Monitor window was opened

    The second Teensy opens COM7. It also blinks slowly, but as soon as I open the Serial Monitor window, it will start blinking faster, and I see the B B B B B messages showing up, as expected.

    Is it common for these products to be faulty?

    In addition to that: can I decide what port to use, or it is assigned randomly?

    Thanks!

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,238
    Teensy should be very reliable.

    But the serial driver on Windows 7 (and 8) is a hot mess.


    Quote Originally Posted by bavareze View Post
    In addition to that: can I decide what port to use, or it is assigned randomly?
    Each time Windows sees a new device as a serial port, it assigns a new COM port number starting at COM3. If you change Tools > USB Type of one of the other options with Serial and upload, as far as Windows is concerned you've just plugged in a completely different USB product and it will assign a new COM port number. Likewise, if you edit usb_desc.c and change the serial number Teensy transmits, as far as Windows is concerned you've purchased another copy of the same product, but it's a physically different device since it has a different serial number, so Windows assigns a new COM port number.

    This is simply how Microsoft has made every version of Windows. It has some upsides and downsides, compared to how Linux and MacOS work...

    There are ways to force Windows to use different COM port numbers, but unless you *really* know what you're doing, it's only a path of painful problems. Actually, I'd be amazed if it even worked well for Windows experts.


    or it is assigned randomly?
    No, it's sequential. The first time you plug in some serial device, it gets assigned COM3. Then if you plug in something different (where any change to USB ID numbers or serial number is considered something different) it gets COM4. In a simple consumer-oriented world where you only buy stuff and never make anything of your own, it's supposed to be simple. Each physical device you own gets its own COM port number, so if you plug the original one back in, it should again be COM3.

    It generally works, but do keep in mind Windows 7 has a lot of serial driver bugs. This stuff never works really well on pre-10 versions of Windows. If you can update to Windows 10 (or Linux) things work so much better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •