Saravanan Ponnusamy

Saravanan Ponnusamy

  • 354
  • 4.4k
  • 1.1m

windows stopped working while communicate with "COM PORT"

Mar 11 2018 9:10 AM
Here is my code...
 
public FrmHome() //Constructor 
{
InitializeComponent();
OpenSerialPort();
 
~FrmHome()
{
serialPort1.Close();
}
 
private void OpenSerialPort()
{
try
{
if (!string.IsNullOrEmpty(Properties.Settings.Default.COMPort.ToString()))
{
serialPort1 = new SerialPort();
serialPort1.PortName = Properties.Settings.Default.COMPort.ToString();
serialPort1.BaudRate = Convert.ToInt32(Properties.Settings.Default.COMBaudRate.ToString());
serialPort1.DataBits = Convert.ToInt32(Properties.Settings.Default.COMDataBits.ToString());
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.COMStopBits.ToString());
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.COMParityBits.ToString());
serialPort1.RtsEnable = false;
serialPort1.DtrEnable = false;
serialPort1.Handshake = Handshake.None;
serialPort1.ReadTimeout = 10000;
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.Open();
}
else
{
MessageBox.Show("ComPort Settings aren't yet Done.");
}
}
 
 
 
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//i tried the following three methods
 
SerialPortdataIN = serialPort1.ReadExisting();
//SerialPortdataIN = serialPort1.Read();
//SerialPortdataIN = serialPort1.ReadLine();
this.Invoke(new EventHandler(ShowData));
}
 
 
 private void ShowData(object sender, EventArgs e)
{
labeldisplayReader.Text = SerialPortdataIN;  // showing weight from com port
 
 
private void FrmHome_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
 
 
im trying to read data from com port and displaying in my application..
 
while APPLICATION START im start reading from com port (for eg. from COM2 Port).
 
but it showing error like "windows stopped working" when application start running.
 
i find out COM port communication is problem. because if i gave COM1 insted of COM2 (COM2 is actual port COM1 is not present) application is running fine. 
 
when i run my application my giving correct com port value like COM2 .. then it showing error "windows stopped working" ...
 
it raised me many doubt's..
 
i should use any threading operation ?? bec upto now im not using any threading opeartion.. (if it is... pls give me any example or any web link)
 
either i need to do any modification in my codings ???
 
 
 
 
 
 
 
 

Answers (1)