the code:
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();
}
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…