为什么访问com口否认? [英] why is access to com port denied?

查看:193
本文介绍了为什么访问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.

真正的解决方法很简单,只需将while循环前的open()调用。

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

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

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