RESOLVED! Trying to send data to PC over USB (serial port)

I have the simple arduino sketch that sends "Hello World" out the serial port every couple of seconds.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Hello World");
  delay(2000);
}

If i upload the program and open the serial monitor the "Hello World" message is displayed every two seconds just as it should. But, as soon as I close the serial monitor it stops sending the message. I have verified with port monitor that it does stop.

I use a terminal emulator to open the port (Com13) using 9600 baud, 8, N, 1. It opens the port and does repeated reads. The port monitor verifies the open port Com13 succeeds but all the reads time out.

Obviously this can be done somehow because the Serial Monitor in arduino does this.

So my question is how can I send data from the Arduino to the PC?

Apparently you did something wrong. Coudl you try to use another terminal program? Double check port number using "Device Manager" (if you are using Windows) or "dmesg" command under Linux.

UPD Did you close an Arduino IDE when open your terminal program?

PickyBiker:
But, as soon as I close the serial monitor it stops sending the message. I have verified with port monitor that it does stop.

I would be very surprised if your Arduino stops unless you disconnect power from it. Mine certainly don't.

...R

I am absolutely sure the port is Com13. It is the only serial port on my PC, verified with device manager.
I tried another emulator with the same result. When I say "stops", it is not the arduino itself that stops, it is the transmission of the data that stops.

Here is the output from port monitor.

[27/02/2019 16:17:13] - Open port COM13 (C:\Program Files (x86)\Arduino\java\bin\javaw.exe)

[27/02/2019 16:17:14] Read data (COM13)
48 65 6c 6c 6f 20 57 6f 72 6c 64 0d 0a 48 65 6c Hello World..Hel
6c 6f 20 57 6f 72 6c 64 0d 0a 48 65 6c 6c 6f 20 lo World..Hello
57 6f 72 6c 64 0d 0a World..
[27/02/2019 16:17:16] Read data (COM13)
48 65 6c 6c 6f 20 57 6f 72 6c 64 0d 0a Hello World..
[27/02/2019 16:17:18] Read data (COM13)
48 65 6c 6c 6f 20 57 6f 72 6c 64 0d 0a Hello World..
[27/02/2019 16:17:20] Read data (COM13)
48 65 6c 6c 6f 20 57 6f 72 6c 64 0d 0a Hello World..
[27/02/2019 16:17:22] - Close port COM13

[27/02/2019 16:17:32] - Open port COM13 (C:\Program Files (x86)\Termite\Termite.exe)

[27/02/2019 16:17:32] Read data (COM13)
48 65 6c 6c 6f 20 57 6f 72 6c 64 0d 0a 48 65 6c Hello World..Hel
6c 6f 20 57 6f 72 6c 64 0d 0a lo World..

The first OPEN at 16:17:13 is when I opened the Serial Monitor. Then you see the data 3 times before I close the serial monitor at 16:17:22. The next open is from the terminal emulator at 16:17:32. Hello world comes out 2 more times and then just stops.

So here is what I determine from this. The Serial Monitor uses open and close to read the data. The terminal emulator sends an open but only read a bit of data before it just stops. There is no port confusion and yes, I close the arduino IDE as quickly as I can after closing the serial monitor. In fact, after all that is closed, I press reset on the arduino to get a clean start. No difference in the result.
In another view in the port monitor, the terminal emulator gets timed out on all subsequent reads.

Can't figure out why the text stop sending when the PC tries to read the data via the terminal emulator. I even wrote a small VB.net program to read the serial data and it also times out on the read.

I also noticed that I can restart arduino's IDE and the serial monitor and it immediately starts receiving the text again. (without uploading the sketch again).

Can't quite figure out what is going on.

Other info: The PC has Windows 10. The Arduino is a Leonardo.

Here is the vb.net program if anyone is interested in that.

Imports System.IO.Ports

Public Class Form1
    Dim SerialPort As New SerialPort                ' The io serial port
    Dim Incoming As String
    Dim Count As Integer = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With SerialPort
            .PortName = "COM13"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None
            .ReadTimeout = 1000
        End With
        SerialPort.Open()
    End Sub

    Function ReceiveSerialData() As String

        Try
            Incoming = SerialPort.ReadLine()
        Catch Ex As Exception
            Count += 1
            Label1.Text = Ex.Message & " " & Count
            'Application.Exit()
        End Try
        Return Incoming
    End Function

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ReceiveSerialData()
    End Sub
End Class

I have made a small adjustment to your VS code which IMO I think will serve you better now and in the future but it is entirely up to you whether you use it or not. The project is just a form with a rich text box but is a great template for future projects.

Imports System.IO.Ports

Public Class Form1
    Dim WithEvents SerialPort As New SerialPort    '********Modified
    Dim Incoming As String
    Dim Count As Integer = 0

    Public Delegate Sub myDelegate(ByVal sData As String)                '********New (research delegates)

    Private Sub Text_Out(ByVal sData As String)    '********New (research delegates)
        RichTextBox1.AppendText(sData.ToString)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With SerialPort
            .PortName = "COM13"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None
            .ReadTimeout = 20        '********Modified......1 second is too long for the timeout
        End With
        SerialPort.Open()
    End Sub

    Private Sub SerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived         '********New...this is a secondary thread

        Do While SerialPort.BytesToRead > 0
            Try
                Incoming = SerialPort.ReadLine
                Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), Incoming)   '********New (research delegates)
            Catch ex As Exception

            End Try
        Loop

    End Sub
End Class

sumguy thanks. A quick look makes me think this is somehow using events to read the serial port. In any case I built it and ran it. The results are the same, no data is being received.

There must be something missing in the communications between the PC and Arduino that I have yet to discover.

I will do some research on the delegates, as soon as I get past this particular issue.

Success!

In the protocol setup in the terminal emulator and the vb program I changed the handshake from "None" to "RequestToSend" and that fixed it. Apparently the arduino was waiting for a request to send the data.

I spent a day and a half on this, but now I know a bit more about comms between PC and Arduino.