diff --git a/README.md b/README.md index 62ab7eb..9cef478 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ nextregs. This is required for ESP [boot mode selection](https://docs.espressif.com/projects/esptool/en/latest/esp8266/advanced-topics/boot-mode-selection.html), and for a proposed NextPi GPIO protocol to control the SUPervisor and applications when the UART is otherwise engaged. +**If you're using this plugin, _do not_ start CSpect with the `-com` or `-com2` arguments.** + ### RTCSys A simple date/time plugin which works in tandem with a custom `RTC.SYS` driver to provide date/time on the NextZXOS main menu, and to the `M_GETDATE` and `IDE_RTC API` calls. See its [wiki page](https://github.com/Threetwosevensixseven/CSpectPlugins/wiki/RTCSys) for installation details. @@ -46,7 +48,7 @@ CSpect is a ZXSpectrum emulator by Mike Dailly. Download the latest version [here](http://www.cspect.org/). These plugins only work with v2.19.4.4 or newer. ## Copyright and Licence -All plugins except i2C_Sample are copyright © 2019-2023 Robin Verhagen-Guest, and are licensed under [Apache 2.0](https://github.com/Threetwosevensixseven/CSpectPlugins/blob/master/LICENSE). +All plugins except i2C_Sample are copyright © 2019-2024 Robin Verhagen-Guest, and are licensed under [Apache 2.0](https://github.com/Threetwosevensixseven/CSpectPlugins/blob/master/LICENSE). CSpect and the i2C_Sample example project are copyright © 1998-2023 Mike Dailly All rights reserved. diff --git a/RTCSys/AssemblyExtensions.cs b/RTCSys/AssemblyExtensions.cs new file mode 100644 index 0000000..a98b2c9 --- /dev/null +++ b/RTCSys/AssemblyExtensions.cs @@ -0,0 +1,19 @@ +using System.Reflection; + +namespace Plugins.RTCSys +{ + public static class AssemblyExtensions + { + public static string GetAssemblyConfiguration(this Assembly assembly) + { + object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false); + + AssemblyConfigurationAttribute attribute = null; + if (attributes.Length > 0) + { + attribute = attributes[0] as AssemblyConfigurationAttribute; + } + return (attribute?.Configuration ?? ""); + } + } +} diff --git a/RTCSys/Properties/AssemblyInfo.cs b/RTCSys/Properties/AssemblyInfo.cs index e388a5c..d823b8b 100644 --- a/RTCSys/Properties/AssemblyInfo.cs +++ b/RTCSys/Properties/AssemblyInfo.cs @@ -7,10 +7,8 @@ // associated with an assembly. [assembly: AssemblyTitle("RTCSys")] [assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("RTCSys")] -[assembly: AssemblyCopyright("Copyright © 2020")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -21,16 +19,3 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("df5ac599-1ee2-49ab-94e5-f44a842db4f7")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RTCSys/Properties/AssemblyInfoExtra.cs b/RTCSys/Properties/AssemblyInfoExtra.cs new file mode 100644 index 0000000..cb29ab6 --- /dev/null +++ b/RTCSys/Properties/AssemblyInfoExtra.cs @@ -0,0 +1,10 @@ +// AssemblyInfoExtra.cs +// +// Auto-generated by ZXVersion.exe + +using System.Reflection; + +[assembly: AssemblyConfiguration("04697a9")] +[assembly: AssemblyCopyright("Copyright © 2019-2024 Robin Verhagen-Guest")] +[assembly: AssemblyVersion("1.6.0.0")] +[assembly: AssemblyFileVersion("1.6.0.0")] diff --git a/RTCSys/RTCStates.cs b/RTCSys/RTCStates.cs index 011b582..b6a6c8c 100644 --- a/RTCSys/RTCStates.cs +++ b/RTCSys/RTCStates.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace RTCSys +namespace Plugins.RTCSys { public enum RTCStates { diff --git a/RTCSys/RTCSys.csproj b/RTCSys/RTCSys.csproj index f353cdc..f4869c6 100644 --- a/RTCSys/RTCSys.csproj +++ b/RTCSys/RTCSys.csproj @@ -7,7 +7,7 @@ {DF5AC599-1EE2-49AB-94E5-F44A842DB4F7} Library Properties - RTCSys + Plugins.RTCSys RTCSys v4.5.2 512 @@ -17,7 +17,7 @@ true full false - bin\Debug\ + ..\..\..\..\..\..\..\spec\CSpect2_19_4_4\ DEBUG;TRACE prompt 4 @@ -46,11 +46,17 @@ + + + + cd "$(ProjectDir)" +"$(ProjectDir)ZXVersion.exe" + + + + + + \ No newline at end of file diff --git a/UARTLogger/AssemblyExtensions.cs b/UARTLogger/AssemblyExtensions.cs new file mode 100644 index 0000000..263128e --- /dev/null +++ b/UARTLogger/AssemblyExtensions.cs @@ -0,0 +1,19 @@ +using System.Reflection; + +namespace Plugins.UARTLogger +{ + public static class AssemblyExtensions + { + public static string GetAssemblyConfiguration(this Assembly assembly) + { + object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false); + + AssemblyConfigurationAttribute attribute = null; + if (attributes.Length > 0) + { + attribute = attributes[0] as AssemblyConfigurationAttribute; + } + return (attribute?.Configuration ?? ""); + } + } +} diff --git a/UARTLogger/FileLogger.cs b/UARTLogger/FileLogger.cs index 00620e6..80899d5 100644 --- a/UARTLogger/FileLogger.cs +++ b/UARTLogger/FileLogger.cs @@ -14,21 +14,45 @@ public class FileLogger : IDisposable public FileLogger(Settings Settings, UARTTargets Target) { - if (Target == UARTTargets.ESP && (Settings == null || !Settings.EnableESPLogging)) - return; - if (Target == UARTTargets.Pi && (Settings == null || !Settings.EnablePiLogging)) - return; - string fn = Target == UARTTargets.ESP ? Settings.ESPLogFile : Settings.PiLogFile; - var mode = Settings.TruncateLogsOnStartup ? FileMode.Create : FileMode.Append; - fileStream = File.Open(fn, mode, FileAccess.Write, FileShare.ReadWrite); - fileWriter = new StreamWriter(fileStream); - fileWriter.AutoFlush = true; + try + { + if (Target == UARTTargets.ESP && (Settings == null || !Settings.EnableESPLogging)) + { + Console.WriteLine(UARTLogger_Device.PluginName + "Disabling " + Target.ToString() + " logging."); + return; + } + if (Target == UARTTargets.Pi && (Settings == null || !Settings.EnablePiLogging)) + { + Console.WriteLine(UARTLogger_Device.PluginName + "Disabling " + Target.ToString() + " logging."); + return; + } + string fn = Target == UARTTargets.ESP ? Settings.ESPLogFile : Settings.PiLogFile; + var mode = Settings.TruncateLogsOnStartup ? FileMode.Create : FileMode.Append; + fileStream = File.Open(fn, mode, FileAccess.Write, FileShare.ReadWrite); + fileWriter = new StreamWriter(fileStream); + fileWriter.AutoFlush = true; + Console.WriteLine(UARTLogger_Device.PluginName + "Logging " + Target.ToString() + " traffic to " + fn + "."); + + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } public void Write(String Value) { - if (fileWriter != null && Value != null) - fileWriter.Write(Value); + try + { + if (fileWriter != null && Value != null) + fileWriter.Write(Value); + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } #region IDisposable Support @@ -36,27 +60,35 @@ public void Write(String Value) protected virtual void Dispose(bool disposing) { - if (!disposedValue) + try { - if (disposing) + if (!disposedValue) { - // Dispose managed state (managed objects). - if (fileWriter != null) - { - fileWriter.Dispose(); - fileWriter = null; - } - if (fileStream != null) + if (disposing) { - fileStream.Dispose(); - fileStream = null; + // Dispose managed state (managed objects). + if (fileWriter != null) + { + fileWriter.Dispose(); + fileWriter = null; + } + if (fileStream != null) + { + fileStream.Dispose(); + fileStream = null; + } } - } - // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. - // TODO: set large fields to null. + // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. + // TODO: set large fields to null. - disposedValue = true; + disposedValue = true; + } + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -69,10 +101,18 @@ protected virtual void Dispose(bool disposing) // This code added to correctly implement the disposable pattern. public void Dispose() { + try + { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); - // TODO: uncomment the following line if the finalizer is overridden above. - // GC.SuppressFinalize(this); + // TODO: uncomment the following line if the finalizer is overridden above. + // GC.SuppressFinalize(this); + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } #endregion } diff --git a/UARTLogger/Properties/AssemblyInfo.cs b/UARTLogger/Properties/AssemblyInfo.cs index 28821bc..85dfd1f 100644 --- a/UARTLogger/Properties/AssemblyInfo.cs +++ b/UARTLogger/Properties/AssemblyInfo.cs @@ -7,10 +7,8 @@ // associated with an assembly. [assembly: AssemblyTitle("UARTLogger")] [assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("UARTLogger")] -[assembly: AssemblyCopyright("Copyright © 2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -21,16 +19,3 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("c313face-83b3-4690-b3f4-c4e8254cec3d")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UARTLogger/Properties/AssemblyInfoExtra.cs b/UARTLogger/Properties/AssemblyInfoExtra.cs new file mode 100644 index 0000000..cb29ab6 --- /dev/null +++ b/UARTLogger/Properties/AssemblyInfoExtra.cs @@ -0,0 +1,10 @@ +// AssemblyInfoExtra.cs +// +// Auto-generated by ZXVersion.exe + +using System.Reflection; + +[assembly: AssemblyConfiguration("04697a9")] +[assembly: AssemblyCopyright("Copyright © 2019-2024 Robin Verhagen-Guest")] +[assembly: AssemblyVersion("1.6.0.0")] +[assembly: AssemblyFileVersion("1.6.0.0")] diff --git a/UARTLogger/Settings.cs b/UARTLogger/Settings.cs index f1fc011..352b3c2 100644 --- a/UARTLogger/Settings.cs +++ b/UARTLogger/Settings.cs @@ -58,7 +58,7 @@ public static Settings Load(string OptionalFileNameAndPath = null) reader.Close(); } } - catch (Exception /*ex*/) + catch (Exception) { settings = new Settings(); } @@ -75,7 +75,7 @@ public bool Save(string OptionalFileNameAndPath = null) File.WriteAllText(fn, ToXML()); return true; } - catch (Exception /*ex*/) + catch (Exception) { return false; } diff --git a/UARTLogger/UARTBuffer.cs b/UARTLogger/UARTBuffer.cs index 9b68a1d..f4538d6 100644 --- a/UARTLogger/UARTBuffer.cs +++ b/UARTLogger/UARTBuffer.cs @@ -21,113 +21,153 @@ public class UARTBuffer : IDisposable public UARTBuffer(Settings Settings) { - settings = Settings; - buffer = new Queue(); - state = UARTStates.Reading; - target = UARTTargets.ESP; - sync = new object(); - timer = new Timer(timerElapsed, this, settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); - espLogger = new FileLogger(Settings, UARTTargets.ESP); - piLogger = new FileLogger(Settings, UARTTargets.Pi); + try + { + settings = Settings; + buffer = new Queue(); + state = UARTStates.Reading; + target = UARTTargets.ESP; + sync = new object(); + timer = new Timer(timerElapsed, this, settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); + espLogger = new FileLogger(Settings, UARTTargets.ESP); + piLogger = new FileLogger(Settings, UARTTargets.Pi); + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } public void Log(byte Value, UARTStates NewState) { - timer.Change(settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); - if (state != NewState) + try + { + timer.Change(settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); + if (state != NewState) + { + Flush(); + state = NewState; + } + buffer.Enqueue(Value); + } + catch (Exception ex) { - Flush(); - state = NewState; + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } - buffer.Enqueue(Value); } public void ChangeUARTType(UARTTargets NewTarget) { - timer.Change(settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); - if (NewTarget != target) + try + { + timer.Change(settings.FlushLogsAfterSecs * 1000, Timeout.Infinite); + if (NewTarget != target) + { + Flush(); + target = NewTarget; + } + } + catch (Exception ex) { - Flush(); - target = NewTarget; + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } private void Flush() { - lock (sync) + try { - if (!settings.EnableESPLogging && target == UARTTargets.ESP) - { - buffer = new Queue(); - return; - } - else if (!settings.EnablePiLogging && target == UARTTargets.Pi) + lock (sync) { - buffer = new Queue(); - return; - } + if (!settings.EnableESPLogging && target == UARTTargets.ESP) + { + buffer = new Queue(); + return; + } + else if (!settings.EnablePiLogging && target == UARTTargets.Pi) + { + buffer = new Queue(); + return; + } - if (buffer.Count > 0) - { - // Write header - string action = state == UARTStates.Reading ? "Read " : "Written "; - string prep = state == UARTStates.Reading ? "from " : "to "; - string plural = buffer.Count == 1 ? "" : "s"; - var now = DateTime.Now; - var sb = new StringBuilder(); - sb.AppendLine(); - sb.Append("["); - sb.Append(now.ToShortDateString()); - sb.Append(" "); - sb.Append(now.ToLongTimeString()); - sb.Append("] "); - sb.Append(action); - sb.Append(buffer.Count); - sb.Append(" byte"); - sb.Append(plural); - sb.Append(" "); - sb.Append(prep); - sb.Append(target.ToString()); - sb.AppendLine(":"); - - // Write data rows - int rowCount = 0; - string hex = ""; - string asc = ""; - while (buffer.Count > 0) + if (buffer.Count > 0) { - if (rowCount == 0) - { - hex = " "; - asc = ""; - } - byte b = buffer.Dequeue(); - hex += b.ToString("x2") + " "; - asc += SpectrumCharset.ToASCII(b); - rowCount++; - if (rowCount >= 16 || buffer.Count == 0) + // Write header + string action = state == UARTStates.Reading ? "Read " : "Written "; + string prep = state == UARTStates.Reading ? "from " : "to "; + string plural = buffer.Count == 1 ? "" : "s"; + var now = DateTime.Now; + var sb = new StringBuilder(); + sb.AppendLine(); + sb.Append("["); + sb.Append(now.ToShortDateString()); + sb.Append(" "); + sb.Append(now.ToLongTimeString()); + sb.Append("] "); + sb.Append(action); + sb.Append(buffer.Count); + sb.Append(" byte"); + sb.Append(plural); + sb.Append(" "); + sb.Append(prep); + sb.Append(target.ToString()); + sb.AppendLine(":"); + + // Write data rows + int rowCount = 0; + string hex = ""; + string asc = ""; + while (buffer.Count > 0) { - sb.Append(hex.PadRight(54)); - sb.Append(asc); - sb.AppendLine(); - rowCount = 0; + if (rowCount == 0) + { + hex = " "; + asc = ""; + } + byte b = buffer.Dequeue(); + hex += b.ToString("x2") + " "; + asc += SpectrumCharset.ToASCII(b); + rowCount++; + if (rowCount >= 16 || buffer.Count == 0) + { + sb.Append(hex.PadRight(54)); + sb.Append(asc); + sb.AppendLine(); + rowCount = 0; + } } - } - // Log result - var text = sb.ToString(); - if (target == UARTTargets.ESP) - espLogger.Write(text); - else if (target == UARTTargets.Pi) - piLogger.Write(text); + // Log result + var text = sb.ToString(); + if (target == UARTTargets.ESP) + espLogger.Write(text); + else if (target == UARTTargets.Pi) + piLogger.Write(text); + } } } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } private void timerElapsed(object Buffer) { - if (Buffer is UARTBuffer) - ((UARTBuffer)Buffer).Flush(); + try + { + if (Buffer is UARTBuffer) + ((UARTBuffer)Buffer).Flush(); + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } #region IDisposable @@ -136,28 +176,36 @@ private void timerElapsed(object Buffer) protected virtual void Dispose(bool disposing) { - if (!disposedValue) + try { - if (disposing) + if (!disposedValue) { - // Dispose managed state (managed objects). - Flush(); - if (espLogger != null) + if (disposing) { - espLogger.Dispose(); - espLogger = null; - } - if (piLogger != null) - { - piLogger.Dispose(); - piLogger = null; + // Dispose managed state (managed objects). + Flush(); + if (espLogger != null) + { + espLogger.Dispose(); + espLogger = null; + } + if (piLogger != null) + { + piLogger.Dispose(); + piLogger = null; + } } - } - // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. - // TODO: set large fields to null. + // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. + // TODO: set large fields to null. - disposedValue = true; + disposedValue = true; + } + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -170,10 +218,18 @@ protected virtual void Dispose(bool disposing) // This code added to correctly implement the disposable pattern. public void Dispose() { - // Do not change this code. Put cleanup code in Dispose(bool disposing) above. - Dispose(true); - // TODO: uncomment the following line if the finalizer is overridden above. - // GC.SuppressFinalize(this); + try + { + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + Dispose(true); + // TODO: uncomment the following line if the finalizer is overridden above. + // GC.SuppressFinalize(this); + } + catch (Exception ex) + { + Console.Error.Write(UARTLogger_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } } #endregion IDisposable } diff --git a/UARTLogger/UARTLogger.csproj b/UARTLogger/UARTLogger.csproj index 82a8c07..fe4c66b 100644 --- a/UARTLogger/UARTLogger.csproj +++ b/UARTLogger/UARTLogger.csproj @@ -16,7 +16,7 @@ true full false - bin\Debug\ + ..\..\..\..\..\..\..\spec\CSpect2_19_4_4\ DEBUG;TRACE prompt 4 @@ -44,7 +44,9 @@ + + @@ -54,6 +56,10 @@ + + cd "$(ProjectDir)" +"$(ProjectDir)ZXVersion.exe" + + + + + + \ No newline at end of file diff --git a/UARTReplacement/AssemblyExtensions.cs b/UARTReplacement/AssemblyExtensions.cs new file mode 100644 index 0000000..51613b6 --- /dev/null +++ b/UARTReplacement/AssemblyExtensions.cs @@ -0,0 +1,19 @@ +using System.Reflection; + +namespace Plugins.UARTReplacement +{ + public static class AssemblyExtensions + { + public static string GetAssemblyConfiguration(this Assembly assembly) + { + object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false); + + AssemblyConfigurationAttribute attribute = null; + if (attributes.Length > 0) + { + attribute = attributes[0] as AssemblyConfigurationAttribute; + } + return (attribute?.Configuration ?? ""); + } + } +} diff --git a/UARTReplacement/Properties/AssemblyInfo.cs b/UARTReplacement/Properties/AssemblyInfo.cs index 4fcee3b..fd203d0 100644 --- a/UARTReplacement/Properties/AssemblyInfo.cs +++ b/UARTReplacement/Properties/AssemblyInfo.cs @@ -7,10 +7,8 @@ // associated with an assembly. [assembly: AssemblyTitle("UARTReplacement")] [assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("UARTReplacement")] -[assembly: AssemblyCopyright("Copyright © 2020 Robin Verhagen-Guest")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -21,16 +19,3 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("b620589d-3613-40fb-acfb-9c825d474d7f")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UARTReplacement/Properties/AssemblyInfoExtra.cs b/UARTReplacement/Properties/AssemblyInfoExtra.cs new file mode 100644 index 0000000..cb29ab6 --- /dev/null +++ b/UARTReplacement/Properties/AssemblyInfoExtra.cs @@ -0,0 +1,10 @@ +// AssemblyInfoExtra.cs +// +// Auto-generated by ZXVersion.exe + +using System.Reflection; + +[assembly: AssemblyConfiguration("04697a9")] +[assembly: AssemblyCopyright("Copyright © 2019-2024 Robin Verhagen-Guest")] +[assembly: AssemblyVersion("1.6.0.0")] +[assembly: AssemblyFileVersion("1.6.0.0")] diff --git a/UARTReplacement/SerialPort.cs b/UARTReplacement/SerialPort.cs index 4faf9b5..8bf6b17 100644 --- a/UARTReplacement/SerialPort.cs +++ b/UARTReplacement/SerialPort.cs @@ -32,7 +32,15 @@ public SerialPort(string PortName, UARTTargets Target, System.IO.Ports.SerialDat { target = Target; logPrefix = target.ToString().Substring(0, 1) + "." + (PortName ?? "").Trim() + "."; + if (string.IsNullOrWhiteSpace(PortName)) + { + port = null; + Console.Write(UARTReplacement_Device.PluginName); + Console.WriteLine(Target.ToString() + " UART not configured, disabling."); + return; + } port = new System.IO.Ports.SerialPort(); + int oldBaud = -1; port.PortName = PortName; int baud = Baud; port.BaudRate = baud > 0 ? baud : 115200; @@ -40,18 +48,32 @@ public SerialPort(string PortName, UARTTargets Target, System.IO.Ports.SerialDat port.DataBits = 8; port.StopBits = System.IO.Ports.StopBits.One; port.Handshake = System.IO.Ports.Handshake.None; - LogClock(false); - LogPrescaler(""); + LogClock(oldBaud, baud, false); + LogPrescaler(oldBaud, baud, ""); if (dataReceivedHandler != null) port.DataReceived += dataReceivedHandler; port.Open(); } + catch (System.IO.IOException ex) + { + port = null; + if (ex.Message.Contains("does not exist")) + { + Console.Write(UARTReplacement_Device.PluginName); + Console.WriteLine("Port " + (PortName ?? "").Trim() + + " does not exist, disabling " + Target.ToString() + " UART."); + } + else + { + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); + } + } catch (Exception ex) { port = null; - Debug.Write("UARTReplacement_Device.SerialPort Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -71,9 +93,8 @@ public void Write(byte[] buffer, int offset, int count) } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.Write Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -95,9 +116,8 @@ public byte ReadByte(out bool Success) } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.ReadByte Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } Success = false; return 0x00; @@ -112,6 +132,7 @@ public byte ReadByte(out bool Success) /// public int SetPrescalerAndClock(byte BaudByte, byte VideoTimingByte) { + int oldBaud = Baud; try { if (port == null) @@ -156,21 +177,20 @@ public int SetPrescalerAndClock(byte BaudByte, byte VideoTimingByte) // Combine the two sets of bits prescaler = oldBits | newBits; port.BaudRate = Baud; - LogClock(false); - LogPrescaler("16:14"); + LogClock(oldBaud, Baud, false); + LogPrescaler(oldBaud, Baud, "16:14"); } else { port.BaudRate = Baud; - LogClock(); + LogClock(oldBaud, Baud); } return clock; } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.SetPrescalerAndClock Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); return 0; } } @@ -183,6 +203,7 @@ public int SetPrescalerAndClock(byte BaudByte, byte VideoTimingByte) /// Returns the newly recalculated baud. public int SetPrescaler(byte BaudByte) { + int oldBaud = Baud; try { if (port == null) @@ -196,7 +217,7 @@ public int SetPrescaler(byte BaudByte) // Combine the two sets of bits prescaler = oldBits | newBits; port.BaudRate = Baud == 1928571 ? 2000000 : Baud; - LogPrescaler("6:0"); + LogPrescaler(oldBaud, Baud, "6:0"); } else { @@ -207,16 +228,15 @@ public int SetPrescaler(byte BaudByte) // Combine the two sets of bits prescaler = oldBits | newBits; port.BaudRate = Baud == 1928571 ? 2000000 : Baud; - LogPrescaler("13:7"); + LogPrescaler(oldBaud, Baud, "13:7"); return Baud; } return Baud; } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.SetPrescaler Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); return 0; } } @@ -237,9 +257,8 @@ public int Baud } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.Baud Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); return 0; } } @@ -254,20 +273,19 @@ public void EspReset(byte ResetByte) bool resetESP = (ResetByte & 128) == 128; if (resetESP) { - Debug.WriteLine(logPrefix + "RTS: " + resetESP + " (drive /RST low)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "RTS=" + resetESP + " (drive /RST low)"); port.RtsEnable = true; } else { - Debug.WriteLine(logPrefix + "RTS: " + resetESP + " (release /RST high)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "RTS=" + resetESP + " (release /RST high)"); port.RtsEnable = false; } } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.EspReset Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -277,13 +295,12 @@ public void EnableEspGpio(byte EnableByte) { // bit 0 = ESP GPIO0 output enable enableEspGpio = (EnableByte & 1) == 1; - Debug.WriteLine(logPrefix + "EnableEspGpio:" + enableEspGpio); + Console.WriteLine(UARTReplacement_Device.PluginName + "EnableEspGpio=" + enableEspGpio); } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.EnableEspGpio Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -298,20 +315,19 @@ public void SetEspGpio(byte GpioByte) bool gpio0 = !((GpioByte & 1) == 1); if (gpio0) { - Debug.WriteLine(logPrefix + "E.DTR: " + gpio0 + " (drive /GPIO0 low)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "ESP.DTR=" + gpio0 + " (drive /GPIO0 low)"); port.DtrEnable = true; } else { - Debug.WriteLine(logPrefix + "E.DTR: " + gpio0 + " (release /GPIO0 high)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "ESP.DTR=" + gpio0 + " (release /GPIO0 high)"); port.DtrEnable = false; } } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.SetEspGpio Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -323,18 +339,17 @@ public void EnablePiGpio(byte EnableByte) bool old = enablePiGpio4Output; enablePiGpio4Output = (EnableByte & 16) == 16; if (enablePiGpio4Output != old) - Debug.WriteLine(logPrefix + "EnablePiGpio4Output:" + enablePiGpio4Output); + Console.WriteLine(UARTReplacement_Device.PluginName + "EnablePiGpio4Output=" + enablePiGpio4Output); // bit 5 = Pi GPIO5 output enable old = enablePiGpio5Output; enablePiGpio5Output = (EnableByte & 32) == 32; if (enablePiGpio5Output != old) - Debug.WriteLine(logPrefix + "EnablePiGpio5Output:" + enablePiGpio5Output); + Console.WriteLine(UARTReplacement_Device.PluginName + "EnablePiGpio5Output=" + enablePiGpio5Output); } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.EnablePiGpio Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -350,12 +365,12 @@ public void SetPiGpio(byte GpioByte) bool gpio4 = ((GpioByte & 16) == 16); if (gpio4 && port.DtrEnable != gpio4) { - Debug.WriteLine(logPrefix + "DTR: " + gpio4 + " (drive /GPIO4 low)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "Pi.DTR=" + gpio4 + " (drive /GPIO4 low)"); port.DtrEnable = true; } else if (!gpio4 && port.DtrEnable != gpio4) { - Debug.WriteLine(logPrefix + "DTR: " + gpio4 + " (release /GPIO4 high)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "Pi.DTR=" + gpio4 + " (release /GPIO4 high)"); port.DtrEnable = false; } } @@ -365,24 +380,30 @@ public void SetPiGpio(byte GpioByte) bool gpio5 = ((GpioByte & 32) == 32); if (gpio5 && port.RtsEnable != gpio5) { - Debug.WriteLine(logPrefix + "RTS: " + gpio5 + " (drive /GPIO5 low)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "Pi.RTS=" + gpio5 + " (drive /GPIO5 low)"); port.RtsEnable = true; } else if (!gpio5 && port.RtsEnable != gpio5) { - Debug.WriteLine(logPrefix + "RTS: " + gpio5 + " (release /GPIO5 high)"); + Console.WriteLine(UARTReplacement_Device.PluginName + "Pi.RTS=" + gpio5 + " (release /GPIO5 high)"); port.RtsEnable = false; } } } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.SetPiGpio Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } + public bool IsEnabled + { + get + { + return port != null; + } + } /// /// Convenience method to log the clock and calculated baud to the debug console, every time the video timing clock changes. @@ -391,21 +412,20 @@ public void SetPiGpio(byte GpioByte) /// Optionally choose not to log the calculated baud, if you know the prescaler is about to be changed /// and logged straight afterwareds. /// - private void LogClock(bool LogBaud = true) + private void LogClock(int oldBaud, int newBaud, bool LogBaud = true) { try { if (port == null) return; - Debug.WriteLine(logPrefix + "Clock: " + clock); - if (LogBaud) - Debug.WriteLine(logPrefix + "Baud: " + Baud); + //Console.WriteLine(UARTReplacement_Device.PluginName + "Clock=" + clock); + if (LogBaud && newBaud != oldBaud) + Console.WriteLine(UARTReplacement_Device.PluginName + "Setting " + target.ToString() + " baud to " + Baud); } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.SerialPort.LogClock Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } @@ -413,22 +433,22 @@ private void LogClock(bool LogBaud = true) /// Convenience method to log the prescaler and calculated baud to the debug console, every time the prescaler changes. /// /// - private void LogPrescaler(string BitsChanged) + private void LogPrescaler(int oldBaud, int newBaud, string BitsChanged) { try { if (port == null) return; string bstr = Convert.ToString(prescaler, 2).PadLeft(17, '0'); - Debug.WriteLine(logPrefix + "Prescaler: " + bstr.Substring(0, 3) + " " + bstr.Substring(3, 7) + " " + bstr.Substring(10, 7) - + " (" + prescaler + (string.IsNullOrWhiteSpace(BitsChanged) ? "" : ", changed bits " + BitsChanged) + ")"); - Debug.WriteLine(logPrefix + "Baud: " + Baud); + //Console.WriteLine(UARTReplacement_Device.PluginName + "Prescaler=" + bstr.Substring(0, 3) + " " + bstr.Substring(3, 7) + " " + bstr.Substring(10, 7) + // + " (" + prescaler + (string.IsNullOrWhiteSpace(BitsChanged) ? "" : ", changed bits " + BitsChanged) + ")"); + if (newBaud != oldBaud) + Console.WriteLine(UARTReplacement_Device.PluginName + "Setting " + target.ToString() + " baud to " + Baud); } catch (Exception ex) { - Debug.Write("UARTReplacement_Device.LogPrescaler Exception: "); - Debug.Write(ex.Message); - Debug.Write(ex.StackTrace); + Console.Error.Write(UARTReplacement_Device.PluginName); + Console.Error.WriteLine(ex.ToString()); } } diff --git a/UARTReplacement/Settings.cs b/UARTReplacement/Settings.cs index 673a619..2d2fac9 100644 --- a/UARTReplacement/Settings.cs +++ b/UARTReplacement/Settings.cs @@ -61,7 +61,7 @@ public static Settings Load(string OptionalFileNameAndPath = null) reader.Close(); } } - catch (Exception /*ex*/) + catch (Exception) { settings = new Settings(); } @@ -76,7 +76,7 @@ public bool Save(string OptionalFileNameAndPath = null) File.WriteAllText(fn, ToXML()); return true; } - catch (Exception ex) + catch (Exception) { return false; } diff --git a/UARTReplacement/UARTReplacement.csproj b/UARTReplacement/UARTReplacement.csproj index beb3c5c..0d076cb 100644 --- a/UARTReplacement/UARTReplacement.csproj +++ b/UARTReplacement/UARTReplacement.csproj @@ -47,8 +47,10 @@ + + @@ -60,6 +62,10 @@ + + cd "$(ProjectDir)" +"$(ProjectDir)ZXVersion.exe" + + + + + + \ No newline at end of file