首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Close串口后又重新Open串口,报出“对端口的访问被拒绝”,这显然是之前关串口没有关掉,重复打开了,为什么会这样

2013-07-04 
Close串口后再重新Open串口,报出“对端口的访问被拒绝”,这显然是之前关串口没有关掉,重复打开了,为什么会这

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();
                //}
                JoystickerLexium23dPort.DiscardOutBuffer();
                JoystickerLexium23dPort.DiscardInBuffer();
                JoystickerLexium23dPort.WriteTimeout = 100;


                JoystickerLexium23dPort.ReadTimeout = 100;  //**操作杆一发送命令,即使没有动操作杆,也会返回值  //MJ
                JoystickerLexium23dPort.DataReceived += new SerialDataReceivedEventHandler(JoystickerRightport_DataReceived);
}
}
catch(Exception ex)
{
   writeExceptionToLogFile("OpenXYZJoystickRightport", ex.Message);
}

串口通信
[解决办法]
private void OpenJoystickRightPot()
{
            try
            {
                //if (JoystickerLexium23dPort.IsOpen)
                //{
                while (JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Close();
                if (!JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Open();
                //}
                //else
                //{
                //    JoystickerLexium23dPort.Open();
                //}
                JoystickerLexium23dPort.DiscardOutBuffer();
                JoystickerLexium23dPort.DiscardInBuffer();
                JoystickerLexium23dPort.WriteTimeout = 100;
                JoystickerLexium23dPort.ReadTimeout = 100;  //**操作杆一发送命令,即使没有动操作杆,也会返回值  //MJ
                JoystickerLexium23dPort.DataReceived += new SerialDataReceivedEventHandler(JoystickerRightport_DataReceived);
}
}
catch(Exception ex)
{
   writeExceptionToLogFile("OpenXYZJoystickRightport", ex.Message);
}

这里异常里关掉串口。。。
[解决办法]
异常处理中也应该做关闭操作
[解决办法]
为什么LZ老是遇到奇怪的问题。。。断点调试找问题啊

我用这两句测试了下,怎么没遇到问题的。。。。
while (JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Close();
if (!JoystickerLexium23dPort.IsOpen) JoystickerLexium23dPort.Open();



怀疑LZ的端口8被其他程序占用,或者在本程序中别的SerialPort控件占用,但这个控件不叫JoystickerLexium23dPort。检查下程序里有没有别的串口控件
[解决办法]
看看,你的代码是不是放在线程了执行了,线程没有关闭,也会这样的。

热点排行