From 1db8219ef4dc22a7cdda24a7616dcdcb7bc871c0 Mon Sep 17 00:00:00 2001 From: Threetwosevensixseven Date: Thu, 23 Jan 2020 20:52:48 -0500 Subject: [PATCH] Serial PortName now comes from the UARTReplacement.dll.config files in the CSpect directory. --- UARTReplacement/SerialPort.cs | 17 ++++- UARTReplacement/Settings.cs | 82 ++++++++++++++++++++++ UARTReplacement/UARTReplacement.csproj | 6 ++ UARTReplacement/UARTReplacement.dll.config | 4 ++ UARTReplacement/UARTReplacement_Device.cs | 5 +- UARTReplacement/UARTTargets.cs | 3 + 6 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 UARTReplacement/Settings.cs create mode 100644 UARTReplacement/UARTReplacement.dll.config diff --git a/UARTReplacement/SerialPort.cs b/UARTReplacement/SerialPort.cs index 55ee84d..6aa341f 100644 --- a/UARTReplacement/SerialPort.cs +++ b/UARTReplacement/SerialPort.cs @@ -7,13 +7,21 @@ namespace Plugins.UARTReplacement { + /// + /// This class encapsulates a .NET serial port, together with the logic to change the baud + /// according to instructions received from ythe Next UART I/O ports. + /// public class SerialPort : IDisposable { private System.IO.Ports.SerialPort port; private int clock = 27000000; // CSpect defaults to HDMI timings private int prescaler = 234; // Next baud defaults to 115200 (more accurately 115384 with integer division) - public SerialPort(string PortName, int BaudRate) + /// + /// Creates an instance of the SerialPort class. + /// + /// The serial port name to bind to, for example COM1. + public SerialPort(string PortName) { try { @@ -35,6 +43,13 @@ public SerialPort(string PortName, int BaudRate) } } + /// + /// Writes a specified number of bytes to the serial port using data from a buffer. + /// + /// The byte array that contains the data to write to the port. + /// The zero-based byte offset in the buffer parameter at which to begin + /// copying bytes to the port. + /// The number of bytes to write. public void Write(byte[] buffer, int offset, int count) { if (port != null) diff --git a/UARTReplacement/Settings.cs b/UARTReplacement/Settings.cs new file mode 100644 index 0000000..c828e1d --- /dev/null +++ b/UARTReplacement/Settings.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Serialization; + +namespace Plugins.UARTReplacement +{ + public class Settings + { + public string PortName { get; set; } + + public Settings() + { + PortName = "COM1"; + } + + public string ToXML() + { + string output = ""; + var xmlSerializer = new XmlSerializer(GetType()); + using (var memoryStream = new MemoryStream()) + using (var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8)) + { + xmlTextWriter.Formatting = Formatting.Indented; + xmlSerializer.Serialize(xmlTextWriter, this); + output = Encoding.UTF8.GetString(memoryStream.ToArray()); + string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); + if (output.StartsWith(_byteOrderMarkUtf8)) + output = output.Remove(0, _byteOrderMarkUtf8.Length); + } + return output; + } + + public static Settings Load(string OptionalFileNameAndPath = null) + { + Settings settings; + try + { + string fn = string.IsNullOrWhiteSpace(OptionalFileNameAndPath) ? GetFileName() : OptionalFileNameAndPath; + string xml = File.ReadAllText(fn); + var reader = new StringReader(xml); + using (reader) + { + var serializer = new XmlSerializer(typeof(Settings)); + settings = (Settings)serializer.Deserialize(reader); + reader.Close(); + } + } + catch (Exception /*ex*/) + { + settings = new Settings(); + } + return settings; + } + + public bool Save(string OptionalFileNameAndPath = null) + { + try + { + string fn = string.IsNullOrWhiteSpace(OptionalFileNameAndPath) ? GetFileName() : OptionalFileNameAndPath; + File.WriteAllText(fn, ToXML()); + return true; + } + catch (Exception ex) + { + return false; + } + } + + public static string GetFileName() + { + string path = AppDomain.CurrentDomain.BaseDirectory; + var dll = Assembly.GetAssembly(typeof(Settings)).ManifestModule.ScopeName + ".config"; + return Path.Combine(path, dll); + } + } +} diff --git a/UARTReplacement/UARTReplacement.csproj b/UARTReplacement/UARTReplacement.csproj index c7d8264..5e52289 100644 --- a/UARTReplacement/UARTReplacement.csproj +++ b/UARTReplacement/UARTReplacement.csproj @@ -48,9 +48,15 @@ + + + + PreserveNewest + +