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

Thread: Serial Comm over USB Teensy 4.1

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    9

    Serial Comm over USB Teensy 4.1

    Is there any secret to sending/receiving data from the PC via the USB connection to the Teensy (4.1) ??

    Here's my setup:
    I have a PC program that I want to communicate with the teensy over the USB serial port. The teensy is opening the Serial connection on its side and waiting for a message.
    Then the PC sends a message, teensy reads it, and replies. Sounds simple but here's what happens:
    The PC opens the com port OK, sends the message, the teensy gets the message and replies, but the reply doesn't come back to the PC.
    If I close the PC connection and then get on the Arduino interface and open the serial monitor, the reply then comes back on the serial monitor!

    It would seem that the teensy is getting the message from the PC, but the reply is sticking somewhere. PC is running Windows 10 with a program written in BASIC.
    I can send messages back and forth all day long using the serial monitor so it would seem the teensy side is doing what it needs to do.

    Thoughts???

    NOTE: After further experimenting, it appears the message is NOT reaching the teensy from the PC until AFTER I close the pc program and open the serial monitor, so it's in a buffer somewhere but not really sent. But after opening the serial monitor it actually shows up...?????
    Last edited by cdavis2; 06-23-2020 at 08:27 PM.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,724
    don't have the specifics just now - but it may be a "control signal" setting to trigger the Teensy to see the port as connected/open. It seems there is one that needs to be handled to wake the Teensy to a valid connection existing.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,219
    I assume you are talking about VB.net right? If so, can you post your PC program? Ideally a minimal version showing the effect.

  4. #4
    Junior Member
    Join Date
    Sep 2019
    Posts
    9
    This is written in POWERBASIC and EZ_GUI but is basically similar to VB.

    I extracted a bit of the code, because the whole program is 7000 lines, most of which are not of any interest.

    LOCAL BYTES_REPLY AS LONG
    LOCAL REPLY_MSG AS STRING
    COM_HANDLE = FREEFILE
    COM_PORT = EZ_GetText("FORM1", %FORM1_TEXTCOMPORT) 'GET THE COM PORT NUMBER
    COM_PORT = TRIM$(COM_PORT) 'MAKE SURE NO BLANKS
    IF LEN(COM_PORT)>1 THEN ' USE ALTERNATE FORM
    COM_NAME="\\.\COM"+COM_PORT 'FORM COM PORT NAME
    ELSE
    COM_NAME="COM"+COM_PORT 'USE SHORT NAME
    END IF
    COMM OPEN COM_NAME AS COM_HANDLE 'TRY TO OPEN THE PORT
    COM_ERR=ERRCLEAR ' CHECK FOR AND CLEAR ERROR
    IF COM_ERR>0 THEN 'SHOW ERROR MESSAGE IF FAILED
    BEEP
    EZ_SetText "FORM1", %FORM1_TEXTMSG, "COMMUNICATION FAILURE, CODE="+STR$(COM_ERR)
    EXIT SUB 'FAILED SO BAIL OUT
    END IF
    SLEEP 1000 'wait 1 second
    BYTES_REPLY=COMM(COM_HANDLE, RXQUE)
    IF BYTES_REPLY=0 THEN
    EZ_SetText "FORM1", %FORM1_TEXTMSG, "NO REPLY!"
    EXIT SUB
    END IF
    COMM RECV COM_HANDLE, BYTES_REPLY, REPLY_MSG

    Basically what is happening is that I get no reply bytes, so it displays "NO REPLY!" and quits. If it got a reply, the last line would read them.

  5. #5
    Junior Member
    Join Date
    Sep 2019
    Posts
    9
    Thanks for that suggestion... It turned out you have to set the DTR (Data Terminal Ready) line ON before the PC will connect to the Teensy. Who knew... Anyway, all seems OK now.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,724
    Quote Originally Posted by cdavis2 View Post
    Thanks for that suggestion... It turned out you have to set the DTR (Data Terminal Ready) line ON before the PC will connect to the Teensy. Who knew... Anyway, all seems OK now.
    Good work. Apparently I once knew that ... DTR (Data Terminal Ready) it is. ... just hoped you'd find it or someone else would post with that clue.

    Posted back some years about a cool android phone terminal program - it worked to allow enabling DTR when others did not with OTG cable.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,061
    Communication should be possible without raising DTR. But if code on the Teensy side is doing something like "while (!Serial) ;" to wait for the PC side to open the port, indeed that boolean use of "Serial" expects DTR to go high to indicate the PC is present and software running on the PC has opened the COM port and is ready to communicate.

    Windows is unique, in that it doesn't necessarily raise DTR when the port is opened, even though that's the standard way to communicate with serial devices that software on the PC really is listening. Mac and Linux automatically raise DTR upon open. Windows does too, if configured to do so, but Microsoft provides an API to configure whether DTR will be high or low when the port is opened.

Posting Permissions

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