Results 1 to 7 of 7

Thread: COMM_Print() never returns when opening Arduino UNO R3 serial port

  1. #1

    COMM_Print() never returns when opening Arduino UNO R3 serial port

    If I have an Arduino Uno R3 plugged into my Windows 8 PC, on COM18, and run the following program:

    Uses "Console" 
    Uses "COMM"
    
    Dim hComm     As Long
    Dim nBytes    As Long
    Dim sBuffer   As String
    
    hComm = COMM_FreeFile
    Console_WriteLine("Opening")
    COMM_Open("\\.\COM18", hComm)
    Console_WriteLine("Done with Err = " & Err)
    COMM_Print(hComm, "V" + Chr$(13))    
    nBytes = COMM_Get(hComm, %COMM_RXQUE)
    COMM_TRecv(hComm, nBytes, sBuffer, 1000)
    Console_WriteLine("Pre print")
    COMM_Print(hComm, "V" + Chr$(13))    
    Console_WriteLine("Post print")
    COMM_Close(hComm)    
    Console_WriteLine("Press any key to exit.")
    
    Console_WaitKey
    
    then I get
    Opening
    Done with Err = 0
    Pre print
    
    and then it hangs there. I have to unplug the Arduino, at which time the program finishes properly.

    I can do this same thing (print "V" + chr$(13) twice) to the Arduino using a terminal emulator just fine, without any hangs. Note that the Arduino is not sending anything back - nBytes is always zero.

    Any ideas on how I can debug this further? Why would a COMM_Print() not ever return? Is there some type of COMM setting that I can change to try and help thing?

    It's critical that I do two writes with a read between them, or the program doesn't hang. So there's something going on with this USB serial port that involves a writing, then a reading, then the next write never returning.

    *Brian

  2. #2
    I know there's something a little strange with the Arduino Uno, as none of my other USB serial devices (and I've tried many) cause this same hang.

    Is there any way to see more what's happening inside ThinBasic when it does the COMM_Print() call?

    *Brian

  3. #3
    Another clue : If I plug the UNO in, and run the sample program above, I get the hang.

    If I plug the UNO in, then open up COM18 in a terminal emulator (like Tera Term), then close the terminal emulator (note, I don't type anything), and THEN run the above test program it works fine - no problems.

    So there's something going on with how the com port is set up (within Windows?) that TeraTerm does right, but is not being done in my sample program.

    *Brian

  4. #4
    Nevermind. :-) I got it figured out.

    For whatever reason, the UNO absolutely needs the baud rate set (to any value - it doesn't seem to matter).

    So the following program:

    Uses "Console" 
    Uses "COMM"
    
    Dim hComm     As Long
    Dim nBytes    As Long
    Dim sBuffer   As String
    
    hComm = COMM_FreeFile
    Console_WriteLine("Opening")
    COMM_Open("\\.\COM18", hComm) 
    COMM_Set(hComm, %COMM_BAUD, 123)
    Console_WriteLine("Done with Err = " & Err)
    COMM_Print(hComm, "V" + Chr$(13))
    nBytes = COMM_Get(hComm, %COMM_RXQUE)
    COMM_TRecv(hComm, nBytes, sBuffer, 1000)
    Console_WriteLine("Pre print")
    COMM_Print(hComm, "V" + Chr$(13))    
    Console_WriteLine("Post print")
    COMM_Close(hComm)    
    Console_WriteLine("Press any key to exit.")
    
    Console_WaitKey
    
    Works every time!!!

    No idea why, but I don't much care. It works.

    *Brian

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Hi Brian,

    I was to reply to set some communication settings before start sending/reading data.
    Serial communications always need to "handshake" a common way to talk before talking.
    Maybe (but quite sure it is so) your terminal emulator send communication setting (maybe default ones) at the very beginning when you open the serial port.

    Use a more classic 9600 baud rate instead of 123 or one of the standard rates: https://www.arduino.cc/en/Serial/Begin

    Thanks for letting us know the problem and the solution

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Eros,

    Yes! That was the solution to the problem. This device needed to see a baud rate change message over USB before it would properly allow reading or writing from the PC, and the terminal emulator always sends default settings to the COM port on startup, like you say.

    The baud rate doesn't matter in this case, since the device on the other side of the UART link is not trying to read any bytes from the PC nor write any data back to the PC.

    If I were expecting to actually TX or RX data with the Arduino, then I would have to set the COM port's baud rate to the baud rate that the Arduino sketch used. But in this case, all that mattered was that a baud rate message was sent, not the actual value of the baud rate.

    *Brian

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10


    Programming sometimes is an act of faith!
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. can't opening comm
    By hendryawan in forum COMM (Serial communication)
    Replies: 15
    Last Post: 11-12-2012, 02:38
  2. Olympics Opening Ceremony
    By Charles Pegge in forum Shout Box Area
    Replies: 6
    Last Post: 13-08-2012, 03:00
  3. Arduino and ThinBasic
    By oldpapa49 in forum thinBasic General
    Replies: 4
    Last Post: 12-04-2012, 15:02
  4. opening 2 forms
    By hendryawan in forum UI (User Interface)
    Replies: 6
    Last Post: 15-02-2011, 11:19

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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