Serial Port on Startup

bdoan

Well-known member
1733940640988.png

I am having an issue with serial communication on a Teensy 4.1
(1) -- When I first power the board up, I cannot receive serial characters from the Teensy with my Delphi application.
(2) -- If I close that application and open Terminal.exe , The response characters show up, but preceeded by byte values 00 00
(3) -- If I close terminal and then reopen my application , communication resumes normally.

What is causing the Teensy serial to transmit (2) null characters?

Code:
char USBrxData[20];
int USBrxptr = 0; 
// *********************************************************************
void setup()
{
    Serial.begin(115200);
    while (!Serial) {};    // Wait for serial to start
}
// *********************************************************************
void loop()
{
USBrx();
}
// *********************************************************************
void USBrx()
{
char RXChar;
if (Serial.available() > 0)   // USB UART RX  = 0
    {
    RXChar = Serial.read();
    if (RXChar == 0x0D )
        {
        USBrxData[USBrxptr+1] = 0;  // terminate string
        DecodeCmd();
        }
      else
        {
        USBrxData[USBrxptr] = RXChar;
        USBrxptr++;
        }
    }
}
// *********************************************************************
void DecodeCmd()
{
int ptr;
if (USBrxData[0] == '?' && USBrxData[1] == 'V')      // Respond to GUI
  Serial.println("TWA2test");
for (ptr=0;ptr<20;ptr++)    // clear rx buffer
  USBrxData[ptr] = 0;
USBrxptr = 0;               // reset pointer
}
// ------------------------------------------------------------------------
 

Attachments

  • 1733940549533.png
    1733940549533.png
    35.5 KB · Views: 13
Unlikely that the Teensy is transmitting the two NULL characters...much more likely that your app connecting/disconnectng is leaving them in the RX buffer.

Comment out the while (!Serial) {}; line (which, when uncommented, will wait forever for a valid serial terminal, such as the Serial Monitor in the Arduino IDE, to "connect" . . . the function of this line of code is not just "is the serial hardware ready ??") & see if the behavior is different/better . . .

Mark J Culross
KD5RXT
 
Unlikely that the Teensy is transmitting the two NULL characters...much more likely that your app connecting/disconnectng is leaving them in the RX buffer.

Comment out the while (!Serial) {}; line (which, when uncommented, will wait forever for a valid serial terminal, such as the Serial Monitor in the Arduino IDE, to "connect" . . . the function of this line of code is not just "is the serial hardware ready ??") & see if the behavior is different/better . . .

Mark J Culross
KD5RXT
It works, Thank you
 
Which version of Windows are you using?

Windows 7 and all pre-10 versions had several bugs in their USBSER.SYS driver. Microsoft finally fixed those problems with Windows 10.
 
Back
Top