none
c# .NET SerialPort - unable to receive data from port RRS feed

  • Question

  • 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()  
            {  
                InitializeComponent();  
                port1.Open();  
                port1.DataReceived += new SerialDataReceivedEventHandler(SerialReceive);  
            }  
     
            public void 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.
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                port1.WriteLine("12345");  

                // Clicking this button works.  I can see the data "12345" in my device.
            }  
     
            private void button2_Click(object sender, EventArgs e)  
            {  
                textBox2.Text = s;  
            }  
        }  
    Thursday, February 26, 2009 1:58 AM

Answers

  • Turn the hardware handshake signals on with DtrEnable and RtsEnable.  You'll then have to fix the NullReference exception you'll get in SerialReceive().
    Hans Passant.
    Thursday, February 26, 2009 4:49 AM
    Moderator

All replies

  • Turn the hardware handshake signals on with DtrEnable and RtsEnable.  You'll then have to fix the NullReference exception you'll get in SerialReceive().
    Hans Passant.
    Thursday, February 26, 2009 4:49 AM
    Moderator
  • 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.

     

     

     


    Amir
    Wednesday, June 30, 2010 8:43 PM
  • Hi,

    I am also facing the same problem, were you able to solve the issue? If yes can you please share the solution.

    Thanks,

    Raj

    Thursday, March 10, 2011 8:18 PM
  • This thread is marked as answered, so how can you have the same problem without an answer?

    Always start a new thread on a new question. Don't just use an old thread and especially one, which is already marked as answered. You may not get any answers.

     


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.
    Friday, March 11, 2011 12:40 PM