Skip to content

Commit

Permalink
Serial PortName now comes from the UARTReplacement.dll.config files i…
Browse files Browse the repository at this point in the history
…n the CSpect directory.
  • Loading branch information
Threetwosevensixseven committed Jan 24, 2020
1 parent 4a42f26 commit 1db8219
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
17 changes: 16 additions & 1 deletion UARTReplacement/SerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@

namespace Plugins.UARTReplacement
{
/// <summary>
/// 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.
/// </summary>
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)
/// <summary>
/// Creates an instance of the SerialPort class.
/// </summary>
/// <param name="PortName">The serial port name to bind to, for example COM1.</param>
public SerialPort(string PortName)
{
try
{
Expand All @@ -35,6 +43,13 @@ public SerialPort(string PortName, int BaudRate)
}
}

/// <summary>
/// Writes a specified number of bytes to the serial port using data from a buffer.
/// </summary>
/// <param name="buffer">The byte array that contains the data to write to the port.</param>
/// <param name="offset">The zero-based byte offset in the buffer parameter at which to begin
/// copying bytes to the port.</param>
/// <param name="count">The number of bytes to write.</param>
public void Write(byte[] buffer, int offset, int count)
{
if (port != null)
Expand Down
82 changes: 82 additions & 0 deletions UARTReplacement/Settings.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
6 changes: 6 additions & 0 deletions UARTReplacement/UARTReplacement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerialPort.cs" />
<Compile Include="Settings.cs" />
<Compile Include="UARTReplacement_Device.cs" />
<Compile Include="UARTTargets.cs" />
</ItemGroup>
<ItemGroup>
<None Include="UARTReplacement.dll.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions UARTReplacement/UARTReplacement.dll.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PortName>COM1</PortName>
</Settings>
5 changes: 3 additions & 2 deletions UARTReplacement/UARTReplacement_Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class UARTReplacement_Device : iPlugin
private const ushort PORT_UART_CONTROL = 0x153b;
private const byte REG_VIDEO_TIMING = 0x11;


private iCSpect CSpect;
private UARTTargets Target;
private SerialPort espPort;
private Settings settings;
private bool UART_RX_Internal;
private bool UART_TX_Internal;
private Thread espThread;
Expand All @@ -33,7 +33,8 @@ public List<sIO> Init(iCSpect _CSpect)
sync = new object();
CSpect = _CSpect;
Target = UARTTargets.ESP;
espPort = new SerialPort("COM5", 115200);
settings = Settings.Load();
espPort = new SerialPort(settings.PortName);
UART_RX_Internal = false;
UART_TX_Internal = false;
threadCancel = false;
Expand Down
3 changes: 3 additions & 0 deletions UARTReplacement/UARTTargets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Plugins.UARTReplacement
{
/// <summary>
/// This represents the two different Next UARTs. Currently only the ESP UART is replaced by this plugin.
/// </summary>
public enum UARTTargets
{
ESP,
Expand Down

0 comments on commit 1db8219

Please sign in to comment.