pravalika reddy

pravalika reddy

  • NA
  • 15
  • 17.7k

serialport reading error (I/O operation exception )

Mar 2 2015 6:43 AM
when i run this program, it can display the data and refresh it smoothly, and after a while, the whole form hanged.(read the data from serial port every 1 millisecond   )


please help me any idea

here my code


 private void SetComport()
        {
            if (GlobVar.nTxRxFlag == 1)       // if port configuration is successfull then  //
            {
                GlobVar.TheMCU.serialPort1.DiscardInBuffer();
                GlobVar.TheMCU.serialPort1.DiscardOutBuffer();

                GlobVar.TheMCU.serialPort1.Close();   // port is closed //
                GlobVar.nTxRxFlag = 0;          // set the default flag value //  
                GlobVar.nComflag = 0;
              
            }
            GlobVar.TheMCU.serialPort1.PortName = comboBox_ComPort.SelectedItem.ToString();
            GlobVar.TheMCU.serialPort1.BaudRate = Convert.ToInt32(comboBox_Buadrate.SelectedItem.ToString());   // Converted the selected combobox value into the Integer Value //
            GlobVar.TheMCU.serialPort1.Parity = Parity.None;
            GlobVar.TheMCU.serialPort1.DataBits = Convert.ToInt16(comboBox_DataBits.SelectedItem.ToString());    // Converted the selected combobox value into the Integer Value //

            GlobVar.TheMCU.serialPort1.Handshake = (Handshake)Enum.Parse(typeof(Handshake), comboBox_FlowControl.Text);
            GlobVar.TheMCU.serialPort1.StopBits = StopBits.One; // if select the  stopbits is one //
            GlobVar.TheMCU.serialPort1.ReadBufferSize=6400000;
        
            /***  if port is available then port is open otherwise port is can't open then error is occured   ***/
            try
            {
                if (GlobVar.TheMCU.serialPort1.IsOpen == false)
                {

                    GlobVar.TheMCU.serialPort1.Open();    // try to open the selected port //

                    MessageBox.Show("Port configuration Is Successful");  // port configuration is successful //
                    GlobVar.nTxRxFlag = 1;
                    GlobVar.nComflag = 1;


               //  
                }
            }
            catch    // give a message, if the port is not available //
            {
                MessageBox.Show("Serial port " + GlobVar.TheMCU.serialPort1.PortName +
                    " cannot be opened!, port is not available OR Port is used another process", " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);  // port error //
            }
         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
  GlobVar.nLeng = GlobVar.TheMCU.serialPort1.BytesToRead;
if ((GlobVar.nLeng > 0) && (GlobVar.nLeng < 30000))
                {
                    for (int s = 0; s < GlobVar.nLeng; s++)
                    {
                        nLength = GlobVar.TheMCU.serialPort1.Read(bLocalBuf, 0, 1);
                        if (nLength == 1)
                        {
                            if (bLocalBuf[0] == 0xc9 && GlobVar.nSOMFlag == 0)
                            {
                                GlobVar.nRindex = 0;
                                GlobVar.RxBufferData[GlobVar.nRindex++] = bLocalBuf[0];
                                GlobVar.nSOMFlag = 1;
                            }
                            else if (bLocalBuf[0] == 0xcf && GlobVar.nSOMFlag == 1)
                            {
                                Rx_Checksum();       // To compare the recv data Checksum  //

                                GlobVar.RxBufferData[GlobVar.nRindex++] = bLocalBuf[0];

                                if ((GlobVar.RxBufferData[2] == 0x01)) Rx_LinkCheck();


}