c# .NET SerialPort – unable to receive data from port

I am writing my first C# program and its purpose is to communicate with a device through RS-232 using a serial port.  The device is setup to transmit and receive some data.

When using HyperTerminal, I can succesfully send data to the device and succesfully receive data from the device, so I know wiring, communication settings, and the device are all correct.

I started writing a simple C# program using Windows Forms to test sending and receiving data.  Using this small program, I can succesfully send data to my device.  I cannot, however, receive any data from the device.

There is something odd going on which I cannot figure out due to my lack of C# and .NET experience.  When I run my program and tell my device to send data, I get nothing.  When I close the program and run it again, I get the previously sent line of data from the device.  In other words, when my application is running, the device is not able to send data through the port.  I don’t know if there is some handshaking element that is preventing the device from sending data or what…

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using System.IO.Ports;  
namespace WindowsFormsApplication2  
public partial class Form1 : Form  
        SerialPort port1 = new SerialPort(“COM1”, 9600, Parity.Odd, 7, StopBits.One);  
string s = null;  
public Form1()  
            port1.DataReceived += new SerialDataReceivedEventHandler(SerialReceive);  
publicvoid SerialReceive(object sender, SerialDataReceivedEventArgs e)  
            s += port1.ReadExisting();  

            // The contents of s never change unless the application is closed and restarted.
            // When the application is restarted, s contains the data sent by the device
            // during the previous instance of the application.

privatevoid button1_Click(object sender, EventArgs e)  

            // Clicking this button works.  I can see the data “12345” in my device.

privatevoid button2_Click(object sender, EventArgs e)  
            textBox2.Text = s;  

Turn the hardware handshake signals on with DtrEnable and RtsEnable.  You’ll then have to fix the NullReference exception you’ll get in SerialReceive().

Hi. I’m trying to run  the code from:http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx Still (after enabling DtrEnable and RtsEnable after “_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);”) -I get timeout when trying to read from port… What am I doing wrong?? Thanks.