Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 978
DesMY Avatar asked Feb 24 '11 06:02

DesMY


People also ask

What is serial communication RS-232?

RS232 is a standard protocol used for serial communication, it is used for connecting computer and its peripheral devices to allow serial data exchange between them. As it obtains the voltage for the path used for the data exchange between the devices.

What is difference between RS-232 and serial port?

Serial transmission is still used extensively in industrial monitoring and embedded systems and can be used with converters on USB equipped machines. RS232 allows direct control of industrial devices like UPS systems without the need for software assistance.

How does RS-232 send data?

RS232 is a method (or protocol) that defines how to transfer data between two devices using a few wires. It uses a serial transmission method where bytes of data are sent out, one bit at a time, onto a single wire.

What is the function of RS-232?

The RS232 protocol and cable allow the computer to give commands to the printer via a voltage signal. The printer then deciphers those commands and completes the print. There are a couple of disadvantages of RS232. One is the speed at which data can be transferred.


2 Answers

        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.

like image 86
Hans Passant Avatar answered Oct 15 '22 06:10

Hans Passant


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.

like image 1
Drew Goodwin Avatar answered Oct 15 '22 06:10

Drew Goodwin