Trying to Receive Data from Leonardo on USB Port

I have successfully installed a Leonardo Arduino and have a simple program running. This flashes the onboard LED initially in the setup function. In the loop function, the code flashes an LED connected to a pin and outputs a string to the virtual USB serial port using the Serial class. I use a while(!Serial); loop to wait for the serial port to be established and ready.
Now this works ok and the LEDs flash and I can display the incoming data on the USB port using terminal software. However a VB program I wrote doesn’t display anything in the textbox I have added to a form. This software works ok with a connected GPS unit on COM11 when the device is connected using a USB to serial adapter. I have added code to the form load and this displays the available ports (COM3 is a modem and COM11 is the virtual com port). When I run the program and open the port, the code on the Leonardo starts running (I can see the LEDs flashing, so execution continues after the wait(!Serial) loop). However the eventhandler for data received never fires in the VB code. I also have a button on the form which sends a string to the Leonardo and when I click this, the green RX LED on the board lights.
Any ideas what the problem is? Is it something to do with the USB drivers for the device?

Leonardo code. :

int button_input =12;
int led_output =7;
int onboard_led =13;

void setup()
{
pinMode(onboard_led,OUTPUT);
pinMode(button_input,INPUT_PULLUP);
pinMode(led_output,OUTPUT);

int i;

while(!Serial);

Serial.begin(9600);
for(i=0;i<10;i++)
{
digitalWrite(onboard_led,HIGH);
Serial.println(“In Setup Loop”);
delay(500);
digitalWrite(onboard_led,LOW);
delay(500);
}

}

void loop()
{
if(digitalRead(button_input))
{
Serial.println(“hello”);
digitalWrite(led_output,HIGH);
delay(500);
digitalWrite(led_output,LOW);
delay(500);
}
else;
}

VB Code. Its a bit more complicated than this but I have extracted the valid sections:

Private Delegate Sub DelegateDataReceivedDefinition(ByVal Data As String)
Private DelegateDataReceived As New DelegateDataReceivedDefinition(AddressOf HandlerDelegateDataReceived)
Dim iasync1 As IAsyncResult
Dim timeout As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ports As String() = System.IO.Ports.SerialPort.GetPortNames()

Dim port As String
For Each port In ports
txtBoxRecData.AppendText(port)
Next port

Private Sub StartComs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartComs.Click
SerialPort1.PortName = “COM11”

If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = 9600
SerialPort1.ReadTimeout = 2000
SerialPort1.Open()
End If
End Sub

End Sub

Private Sub btnCR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCR.Click
If SerialPort1.IsOpen = True Then
SerialPort1.WriteLine(“AT”) ’ this was for sending a command to a modem
End If
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim ReceivedData As String

timeout = False

Try
ReceivedData = SerialPort1.ReadLine()
Catch ex As TimeoutException
timeout = True
Beep()
End Try

’ Use a delegate to update textbox
If timeout = False Then
iasync1 = Me.BeginInvoke(DelegateDataReceived, ReceivedData)
End If
End Sub

Private Sub HandlerDelegateDataReceived(ByVal Data As String)

txtBoxRecData.Text = Data

End Sub

The Leonardo needs the DTREnable flag set, on the SerialPort1 instance, in the VB code, when the port is opened.

You use INPUT_PULLUP for the button, so the button is to ground.
But you test if the input is HIGH.
The "if" in the code is executed if the button is not pressed.

You could change it to this:

if(digitalRead(button_input) == LOW)

The virtual com port of the Leonardo is something special. You can avoid a lot of problems by using the Serial1, the hardware serial port on pin 0 and 1. You could use a usb-to-serial-ttl converter to connect those pins to a PC.

Erdin:
You use INPUT_PULLUP for the button, so the button is to ground.
But you test if the input is HIGH.
The "if" in the code is executed if the button is not pressed.

You could change it to this:

if(digitalRead(button_input) == LOW)

The virtual com port of the Leonardo is something special. You can avoid a lot of problems by using the Serial1, the hardware serial port on pin 0 and 1. You could use a usb-to-serial-ttl converter to connect those pins to a PC.

The loop function runs as long as the input pin is high. I pull it low to halt execution so this isn't really a problem.

PaulS:
The Leonardo needs the DTREnable flag set, on the SerialPort1 instance, in the VB code, when the port is opened.

Great! That worked! I should have remembered setting the DTR line from my days programing dialup modems! (Even though there isn't actually a "DTR line" )
Thanks for the help!