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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With