为什么访问com端口被拒绝? [英] why is access to com port denied?

查看:178
本文介绍了为什么访问com端口被拒绝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

static void Main(string[] args)
{
    Console.WriteLine("Memory mapped file reader started");

    using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
    {
        using (var readerz = file.CreateViewAccessor(0, 0))
        {
            var bytes = new byte[567];
            var encoding = Encoding.ASCII;
            readerz.ReadArray<byte>(0, bytes, 0, bytes.Length);

            File.WriteAllText("C:\myFile.txt", encoding.GetString(bytes));

            var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
            using (var reader = XmlReader.Create("C:\myFile.txt", readerSettings))
            {
                while (reader.Read())
                {
                    using (var fragmentReader = reader.ReadSubtree())
                    {
                        if (fragmentReader.Read())
                        {

                            reader.ReadToFollowing("value");
                            SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
                            port.Open();
                            port.Write(reader.ReadElementContentAsString() + ",");
                        }
                    }
                }
            }    
        }
    }

    Console.WriteLine("Press any key to exit ...");
    Console.ReadLine();
}

它读取共享内存,将该共享内存写入文件,然后使用 xml 阅读器打开同一个文件并拆分 xml,因为它有多个根,然后在每个新拆分 xml 上获取节点的值并通过串行发送.它在第一个拆分 xml 上工作,它的节点通过串行发送,然后它停止并在尝试将第二个节点写入串行时拒绝访问 com 端口消息.

it reads shared memory, writes that shared memory to file, then the same file is opened with xml reader and splits xml since it has multiple roots, then gets the value of a node on each new split xml and sends over serial. it works on the first split xml and its node is sent over serial then it stops with a access is denied to com port message on attempt for writing second node to serial.

我有另一个使用相同序列码制作的应用程序,它运行良好(我只是累了它然后关闭它.)......所以它很奇怪.

i have another app i made with same serial code and it works fine(i just tired it then closed it.)... so its strange.

推荐答案

一个串口只能打开一次.但是您的代码在 while 循环中有 Open() 调用.这仅适用于第一次通过循环,第二次通过 kaboom.@cdhowie 的解决方案也不起作用,SerialPort 有一个文档警告的怪癖(又名错误).在 Dispose() 或 Close() 调用之后让工作线程退出需要时间.时间量未指定且不可预测.

You can open a serial port only once. But your code has the Open() call inside the while loop. That will only work for the first pass through the loop, kaboom on the 2nd pass. @cdhowie's solution doesn't work either, SerialPort has a quirk (aka bug) that the documentation warns about. It needs time to let a worker thread exit after the Dispose() or Close() call. The amount of time is unspecified and unpredictable.

真正的解决方案很简单,只需将 Open() 调用移到 while 循环之前.

The real solution is simple, just move the Open() call before the while loop.

这篇关于为什么访问com端口被拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆