|
|
|
|
using EasyModbus;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ModbusSlave
|
|
|
|
|
{
|
|
|
|
|
public partial class MainForm : Form
|
|
|
|
|
{
|
|
|
|
|
public MainForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Field
|
|
|
|
|
|
|
|
|
|
private ModbusServer server;
|
|
|
|
|
private bool isFirstLog = true;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Button event
|
|
|
|
|
|
|
|
|
|
private void btnRun_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
server.Port = (int)nudPort.Value;
|
|
|
|
|
server.UnitIdentifier = Convert.ToByte(nudAddress.Value);
|
|
|
|
|
|
|
|
|
|
if (!CheckPortInUse(server.Port))
|
|
|
|
|
{
|
|
|
|
|
Log("[WARNING] unavailable port");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server.Listen();
|
|
|
|
|
Log("--- Server start ---");
|
|
|
|
|
|
|
|
|
|
btnRun.Enabled = false;
|
|
|
|
|
nudAddress.Enabled = false;
|
|
|
|
|
nudPort.Enabled = false;
|
|
|
|
|
|
|
|
|
|
tbStatus.Text = "Run";
|
|
|
|
|
tbStatus.BackColor = Color.Lime;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log("[ERROR] " + ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnStop_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
server.StopListening();
|
|
|
|
|
Log("--- Server stop ---");
|
|
|
|
|
|
|
|
|
|
btnRun.Enabled = true;
|
|
|
|
|
nudAddress.Enabled = true;
|
|
|
|
|
nudPort.Enabled = true;
|
|
|
|
|
|
|
|
|
|
tbStatus.Text = "Stop";
|
|
|
|
|
tbStatus.BackColor = Color.Silver;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log("[ERROR] " + ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnClear_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ClearLog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Timer event
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Listview event
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Etc event
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Method
|
|
|
|
|
|
|
|
|
|
private void InitInstance()
|
|
|
|
|
{
|
|
|
|
|
server = new ModbusServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Log(string text)
|
|
|
|
|
{
|
|
|
|
|
rtbLog.AppendText((isFirstLog ? "" : Environment.NewLine) + text);
|
|
|
|
|
isFirstLog = false;
|
|
|
|
|
|
|
|
|
|
ScrollToEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScrollToEnd()
|
|
|
|
|
{
|
|
|
|
|
if (!chkAutoScroll.Checked)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
rtbLog.SelectionStart = rtbLog.Text.Length;
|
|
|
|
|
rtbLog.ScrollToCaret();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClearLog()
|
|
|
|
|
{
|
|
|
|
|
rtbLog.Clear();
|
|
|
|
|
isFirstLog = true;
|
|
|
|
|
ScrollToEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckPortInUse(int port)
|
|
|
|
|
{
|
|
|
|
|
bool isAvailable = true;
|
|
|
|
|
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
|
|
|
|
|
var tcpListenerArray = ipGlobalProperties.GetActiveTcpListeners();
|
|
|
|
|
|
|
|
|
|
foreach (var ip in tcpListenerArray)
|
|
|
|
|
{
|
|
|
|
|
if (ip.Port == port)
|
|
|
|
|
{
|
|
|
|
|
isAvailable = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|