Powershell serial communication with TeensyLC

Status
Not open for further replies.
Hey everyone,

I've been working on a project that requires ubs serial communication between a desktop running Windows 10 via powershell, and a TeensyLC. I began developing for this project with a Teeny 2.0, which has been and still is working great, but as soon as I swap the 2.0 out for the LC I run into a snag immediately. I'd suspect I'm just missing some crucial difference between the two boards that I was taking for granted when using the 2.0, but after many hours of reading forum posts and documentation I haven't come any closer to a solution.

To make things simple, I've recreated the problem with just a couple lines of code:

Code:
void setup() {
  Serial.begin(9600);
  while(!Serial);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:

}

The powershell commands I use are:

Code:
$port = New-Object System.IO.Ports.SerialPort COM40,9600
$port.open()

I am aware that the COM port is subject to change, and I check the COM port the boards are using each time.

With a Teensy 2.0 the led will turn on immediately (I of course use pin 11 for the led on a 2.0) when opening the port through powershell, however an LC will sit inside of that while loop forever. What leads me to believe that I am simply missing something is that if I use the arduino serial monitor, both boards will make it through that while loop just fine. I've tried about 5 different LC boards, one of them being brand new, all showing the same results so I don't think I have defective boards. I think that covers just about all of the relevant information.

If anyone has any ideas I would be very happy to hear them.

Thanks in advance,
Travis
 
To make things simple, I've recreated the problem with just a couple lines of code:

Code:
void setup() {
  Serial.begin(9600);
  while(!Serial);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:

}

The powershell commands I use are:

Code:
$port = New-Object System.IO.Ports.SerialPort COM40,9600
$port.open()

I am aware that the COM port is subject to change, and I check the COM port the boards are using each time.

With a Teensy 2.0 the led will turn on immediately (I of course use pin 11 for the led on a 2.0) when opening the port through powershell, however an LC will sit inside of that while loop forever. What leads me to believe that I am simply missing something is that if I use the arduino serial monitor, both boards will make it through that while loop just fine.

which while loop are you talking about?
 
which while loop are you talking about?

while(!Serial) in the setup() function.

Edit: My wording was unintentionally ambiguous. When I said "make it through that while loop", what I'd meant was "make it out of the while(!Serial) loop"

Sorry for the confusion
 
Code:
$port = New-Object System.IO.Ports.SerialPort COM40
[B][COLOR=#ff0000]$port.DTREnable = "true"
$port.RTSEnable = "true"[/COLOR][/B]
$port.open()
 
Code:
$port = New-Object System.IO.Ports.SerialPort COM40
[B][COLOR=#ff0000]$port.DTREnable = "true"
$port.RTSEnable = "true"[/COLOR][/B]
$port.open()

That works, thank you!

I apologize if this is a silly question, but is there a reason this is not necessary for the 2.0 boards?

Perhaps I should spend some time this weekend reading further into serial communication.
 
DTR and RTS are special handshake signals.
DTR is DataTerminalReady, RTS is RequestToSend.
Perhaps one of both is enough (did not test this much), and it may be that it is better to move the lines after the open() line, to avoid a race-condition when Teensy starts sending data, but the port is not open?

I guess Teensy2 has just the older codebase, and "Serial" does not reflect if it is ready to send/receive data.
Should be somewhere in the USB-Code.
 
That would make sense. I wonder if Serial would always return true on a 2.0 then.

In the case of the example in my original post, only DTR appears to be required. I've been reading through the usb code in an effort to find a concise explanation but I'm afraid it may be a bit over my head.

Edit: Serial definitely does not always return true with a Teensy2.
 
Last edited:
For Teensy 3.x /LC , it looks like this
Code:
        operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) &&
        ((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 15);
    }
(You're right, DTR is enough)

https://github.com/PaulStoffregen/cores/blob/master/teensy3/usb_serial.h#L112

Teensy 2: Havn't found it, yet :)

Edit: Found it, and that's interesting:
Code:
usb_serial_class::operator bool()
{
    if (usb_configuration &&
      (cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS))) {
        return true;
    }
    return false;
}
https://github.com/PaulStoffregen/cores/blob/master/usb_serial/usb_api.cpp#L442
So, DTR or RTS.
Maybe Powershell sets RTS by default to true?
Don't know. stopping that now.. i don't use AVRs anymore, since years.
 
Last edited:
No need to investigate further on my behalf, but I felt the need to check the status of RTS and DTR in powershell after creating the SerialPort object and it would appear that both are defaulted to false. Weird.

At any rate my project is now working as intended (for now...)
Thank you for your help today, Frank.
 
Status
Not open for further replies.
Back
Top