Form1.cs:
using HslCommunication.LogNet;
using HslCommunication.ModBus;
using System;
using System.Windows.Forms;
using System.Timers;
namespace Modbus模拟器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Timers.Timer timer_refresh_data, timer_move_delay;
HslCommunication.ModBus.ModbusTcpServer server;
ModBusMonitorAddress monitor_x_address,monitor_x_start, monitor_y_address, monitor_y_start, monitor_z_address, monitor_z_start;
System.Threading.AutoResetEvent move_delay;
private void Form1_Load(object sender, EventArgs e)
{
btn_stop.Enabled = false;
CheckForIllegalCrossThreadCalls = false;
lb_x.Text = "X:" + config_json.X轴当前位置a;
lb_y.Text = "Y:" + config_json.Y轴当前位置a;
lb_z.Text = "Z:" + config_json.Z轴当前位置a;
ts1.Text = "Design by zyb.pub.";
ts2.Text = "2022-05-10";
ts3.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); ;
}
private void btn_start_Click(object sender, EventArgs e)
{
server = new HslCommunication.ModBus.ModbusTcpServer();
server.DataFormat = HslCommunication.Core.DataFormat.CDAB;
server.LogNet = new HslCommunication.LogNet.LogNetSingle("modbus_server_log.txt");
server.LogNet.BeforeSaveToFile += LogNetBeforeSaveToFile;
server.OnDataReceived += ServerOnDataReceived;
server.ServerStart(502);
tb_log.AppendText("ModbusTcpServer 已启动" +"\r\n");
monitor_x_address = new ModBusMonitorAddress();
monitor_x_address.Address = Convert.ToUInt16( config_json.X轴相对运动坐标写入a);
monitor_x_address.OnChange += monitor_x_OnChange;
monitor_x_address.OnWrite += monitor_x_OnWrite;
server.AddSubcription(monitor_x_address);
monitor_x_start = new ModBusMonitorAddress();
monitor_x_start.Address = Convert.ToUInt16(config_json.X轴相对运动启动a);
monitor_x_start.OnChange += monitor_x_start_OnChange;
monitor_x_start.OnWrite += monitor_x_start_OnWrite;
server.AddSubcription(monitor_x_start);
monitor_y_address = new ModBusMonitorAddress();
monitor_y_address.Address = Convert.ToUInt16(config_json.Y轴相对运动坐标写入a);
monitor_y_address.OnChange += monitor_y_OnChange;
monitor_y_address.OnWrite += monitor_y_OnWrite;
server.AddSubcription(monitor_y_address);
monitor_y_start = new ModBusMonitorAddress();
monitor_y_start.Address = Convert.ToUInt16(config_json.Y轴相对运动启动a);
monitor_y_start.OnChange += monitor_y_start_OnChange;
monitor_y_start.OnWrite += monitor_y_start_OnWrite;
server.AddSubcription(monitor_y_start);
monitor_z_address = new ModBusMonitorAddress();
monitor_z_address.Address = Convert.ToUInt16(config_json.Z轴相对运动坐标写入a);
monitor_z_address.OnChange += monitor_z_OnChange;
monitor_z_address.OnWrite += monitor_z_OnWrite;
server.AddSubcription(monitor_z_address);
monitor_z_start = new ModBusMonitorAddress();
monitor_z_start.Address = Convert.ToUInt16(config_json.Z轴相对运动启动a);
monitor_z_start.OnChange += monitor_z_start_OnChange;
monitor_z_start.OnWrite += monitor_z_start_OnWrite;
server.AddSubcription(monitor_z_start);
config_json.speed = Convert.ToInt16(tb_speed.Text);
timer_refresh_data = new System.Timers.Timer(200);
timer_refresh_data.Elapsed += new System.Timers.ElapsedEventHandler(timer_refresh_data_Tick);
timer_refresh_data.AutoReset = true;
timer_refresh_data.Enabled = true;
timer_move_delay = new System.Timers.Timer(1000);
timer_move_delay.Elapsed += new System.Timers.ElapsedEventHandler(timer_move_delay_Tick);//
timer_move_delay.AutoReset = false;
timer_move_delay.Enabled = false;
//写入当前初始位置
server.Write(config_json.X轴当前位置a, config_json.X轴当前位置v);
server.Write(config_json.Y轴当前位置a, config_json.Y轴当前位置v);
server.Write(config_json.Z轴当前位置a, config_json.Z轴当前位置v);
//写入初始相对移动距离
server.Write(config_json.X轴相对运动坐标写入a, config_json.X轴相对运动坐标写入v);
server.Write(config_json.Y轴相对运动坐标写入a, config_json.Y轴相对运动坐标写入v);
server.Write(config_json.Z轴相对运动坐标写入a, config_json.Z轴相对运动坐标写入v);
move_delay = new System.Threading.AutoResetEvent(false);
btn_start.Enabled = false;
btn_stop.Enabled = true;
}
private void timer_move_delay_Tick(object sender, ElapsedEventArgs e)
{
move_delay.Set();
}
//刷新当前位置、相对移动距离
private void timer_refresh_data_Tick(object sender, ElapsedEventArgs e)
{
lb_currentx.Text = (server.ReadInt32(config_json.X轴当前位置a)/1000).ToString();
lb_currenty.Text = (server.ReadInt32(config_json.Y轴当前位置a) / 1000).ToString();
lb_currentz.Text = (server.ReadInt32(config_json.Z轴当前位置a) / 1000).ToString();
lb_relax.Text = (server.ReadInt32(config_json.X轴相对运动坐标写入a) / 1000).ToString();
lb_relay.Text = (server.ReadInt32(config_json.Y轴相对运动坐标写入a) / 1000).ToString();
lb_relaz.Text = (server.ReadInt32(config_json.Z轴相对运动坐标写入a) / 1000).ToString();
}
private void monitor_x_start_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
Int32 current_x = server.ReadInt32(config_json.X轴当前位置a);
Int32 x = server.ReadInt32(config_json.X轴相对运动启动a);
tb_log.AppendText("X启动:" + monitor_x_start.Address + " 变更为: " + x + "\r\n");
if (x == 1)
{
server.Write(config_json.X轴相对运动启动a, 0);
server.Write(config_json.X轴相对运动完成a, 0);
tb_log.AppendText("X轴开始移动" + "\r\n");
int time = Math.Abs(config_json.X轴相对运动坐标写入v / 1000) / config_json.speed;
tb_log.AppendText("移动将耗时:" + time + "\r\n");
if (time > 0)
{
timer_move_delay.Interval = time * 1000;
timer_move_delay.Enabled = true;
move_delay.WaitOne();
}
server.Write(config_json.X轴当前位置a, current_x + config_json.X轴相对运动坐标写入v);
tb_log.AppendText("X轴移动完成" + "\r\n\r\n");
server.Write(config_json.X轴相对运动完成a, 1);
}
}
private void monitor_y_start_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
Int32 current_y = server.ReadInt32(config_json.Y轴当前位置a);
Int32 y= server.ReadInt32(config_json.Y轴相对运动启动a);
tb_log.AppendText("Y启动:" + monitor_y_start.Address + " 变更为: " + y + "\r\n");
if (y == 1)
{
server.Write(config_json.Y轴相对运动完成a, 0);
tb_log.AppendText("Y轴开始移动" + "\r\n");
int time = Math.Abs(config_json.Y轴相对运动坐标写入v / 1000) / config_json.speed;
tb_log.AppendText("移动将耗时:" + time + "\r\n");
if (time > 0)
{
timer_move_delay.Interval = time * 1000;
timer_move_delay.Enabled = true;
move_delay.WaitOne();
}
server.Write(config_json.Y轴当前位置a, current_y + config_json.Y轴相对运动坐标写入v);
server.Write(config_json.Y轴相对运动启动a, 0);
tb_log.AppendText("Y轴移动完成" + "\r\n\r\n");
server.Write(config_json.Y轴相对运动完成a, 1);
}
}
private void monitor_z_start_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
Int32 current_z = server.ReadInt32(config_json.Z轴当前位置a);
Int32 z = server.ReadInt32(config_json.Z轴相对运动启动a);
tb_log.AppendText("Z启动:" + monitor_z_start.Address + " 变更为: " + z + "\r\n");
if (z == 1)
{
server.Write(config_json.Z轴相对运动完成a, 0);
tb_log.AppendText("Z轴开始移动" + "\r\n");
int time = Math.Abs(config_json.Z轴相对运动坐标写入v / 1000) / config_json.speed;
tb_log.AppendText("移动将耗时:" + time + "\r\n");
if (time > 0)
{
timer_move_delay.Interval = time * 1000;
timer_move_delay.Enabled = true;
move_delay.WaitOne();
}
server.Write(config_json.Z轴当前位置a, current_z + config_json.Z轴相对运动坐标写入v);
server.Write(config_json.Z轴相对运动启动a, 0);
tb_log.AppendText("Z轴移动完成" + "\r\n\r\n");
server.Write(config_json.Z轴相对运动完成a, 1);
}
}
private void monitor_x_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
config_json.X轴相对运动坐标写入v = server.ReadInt32(config_json.X轴相对运动坐标写入a);
tb_X轴相对运动坐标写入v.Text = config_json.X轴相对运动坐标写入v.ToString();
tb_log.AppendText("X" + monitor_x_address.Address + "相对运动位置变更:" + config_json.X轴相对运动坐标写入v + "\r\n");
}
private void monitor_y_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
config_json.Y轴相对运动坐标写入v = server.ReadInt32(config_json.Y轴相对运动坐标写入a);
tb_Y轴相对运动坐标写入v.Text = config_json.Y轴相对运动坐标写入v.ToString();
tb_log.AppendText("Y" + monitor_y_address.Address + "相对运动位置变更:" + config_json.Y轴相对运动坐标写入v + "\r\n");
}
private void monitor_z_OnChange(ModBusMonitorAddress arg1, short arg2, short arg3)
{
config_json.Z轴相对运动坐标写入v = server.ReadInt32(config_json.Z轴相对运动坐标写入a);
tb_Z轴相对运动坐标写入v.Text = config_json.Y轴相对运动坐标写入v.ToString();
tb_log.AppendText("Z" + monitor_z_address.Address + "相对运动位置变更:" + config_json.Z轴相对运动坐标写入v + "\r\n");
}
private void btn_stop_Click(object sender, EventArgs e)
{
try
{
server.ServerClose();
btn_start.Enabled = true;
}
catch { }
tb_log.AppendText("ModbusTcpServer 已停止" + "\r\n");
}
private void ServerOnDataReceived(ModbusTcpServer modbusTcpServer, byte[] data)
{
if (InvokeRequired)
{
// BeginInvoke(new Action<byte[]>(ServerOnDataReceived), data);
// return;
}
//tb_log.AppendText("接收数据:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(data) + "\r\n");
}
private void LogNetBeforeSaveToFile(object sender, HslEventArgs e)
{
tb_log.AppendText("接收数据:" + e.HslMessage.ToString() + "\r\n");
}
private void btn_SetX_Current_Click(object sender, EventArgs e)
{
server.Write(config_json.X轴当前位置a, Convert.ToInt32( tb_x_current.Text)*1000);
}
private void btn_SetY_Current_Click(object sender, EventArgs e)
{
server.Write(config_json.Y轴当前位置a, Convert.ToInt32(tb_y_current.Text)*1000);
}
private void btn_SetZ_Current_Click(object sender, EventArgs e)
{
server.Write(config_json.Z轴当前位置a, Convert.ToInt32(tb_z_current.Text)*1000);
}
private void btn_movex_Click(object sender, EventArgs e)
{
server.Write(config_json.X轴相对运动启动a, 1);
}
private void btn_movey_Click(object sender, EventArgs e)
{
server.Write(config_json.Y轴相对运动启动a, 1);
}
private void btn_movez_Click(object sender, EventArgs e)
{

该程序是一个Modbus模拟器,用于模拟三坐标系统的相对运动控制。它通过Modbus TCP协议与服务器交互,监听并响应地址变更,根据接收到的坐标值启动相对运动,并记录运动日志。用户可以设置初始位置、速度以及相对移动距离,模拟器还提供了实时显示当前位置和相对移动距离的功能。
3615

被折叠的 条评论
为什么被折叠?



