Aggiungere i file di progetto.
This commit is contained in:
3
OMControl.slnx
Normal file
3
OMControl.slnx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="OMControl/OMControl.csproj" />
|
||||||
|
</Solution>
|
||||||
59
OMControl/Form1.Designer.cs
generated
Normal file
59
OMControl/Form1.Designer.cs
generated
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
namespace OMControl
|
||||||
|
{
|
||||||
|
partial class Form1
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Required designer variable.
|
||||||
|
/// </summary>
|
||||||
|
private System.ComponentModel.IContainer components = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clean up any resources being used.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing && (components != null))
|
||||||
|
{
|
||||||
|
components.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Windows Form Designer generated code
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Required method for Designer support - do not modify
|
||||||
|
/// the contents of this method with the code editor.
|
||||||
|
/// </summary>
|
||||||
|
private void InitializeComponent()
|
||||||
|
{
|
||||||
|
listBoxPorts = new ListBox();
|
||||||
|
SuspendLayout();
|
||||||
|
//
|
||||||
|
// listBoxPorts
|
||||||
|
//
|
||||||
|
listBoxPorts.FormattingEnabled = true;
|
||||||
|
listBoxPorts.Location = new Point(196, 150);
|
||||||
|
listBoxPorts.Name = "listBoxPorts";
|
||||||
|
listBoxPorts.Size = new Size(150, 104);
|
||||||
|
listBoxPorts.TabIndex = 0;
|
||||||
|
//
|
||||||
|
// Form1
|
||||||
|
//
|
||||||
|
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||||
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
|
ClientSize = new Size(800, 450);
|
||||||
|
Controls.Add(listBoxPorts);
|
||||||
|
Name = "Form1";
|
||||||
|
Text = "Form1";
|
||||||
|
ResumeLayout(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private ListBox listBoxPorts;
|
||||||
|
}
|
||||||
|
}
|
||||||
46
OMControl/Form1.cs
Normal file
46
OMControl/Form1.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System.IO.Ports;
|
||||||
|
|
||||||
|
namespace OMControl
|
||||||
|
{
|
||||||
|
public partial class Form1 : Form
|
||||||
|
{
|
||||||
|
private SerialPortWatcher _watcher;
|
||||||
|
public Form1()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
this.Load += Form1_Load;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Form1_Load(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
listBoxPorts.Items.Clear();
|
||||||
|
foreach (var p in SerialPort.GetPortNames())
|
||||||
|
listBoxPorts.Items.Add(p);
|
||||||
|
|
||||||
|
// start watcher
|
||||||
|
_watcher = new SerialPortWatcher(1000);
|
||||||
|
|
||||||
|
_watcher.PortsAdded += ports => BeginInvoke(() =>
|
||||||
|
{
|
||||||
|
foreach (var p in ports)
|
||||||
|
if (!listBoxPorts.Items.Contains(p))
|
||||||
|
listBoxPorts.Items.Add(p);
|
||||||
|
});
|
||||||
|
|
||||||
|
_watcher.PortsRemoved += ports => BeginInvoke(() =>
|
||||||
|
{
|
||||||
|
foreach (var p in ports)
|
||||||
|
listBoxPorts.Items.Remove(p);
|
||||||
|
});
|
||||||
|
|
||||||
|
_watcher.Start();
|
||||||
|
}
|
||||||
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||||
|
{
|
||||||
|
_watcher?.Dispose();
|
||||||
|
base.OnFormClosing(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
120
OMControl/Form1.resx
Normal file
120
OMControl/Form1.resx
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<root>
|
||||||
|
<!--
|
||||||
|
Microsoft ResX Schema
|
||||||
|
|
||||||
|
Version 2.0
|
||||||
|
|
||||||
|
The primary goals of this format is to allow a simple XML format
|
||||||
|
that is mostly human readable. The generation and parsing of the
|
||||||
|
various data types are done through the TypeConverter classes
|
||||||
|
associated with the data types.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
... ado.net/XML headers & schema ...
|
||||||
|
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||||
|
<resheader name="version">2.0</resheader>
|
||||||
|
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||||
|
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||||
|
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||||
|
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||||
|
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||||
|
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||||
|
</data>
|
||||||
|
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||||
|
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||||
|
<comment>This is a comment</comment>
|
||||||
|
</data>
|
||||||
|
|
||||||
|
There are any number of "resheader" rows that contain simple
|
||||||
|
name/value pairs.
|
||||||
|
|
||||||
|
Each data row contains a name, and value. The row also contains a
|
||||||
|
type or mimetype. Type corresponds to a .NET class that support
|
||||||
|
text/value conversion through the TypeConverter architecture.
|
||||||
|
Classes that don't support this are serialized and stored with the
|
||||||
|
mimetype set.
|
||||||
|
|
||||||
|
The mimetype is used for serialized objects, and tells the
|
||||||
|
ResXResourceReader how to depersist the object. This is currently not
|
||||||
|
extensible. For a given mimetype the value must be set accordingly:
|
||||||
|
|
||||||
|
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||||
|
that the ResXResourceWriter will generate, however the reader can
|
||||||
|
read any of the formats listed below.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.binary.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.soap.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||||
|
value : The object must be serialized into a byte array
|
||||||
|
: using a System.ComponentModel.TypeConverter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
-->
|
||||||
|
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||||
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:choice maxOccurs="unbounded">
|
||||||
|
<xsd:element name="metadata">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="assembly">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="alias" type="xsd:string" />
|
||||||
|
<xsd:attribute name="name" type="xsd:string" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="data">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="resheader">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:choice>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:schema>
|
||||||
|
<resheader name="resmimetype">
|
||||||
|
<value>text/microsoft-resx</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="version">
|
||||||
|
<value>2.0</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="reader">
|
||||||
|
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="writer">
|
||||||
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
</root>
|
||||||
15
OMControl/OMControl.csproj
Normal file
15
OMControl/OMControl.csproj
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFramework>net10.0-windows</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.IO.Ports" Version="10.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
17
OMControl/Program.cs
Normal file
17
OMControl/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace OMControl
|
||||||
|
{
|
||||||
|
internal static class Program
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The main entry point for the application.
|
||||||
|
/// </summary>
|
||||||
|
[STAThread]
|
||||||
|
static void Main()
|
||||||
|
{
|
||||||
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
|
// see https://aka.ms/applicationconfiguration.
|
||||||
|
ApplicationConfiguration.Initialize();
|
||||||
|
Application.Run(new Form1());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
112
OMControl/Serial/SerialDeviceProbe.cs
Normal file
112
OMControl/Serial/SerialDeviceProbe.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO.Ports;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public sealed class SerialDeviceProbe
|
||||||
|
{
|
||||||
|
public record ProbeResult(string Port, bool Success, string? DeviceId, byte[]? RawResponse, string? Error);
|
||||||
|
|
||||||
|
// Define known signatures here
|
||||||
|
// Example: response contains ASCII "DEV:XYZ" or starts with 0xAA 0x55 etc.
|
||||||
|
private readonly List<(string Name, Func<byte[], bool> Match)> _signatures = new();
|
||||||
|
|
||||||
|
public SerialDeviceProbe AddSignature(string name, Func<byte[], bool> match)
|
||||||
|
{
|
||||||
|
_signatures.Add((name, match));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ProbeResult> ProbeAsync(
|
||||||
|
string portName,
|
||||||
|
int baudRate,
|
||||||
|
byte[] probeBytes,
|
||||||
|
int readTimeoutMs = 250,
|
||||||
|
int writeTimeoutMs = 250,
|
||||||
|
int settleDelayMs = 80,
|
||||||
|
int maxReadBytes = 256,
|
||||||
|
CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await Task.Run(() =>
|
||||||
|
{
|
||||||
|
ct.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
using var sp = new SerialPort(portName, baudRate)
|
||||||
|
{
|
||||||
|
ReadTimeout = readTimeoutMs,
|
||||||
|
WriteTimeout = writeTimeoutMs,
|
||||||
|
Encoding = Encoding.ASCII,
|
||||||
|
DtrEnable = true, // often needed for Arduino-like boards
|
||||||
|
RtsEnable = true
|
||||||
|
};
|
||||||
|
|
||||||
|
sp.Open();
|
||||||
|
|
||||||
|
// Some devices reboot on open (Arduino). Give them a moment.
|
||||||
|
Thread.Sleep(settleDelayMs);
|
||||||
|
|
||||||
|
sp.DiscardInBuffer();
|
||||||
|
sp.DiscardOutBuffer();
|
||||||
|
|
||||||
|
sp.Write(probeBytes, 0, probeBytes.Length);
|
||||||
|
|
||||||
|
// Read available bytes up to timeout
|
||||||
|
var buffer = new List<byte>(maxReadBytes);
|
||||||
|
var start = Environment.TickCount;
|
||||||
|
|
||||||
|
while (buffer.Count < maxReadBytes && Environment.TickCount - start < readTimeoutMs)
|
||||||
|
{
|
||||||
|
ct.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int b = sp.ReadByte(); // blocks until byte or timeout
|
||||||
|
if (b >= 0) buffer.Add((byte)b);
|
||||||
|
|
||||||
|
// Optional: break early if you know response length or terminator
|
||||||
|
// e.g. if buffer ends with '\n'
|
||||||
|
if (buffer.Count >= 2 && buffer[^1] == (byte)'\n') break;
|
||||||
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp = buffer.ToArray();
|
||||||
|
if (resp.Length == 0)
|
||||||
|
return new ProbeResult(portName, false, null, resp, "No response");
|
||||||
|
|
||||||
|
// Identify by signature
|
||||||
|
foreach (var sig in _signatures)
|
||||||
|
{
|
||||||
|
if (sig.Match(resp))
|
||||||
|
return new ProbeResult(portName, true, sig.Name, resp, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown but responded
|
||||||
|
return new ProbeResult(portName, true, "Unknown", resp, null);
|
||||||
|
|
||||||
|
}, ct);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return new ProbeResult(portName, false, null, null, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ProbeResult[]> ProbeAllAsync(
|
||||||
|
IEnumerable<string> ports,
|
||||||
|
int baudRate,
|
||||||
|
byte[] probeBytes,
|
||||||
|
CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
var tasks = ports.Select(p => ProbeAsync(p, baudRate, probeBytes, ct: ct));
|
||||||
|
return await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
53
OMControl/Serial/SerialManager.cs
Normal file
53
OMControl/Serial/SerialManager.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO.Ports;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
public class SerialManager : IDisposable
|
||||||
|
{
|
||||||
|
private SerialPort _serialPort;
|
||||||
|
|
||||||
|
public event Action<string>? DataReceived;
|
||||||
|
|
||||||
|
public bool IsOpen => _serialPort?.IsOpen ?? false;
|
||||||
|
|
||||||
|
public SerialManager(string portName, int baudRate)
|
||||||
|
{
|
||||||
|
_serialPort = new SerialPort(portName, baudRate)
|
||||||
|
{
|
||||||
|
Encoding = Encoding.ASCII,
|
||||||
|
NewLine = "\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
_serialPort.DataReceived += OnDataReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Open()
|
||||||
|
{
|
||||||
|
if (!_serialPort.IsOpen)
|
||||||
|
_serialPort.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (_serialPort.IsOpen)
|
||||||
|
_serialPort.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send(string data)
|
||||||
|
{
|
||||||
|
if (_serialPort.IsOpen)
|
||||||
|
_serialPort.WriteLine(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
string data = _serialPort.ReadLine();
|
||||||
|
DataReceived?.Invoke(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
_serialPort.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
52
OMControl/Serial/SerialPortWatcher.cs
Normal file
52
OMControl/Serial/SerialPortWatcher.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO.Ports;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Timers;
|
||||||
|
|
||||||
|
public sealed class SerialPortWatcher : IDisposable
|
||||||
|
{
|
||||||
|
private readonly System.Timers.Timer _timer;
|
||||||
|
private HashSet<string> _last = new(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public event Action<string[]>? PortsAdded;
|
||||||
|
public event Action<string[]>? PortsRemoved;
|
||||||
|
|
||||||
|
public SerialPortWatcher(double intervalMs = 1000)
|
||||||
|
{
|
||||||
|
_timer = new System.Timers.Timer(intervalMs);
|
||||||
|
_timer.Elapsed += (_, _) => Tick();
|
||||||
|
_timer.AutoReset = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
_last = GetPorts();
|
||||||
|
_timer.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop() => _timer.Stop();
|
||||||
|
|
||||||
|
private void Tick()
|
||||||
|
{
|
||||||
|
var current = GetPorts();
|
||||||
|
|
||||||
|
var added = current.Except(_last).ToArray();
|
||||||
|
var removed = _last.Except(current).ToArray();
|
||||||
|
|
||||||
|
if (added.Length > 0) PortsAdded?.Invoke(added);
|
||||||
|
if (removed.Length > 0) PortsRemoved?.Invoke(removed);
|
||||||
|
|
||||||
|
_last = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashSet<string> GetPorts()
|
||||||
|
=> new HashSet<string>(SerialPort.GetPortNames(), StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_timer.Stop();
|
||||||
|
_timer.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user