You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.3 KiB
135 lines
4.3 KiB
using RJCP.IO.Ports;
|
|
using System.Xml.Linq;
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace SerialTestApp;
|
|
|
|
internal class Program
|
|
{
|
|
private static bool _continue;
|
|
private static Thread readThread;
|
|
private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
|
|
|
|
|
|
public static bool applicationQuitRequested { get; private set; }
|
|
public static SerialPortStream serialPort { get; private set; } = new();
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
while (applicationQuitRequested == false)
|
|
{
|
|
Console.WriteLine($"----- SERIAL PROTOCOL TEST APP -----");
|
|
Console.WriteLine($"Command list:");
|
|
Console.WriteLine($"l = list ports");
|
|
Console.WriteLine($"c = connect");
|
|
Console.WriteLine($"q = quit");
|
|
|
|
var key = Console.ReadKey();
|
|
|
|
switch (key.Key)
|
|
{
|
|
// list com ports
|
|
case ConsoleKey.L:
|
|
string[] ports = SerialPortStream.GetPortNames();
|
|
Console.WriteLine($"Following ports were found: {Environment.NewLine}");
|
|
foreach (var port in ports)
|
|
{
|
|
Console.WriteLine($"-- {port}");
|
|
}
|
|
break;
|
|
|
|
// connect
|
|
case ConsoleKey.C:
|
|
Console.Write($"Write Port name: ");
|
|
string? portName = Console.ReadLine();
|
|
|
|
if (portName == null ||
|
|
portName == "" ||
|
|
!(portName.ToLower()).StartsWith("com"))
|
|
{
|
|
Console.WriteLine($"Invalid Port name entered.");
|
|
break;
|
|
}
|
|
|
|
serialPort.PortName = portName;
|
|
serialPort.BaudRate = 115200;
|
|
serialPort.Parity = Parity.None;
|
|
serialPort.DataBits = 8;
|
|
serialPort.StopBits = StopBits.One;
|
|
serialPort.Handshake = Handshake.None;
|
|
//serialPort.NewLine = "\r\n";
|
|
|
|
serialPort.ReadTimeout = 500;
|
|
serialPort.WriteTimeout = 500;
|
|
|
|
serialPort.Open();
|
|
_continue = true;
|
|
readThread = new Thread(Read);
|
|
readThread.Start();
|
|
|
|
Console.WriteLine("Type QUIT to exit");
|
|
|
|
while (_continue)
|
|
{
|
|
var message = Console.ReadLine();
|
|
|
|
if (stringComparer.Equals("quit", message))
|
|
{
|
|
_continue = false;
|
|
}
|
|
else
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(message!);
|
|
foreach (byte b in bytes)
|
|
{
|
|
serialPort.WriteByte(b);
|
|
}
|
|
serialPort.WriteByte((byte)'\n');
|
|
//serialPort.WriteLine(message);
|
|
}
|
|
}
|
|
|
|
readThread.Join();
|
|
readThread = null;
|
|
|
|
serialPort.Close();
|
|
|
|
break;
|
|
|
|
// quit
|
|
case ConsoleKey.Q:
|
|
applicationQuitRequested = true;
|
|
break;
|
|
|
|
default:
|
|
Console.WriteLine($"{Environment.NewLine}Did not recognize key.. {Environment.NewLine}");
|
|
break;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public static void Read()
|
|
{
|
|
while (_continue)
|
|
{
|
|
try
|
|
{
|
|
int readByte = serialPort.ReadByte();
|
|
if(readByte != -1) // -1 = timeout
|
|
{
|
|
string print = Encoding.UTF8.GetString(new byte[] { (byte)readByte });
|
|
//print = print.Replace("\r", "").Replace("\n", ""); // Newline Mode: none
|
|
print = print.Replace("\r", ""); // Newline Mode: only print \n
|
|
//print = print.Replace("\n", ""); // Newline Mode: only print \r
|
|
Console.Write(print);
|
|
}
|
|
}
|
|
catch (TimeoutException) { }
|
|
}
|
|
}
|
|
} |