Aggiungere i file di progetto.
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user