Add custom string linefeed option

This commit is contained in:
salvathor79 2024-11-16 16:42:01 +01:00
parent 3ff8fab9a2
commit 7b2d9a58c0
2 changed files with 65 additions and 14 deletions

View File

@ -88,7 +88,7 @@ If you have multiple COM ports, multiple CircuitPython devices connected, or nee
`-ec --echo` enable or disable printing typed characters `-ec --echo` enable or disable printing typed characters
`-tx --tx_newline` newline char sent on carriage return `-tx --tx_newline` newline chars sent on carriage return (ex. `-tx:CRLF`, `-tx:custom=CustomString`, `--tx_newline:LF`)
`-i, --input` ut configuration file, with newline separated configuration options. eg: `c:COM1`. Note that the prefix `-` and `--` shall be omitted. `-i, --input` ut configuration file, with newline separated configuration options. eg: `c:COM1`. Note that the prefix `-` and `--` shall be omitted.

View File

@ -5,6 +5,7 @@ using System.IO;
using System.IO.Ports; using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Management; using System.Management;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -57,7 +58,17 @@ namespace SimplySerial
static bool clearScreen = true; static bool clearScreen = true;
static bool noStatus = false; static bool noStatus = false;
static bool localEcho = false; static bool localEcho = false;
static string NewlineTxMode = "CR"; static string CustomString = string.Empty;
static Dictionary<Int16, String> NewlineTxDict = new Dictionary<Int16, String>
{
{0,"" },
{1,"\r"},
{2,"\n"},
{3,"\r\n"}
};
static Int16 NewlineTxMode = 1; // Default value is \r
// dictionary of "special" keys with the corresponding string to send out when they are pressed // dictionary of "special" keys with the corresponding string to send out when they are pressed
static Dictionary<ConsoleKey, String> specialKeys = new Dictionary<ConsoleKey, String> static Dictionary<ConsoleKey, String> specialKeys = new Dictionary<ConsoleKey, String>
@ -270,6 +281,38 @@ namespace SimplySerial
{ {
Output(""); Output("");
} }
if (NewlineTxMode == 1)
{
CustomString = "CR";
}
else if (NewlineTxMode == 2)
{
CustomString = "LF";
}
else if (NewlineTxMode == 3)
{
CustomString = "CRLF";
}
else if (NewlineTxMode == 4)
{
if (NewlineTxDict.TryGetValue(NewlineTxMode, out string t))
{
CustomString = t;
}
else
{
CustomString = "";
}
}
else
{
/* we should never reach this point */
}
Output(String.Format("<<< SimplySerial v{0} connected via {1} >>>\n" + Output(String.Format("<<< SimplySerial v{0} connected via {1} >>>\n" +
"Settings : {2} baud, {3} parity, {4} data bits, {5} stop bit{6}, {7} encoding, auto-connect {8}, echo {9}, tx_linefeed:{10}\n" + "Settings : {2} baud, {3} parity, {4} data bits, {5} stop bit{6}, {7} encoding, auto-connect {8}, echo {9}, tx_linefeed:{10}\n" +
"Device : {11} {12}{13}\n{14}" + "Device : {11} {12}{13}\n{14}" +
@ -283,7 +326,7 @@ namespace SimplySerial
(encoding.ToString() == "System.Text.UTF8Encoding") ? "UTF-8" : (convertToPrintable) ? "RAW" : "ASCII", (encoding.ToString() == "System.Text.UTF8Encoding") ? "UTF-8" : (convertToPrintable) ? "RAW" : "ASCII",
(autoConnect == AutoConnect.ONE) ? "on" : (autoConnect == AutoConnect.ANY) ? "any" : "off", (autoConnect == AutoConnect.ONE) ? "on" : (autoConnect == AutoConnect.ANY) ? "any" : "off",
(localEcho == true) ? "on" : "off", (localEcho == true) ? "on" : "off",
NewlineTxMode, CustomString,
port.board.make, port.board.make,
port.board.model, port.board.model,
(port.isCircuitPython) ? " (CircuitPython-capable)" : "", (port.isCircuitPython) ? " (CircuitPython-capable)" : "",
@ -325,14 +368,17 @@ namespace SimplySerial
// everything else just gets sent right on through // everything else just gets sent right on through
else else
{ {
//String buffer;
string outString = Convert.ToString(keyInfo.KeyChar); string outString = Convert.ToString(keyInfo.KeyChar);
if (NewlineTxMode.Equals("LF")) if (NewlineTxDict.TryGetValue(NewlineTxMode, out String buffer))
outString = outString.Replace("\r", "\n"); {
else if (NewlineTxMode.Equals("CR")) outString = outString.Replace("\r", buffer);
outString = outString.Replace("\n", "\r"); }
else if (NewlineTxMode.Equals("CRLF")) else
outString = outString.Replace("\r", "\r\n"); {
// Do nothing
}
serialPort.Write(outString); serialPort.Write(outString);
@ -685,13 +731,18 @@ namespace SimplySerial
argument[1] = argument[1].ToLower(); argument[1] = argument[1].ToLower();
if (argument[1].Equals("cr")) if (argument[1].Equals("cr"))
NewlineTxMode = "CR"; NewlineTxMode = 1;
else if (argument[1].Equals("lf")) else if (argument[1].Equals("lf"))
NewlineTxMode = "LF"; NewlineTxMode = 2;
else if (argument[1].Equals("crlf")) else if (argument[1].Equals("crlf"))
NewlineTxMode = "CRLF"; NewlineTxMode = 3;
else if ((argument[1].StartsWith("custom=")) && (argument[1].Length <= (255 + 7)))
{
NewlineTxDict.Add(4, argument[1].Substring(argument[1].IndexOf("=") + 1));
NewlineTxMode = 4;
}
else else
ExitProgram(("Invalid newline mode specified (CR | LF | CRLF)<" + argument[1] + ">"), exitCode: -1); ExitProgram(("Invalid newline mode specified (CR | LF | CRLF | CUSTOM=CustomString)\r\n.CustomString should be less than 255 chars.<" + argument[1] + ">"), exitCode: -1);
} }
// an invalid/incomplete argument was passed // an invalid/incomplete argument was passed
else else