RS232 serial communication C # Win7 .NET Framework 3.5 SP1

Hello
I am a newbie to c# serial port. Instantly write a running c# program is winXP and win7, in order to keep the data received from the serial port when the machine sends 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;< br /> 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)< br /> {
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
richTextBox1.Text = indata;

}

}

}

COM6 is active in my computer. But my problem seems to be that when the data comes from the serial port, the datareceived event does not fire. (I checked this sport by using some free software applications)

Who can help?

Thank you

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

This will not work, after opening it, you will close the serial port after a few microseconds. Yes, The DataReceived event handler cannot be triggered. Only the port is closed when the program is closed.

mySerialPort.Handshake = Handshake.None

This is also a problem, you now You need to control the handshake signal yourself. Most serial devices will not send anything until they see that the machine is powered on and ready to receive. Set the DtrEnabled and RtsEnabled properties to true.

< p>Hello
I am a newbie to c# serial port. Instantly write a running c# program is winXP and win7, so as to keep the data received from the serial port when the machine sends data.

< /p>

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


namespace RS232RVR
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
SettingRS232();
}< br />
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 computer. But my problem It seems that when the data comes from the serial port, the datareceived event will not be triggered. (I checked this sport by using some free software applications)

Who can help?

Thank you

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

This will not work. After opening it, you will close the serial port after a few microseconds. Yes, the DataReceived event handler cannot be triggered. Only when closing the program Close the port.

mySerialPort.Handshake = Handshake.None

This is also a problem, you now need to control the handshake signal yourself. Most serial devices are in Nothing will be sent until you see that the machine is powered on and ready to receive. Set the DtrEnabled and RtsEnabled properties to true.

Leave a Comment

Your email address will not be published.