日期:2014-05-17  浏览次数:21437 次

Close串口后再重新Open串口,报出“对端口的访问被拒绝”,这显然是之前关串口没有关掉,重复打开了,为什么会这样?
在软件中,在串口的Data_Received事件中若发生了异常,将相应操作杆线程关闭,关掉串口,再重新打开串口,再重新打开。在打开串口的时候会报异常“对端口的访问被拒绝”。这显然是之前关串口没有关掉,重复打开了,为什么会这样?

  private void CloseJoystickerRightport()
        {
            try
            {
                while (JoystickerLexium23dPort.IsOpen)
                {
                    joyStickRightPortKeepReading = false;
                    JoystickerLexium23dPort.DataReceived -= new SerialDataReceivedEventHandler(JoystickerRightport_DataReceived);
                    JoystickerLexium23dPort.DiscardOutBuffer();
                    JoystickerLexium23dPort.DiscardInBuffer();
                    JoystickerLexium23dPort.Close();  
                }
            }
            catch (System.Exception ex)
            {
                writeExceptionToLogFile("CloseXYZJoystickRightport", ex.Message);
            }
        }
private void OpenJoystickRightPot()
{
            try
            {
                //if (JoystickerLexium23dPort.IsOpen)
                //{
                while (JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Close();
                if (!JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Open();
                //}
                //else
                //{
                //    JoystickerLexium23dPort.Open();
                //}