DesMY
DesMY

Reputation: 71

RS232 serial port communication c# win7 .net framework 3.5 sp1

HI im new in c# serial port. im writing a c# program running is winXP and win7 to keep received data from the serial port when the machine was sent data.

using System.IO;
using System.IO.Ports;
using System.Threading;


namespace RS232RVR
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SettingRS232();
    }

    public void SettingRS232 ()
    {
        try
        {
            SerialPort mySerialPort = new SerialPort("COM6");

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None; //send to hardware flow control.

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);

            mySerialPort.Open();


            richTextBox1.Text = "on";

            mySerialPort.Close();
        }
        catch (Exception ex)
        {
            richTextBox1.Text = ex.Message;    
        }

    }

    private void DataReceviedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        richTextBox1.Text = indata;

    }

}

}

COM6 is active in my pc. but my problem was seem the datareceived event is not fire when it has data coming from the serial port. ( i had checked the sport by using some of the freeware application)

anyone can help?

thanks

Upvotes: 6

Views: 24104

Answers (3)

DesMY
DesMY

Reputation: 71

The problem was solved and i would like to share it. i had fine dunning as below:

namespace RS232RVR
{
    public partial class Form1 : Form
    {
        private delegate void SetTextDeleg(string data);

        public Form1()
        {
            InitializeComponent();
            SettingRS232();
        }

        public void SettingRS232 ()
        {
            try
            {
                SerialPort mySerialPort = new SerialPort("COM6");

                mySerialPort.BaudRate = 9600;
                mySerialPort.Parity = Parity.None;
                mySerialPort.StopBits = StopBits.One;
                mySerialPort.DataBits = 8;
                mySerialPort.Handshake = Handshake.None;
                mySerialPort.ReadTimeout = 2000;
                mySerialPort.WriteTimeout = 500;

                mySerialPort.DtrEnable = true;
                mySerialPort.RtsEnable = true;

                mySerialPort.Open();
                //mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                mySerialPort.DataReceived += DataReceivedHandler;

                textBox1.Text = "Serial Port is Ready.";

            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;    
            }

        }

        public void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            System.Threading.Thread.Sleep(500);
            string indata = sp.ReadExisting();
            this.BeginInvoke(new SetTextDeleg(DisplayToUI), new object[] { indata });
            //textBox1.Text += indata;

        }

        private void DisplayToUI(string displayData)
        {
            textBox1.Text += displayData.Trim();
           // textBox1.Text += displayData;

        }

    }
}

If anyone have comment on the code you are welcome, is my pleasure and wish to code it better.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941218

        mySerialPort.Open();
        richTextBox1.Text = "on";
        mySerialPort.Close();

That's not going to work, you'll close the serial port a couple of microseconds after opening it. Yes, the DataReceived event handler is not likely to fire. Only close the port when shutting down your program.

        mySerialPort.Handshake = Handshake.None

That's a problem too, you'll need to control the handshake signals yourself now. The vast majority of serial port devices won't send anything until they see the machine powered up and ready to receive. Set the DtrEnabled and RtsEnabled properties to true.

Upvotes: 5

Drew Goodwin
Drew Goodwin

Reputation: 173

Did you copy that code from your application? Is it perhaps just a case that the event handler name is misspelled? E.g. DataReceviedHandler should actually be spelt DataReceivedHandler.

Upvotes: 1

Related Questions