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)
{
server.Write(config_json.Z轴相对运动启动a, 1);
}
private void button1_Click(object sender, EventArgs e)
{
config_json.speed = Convert.ToInt16(tb_speed.Text);
}
private void btn_clear_Click(object sender, EventArgs e)
{
tb_log.Text = "";
}
private void btn_保存为初始值_Click(object sender, EventArgs e)
{
string json = System.IO.File.ReadAllText(config_json.config_file_path);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
jsonObj["X轴当前位置v"] = tb_x_current.Text;
jsonObj["Y轴当前位置v"] = tb_x_current.Text;
jsonObj["Z轴当前位置v"] = tb_x_current.Text;
jsonObj["speed"] = tb_speed.Text;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(config_json.config_file_path, output);
config_json.config_json_readall();
}
private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form_setup f = new Form_setup();
f.ShowDialog();
}
//这个暂时没有用
private void monitor_x_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
// tb_log.AppendText(monitor_x_address.Address+"changed to "+arg2 +"\r\n");
}
private void monitor_y_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
}
private void monitor_z_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
}
private void monitor_x_start_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
}
private void monitor_y_start_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
}
private void monitor_z_start_OnWrite(ModBusMonitorAddress arg1, short arg2)
{
}
}
}
Form1.Designer.cs:
namespace Modbus模拟器
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tb_log = new System.Windows.Forms.TextBox();
this.btn_start = new System.Windows.Forms.Button();
this.btn_stop = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.lb_x = new System.Windows.Forms.Label();
this.tb_x_current = new System.Windows.Forms.TextBox();
this.btn_SetX_Current = new System.Windows.Forms.Button();
this.btn_SetY_Current = new System.Windows.Forms.Button();
this.tb_y_current = new System.Windows.Forms.TextBox();
this.lb_y = new System.Windows.Forms.Label();
this.btn_SetZ_Current = new System.Windows.Forms.Button();
this.tb_z_current = new System.Windows.Forms.TextBox();
this.lb_z = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.ts1 = new System.Windows.Forms.ToolStripStatusLabel();
this.ts2 = new System.Windows.Forms.ToolStripStatusLabel();
this.ts3 = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.tb_speed = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.文件ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.btn_movez = new System.Windows.Forms.Button();
this.btn_movey = new System.Windows.Forms.Button();
this.btn_movex = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.tb_Z轴相对运动坐标写入v = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.tb_Y轴相对运动坐标写入v = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.tb_X轴相对运动坐标写入v = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.lb_currentz = new System.Windows.Forms.Label();
this.lb_currenty = new System.Windows.Forms.Label();
this.lb_currentx = new System.Windows.Forms.Label();
this.lb_relaz = new System.Windows.Forms.Label();
this.lb_relay = new System.Windows.Forms.Label();
this.lb_relax = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label17 = new System.Windows.Forms.Label();
this.当前位置 = new System.Windows.Forms.Label();
this.btn_clear = new System.Windows.Forms.Button();
this.btn_保存为初始值 = new System.Windows.Forms.Button();
this.设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.groupBox1.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// tb_log
//
this.tb_log.Location = new System.Drawing.Point(29, 74);
this.tb_log.Multiline = true;
this.tb_log.Name = "tb_log";
this.tb_log.Size = new System.Drawing.Size(560, 388);
this.tb_log.TabIndex = 3;
//
// btn_start
//
this.btn_start.Location = new System.Drawing.Point(913, 62);
this.btn_start.Name = "btn_start";
this.btn_start.Size = new System.Drawing.Size(100, 43);
this.btn_start.TabIndex = 2;
this.btn_start.Text = "Start";
this.btn_start.UseVisualStyleBackColor = true;
this.btn_start.Click += new System.EventHandler(this.btn_start_Click);
//
// btn_stop
//
this.btn_stop.Location = new System.Drawing.Point(913, 119);
this.btn_stop.Name = "btn_stop";
this.btn_stop.Size = new System.Drawing.Size(100, 36);
this.btn_stop.TabIndex = 4;
this.btn_stop.Text = "Stop";
this.btn_stop.UseVisualStyleBackColor = true;
this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(346, 34);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(425, 29);
this.label1.TabIndex = 5;
this.label1.Text = "ModbusTCP三坐标移动PLC模拟器";
//
// lb_x
//
this.lb_x.AutoSize = true;
this.lb_x.Location = new System.Drawing.Point(7, 18);
this.lb_x.Name = "lb_x";
this.lb_x.Size = new System.Drawing.Size(11, 12);
this.lb_x.TabIndex = 6;
this.lb_x.Text = "X";
//
// tb_x_current
//
this.tb_x_current.Location = new System.Drawing.Point(50, 15);
this.tb_x_current.Name = "tb_x_current";
this.tb_x_current.Size = new System.Drawing.Size(36, 21);
this.tb_x_current.TabIndex = 8;
this.tb_x_current.Text = "2150";
//
// btn_SetX_Current
//
this.btn_SetX_Current.Location = new System.Drawing.Point(117, 13);
this.btn_SetX_Current.Name = "btn_SetX_Current";
this.btn_SetX_Current.Size = new System.Drawing.Size(52, 23);
this.btn_SetX_Current.TabIndex = 9;
this.btn_SetX_Current.Text = "Set";
this.btn_SetX_Current.UseVisualStyleBackColor = true;
this.btn_SetX_Current.Click += new System.EventHandler(this.btn_SetX_Current_Click);
//
// btn_SetY_Current
//
this.btn_SetY_Current.Location = new System.Drawing.Point(117, 42);
this.btn_SetY_Current.Name = "btn_SetY_Current";
this.btn_SetY_Current.Size = new System.Drawing.Size(52, 23);
this.btn_SetY_Current.TabIndex = 12;
this.btn_SetY_Current.Text = "Set";
this.btn_SetY_Current.UseVisualStyleBackColor = true;
this.btn_SetY_Current.Click += new System.EventHandler(this.btn_SetY_Current_Click);
//
// tb_y_current
//
this.tb_y_current.Location = new System.Drawing.Point(50, 44);
this.tb_y_current.Name = "tb_y_current";
this.tb_y_current.Size = new System.Drawing.Size(36, 21);
this.tb_y_current.TabIndex = 11;
this.tb_y_current.Text = "0";
//
// lb_y
//
this.lb_y.AutoSize = true;
this.lb_y.Location = new System.Drawing.Point(7, 47);
this.lb_y.Name = "lb_y";
this.lb_y.Size = new System.Drawing.Size(11, 12);
this.lb_y.TabIndex = 10;
this.lb_y.Text = "Y";
//
// btn_SetZ_Current
//
this.btn_SetZ_Current.Location = new System.Drawing.Point(117, 71);
this.btn_SetZ_Current.Name = "btn_SetZ_Current";
this.btn_SetZ_Current.Size = new System.Drawing.Size(52, 23);
this.btn_SetZ_Current.TabIndex = 15;
this.btn_SetZ_Current.Text = "Set";
this.btn_SetZ_Current.UseVisualStyleBackColor = true;
this.btn_SetZ_Current.Click += new System.EventHandler(this.btn_SetZ_Current_Click);
//
// tb_z_current
//
this.tb_z_current.Location = new System.Drawing.Point(50, 73);
this.tb_z_current.Name = "tb_z_current";
this.tb_z_current.Size = new System.Drawing.Size(36, 21);
this.tb_z_current.TabIndex = 14;
this.tb_z_current.Text = "100";
//
// lb_z
//
this.lb_z.AutoSize = true;
this.lb_z.Location = new System.Drawing.Point(7, 76);
this.lb_z.Name = "lb_z";
this.lb_z.Size = new System.Drawing.Size(11, 12);
this.lb_z.TabIndex = 13;
this.lb_z.Text = "Z";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btn_保存为初始值);
this.groupBox1.Controls.Add(this.btn_SetZ_Current);
this.groupBox1.Controls.Add(this.tb_z_current);
this.groupBox1.Controls.Add(this.lb_z);
this.groupBox1.Controls.Add(this.btn_SetY_Current);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.tb_y_current);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.tb_speed);
this.groupBox1.Controls.Add(this.lb_y);
this.groupBox1.Controls.Add(this.btn_SetX_Current);
this.groupBox1.Controls.Add(this.tb_x_current);
this.groupBox1.Controls.Add(this.lb_x);
this.groupBox1.Location = new System.Drawing.Point(604, 201);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(186, 164);
this.groupBox1.TabIndex = 16;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "设置当前坐标(mm)";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ts1,
this.ts2,
this.ts3});
this.statusStrip1.Location = new System.Drawing.Point(0, 496);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(1052, 22);
this.statusStrip1.TabIndex = 17;
this.statusStrip1.Text = "statusStrip1";
//
// ts1
//
this.ts1.Name = "ts1";
this.ts1.Size = new System.Drawing.Size(25, 17);
this.ts1.Text = "ts1";
//
// ts2
//
this.ts2.Name = "ts2";
this.ts2.Size = new System.Drawing.Size(25, 17);
this.ts2.Text = "ts2";
//
// ts3
//
this.ts3.Name = "ts3";
this.ts3.Size = new System.Drawing.Size(25, 17);
this.ts3.Text = "ts3";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Location = new System.Drawing.Point(595, 385);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(429, 101);
this.groupBox2.TabIndex = 18;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "软件说明";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(10, 22);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(281, 12);
this.label2.TabIndex = 0;
this.label2.Text = "本软件用于模拟使用Modbus控制的三坐标移动系统。";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 108);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 12);
this.label3.TabIndex = 19;
this.label3.Text = "Speed:";
//
// tb_speed
//
this.tb_speed.Location = new System.Drawing.Point(48, 103);
this.tb_speed.Name = "tb_speed";
this.tb_speed.Size = new System.Drawing.Size(30, 21);
this.tb_speed.TabIndex = 20;
this.tb_speed.Text = "50";
//
// button1
//
this.button1.Location = new System.Drawing.Point(115, 102);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(52, 23);
this.button1.TabIndex = 21;
this.button1.Text = "Set";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(82, 108);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(29, 12);
this.label4.TabIndex = 22;
this.label4.Text = "mm/s";
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.文件ToolStripMenuItem,
this.帮助ToolStripMenuItem,
this.设置ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1052, 25);
this.menuStrip1.TabIndex = 23;
this.menuStrip1.Text = "menuStrip1";
//
// 文件ToolStripMenuItem
//
this.文件ToolStripMenuItem.Name = "文件ToolStripMenuItem";
this.文件ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
this.文件ToolStripMenuItem.Text = "文件";
//
// 帮助ToolStripMenuItem
//
this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
this.帮助ToolStripMenuItem.Text = "帮助";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.btn_movez);
this.groupBox3.Controls.Add(this.btn_movey);
this.groupBox3.Controls.Add(this.btn_movex);
this.groupBox3.Controls.Add(this.button2);
this.groupBox3.Controls.Add(this.tb_Z轴相对运动坐标写入v);
this.groupBox3.Controls.Add(this.label5);
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Controls.Add(this.tb_Y轴相对运动坐标写入v);
this.groupBox3.Controls.Add(this.label6);
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Controls.Add(this.tb_X轴相对运动坐标写入v);
this.groupBox3.Controls.Add(this.label7);
this.groupBox3.Location = new System.Drawing.Point(796, 238);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(228, 110);
this.groupBox3.TabIndex = 24;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "相对移动距离(mm)";
//
// btn_movez
//
this.btn_movez.Location = new System.Drawing.Point(175, 73);
this.btn_movez.Name = "btn_movez";
this.btn_movez.Size = new System.Drawing.Size(42, 21);
this.btn_movez.TabIndex = 18;
this.btn_movez.Text = "Mov";
this.btn_movez.UseVisualStyleBackColor = true;
this.btn_movez.Click += new System.EventHandler(this.btn_movez_Click);
//
// btn_movey
//
this.btn_movey.Location = new System.Drawing.Point(175, 44);
this.btn_movey.Name = "btn_movey";
this.btn_movey.Size = new System.Drawing.Size(42, 21);
this.btn_movey.TabIndex = 17;
this.btn_movey.Text = "Mov";
this.btn_movey.UseVisualStyleBackColor = true;
this.btn_movey.Click += new System.EventHandler(this.btn_movey_Click);
//
// btn_movex
//
this.btn_movex.Location = new System.Drawing.Point(175, 15);
this.btn_movex.Name = "btn_movex";
this.btn_movex.Size = new System.Drawing.Size(42, 21);
this.btn_movex.TabIndex = 16;
this.btn_movex.Text = "Mov";
this.btn_movex.UseVisualStyleBackColor = true;
this.btn_movex.Click += new System.EventHandler(this.btn_movex_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(117, 71);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(52, 23);
this.button2.TabIndex = 15;
this.button2.Text = "Set";
this.button2.UseVisualStyleBackColor = true;
//
// tb_Z轴相对运动坐标写入v
//
this.tb_Z轴相对运动坐标写入v.Location = new System.Drawing.Point(50, 73);
this.tb_Z轴相对运动坐标写入v.Name = "tb_Z轴相对运动坐标写入v";
this.tb_Z轴相对运动坐标写入v.Size = new System.Drawing.Size(35, 21);
this.tb_Z轴相对运动坐标写入v.TabIndex = 14;
this.tb_Z轴相对运动坐标写入v.Text = "0";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(15, 76);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(11, 12);
this.label5.TabIndex = 13;
this.label5.Text = "Z";
//
// button3
//
this.button3.Location = new System.Drawing.Point(117, 42);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(52, 23);
this.button3.TabIndex = 12;
this.button3.Text = "Set";
this.button3.UseVisualStyleBackColor = true;
//
// tb_Y轴相对运动坐标写入v
//
this.tb_Y轴相对运动坐标写入v.Location = new System.Drawing.Point(50, 44);
this.tb_Y轴相对运动坐标写入v.Name = "tb_Y轴相对运动坐标写入v";
this.tb_Y轴相对运动坐标写入v.Size = new System.Drawing.Size(35, 21);
this.tb_Y轴相对运动坐标写入v.TabIndex = 11;
this.tb_Y轴相对运动坐标写入v.Text = "0";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(15, 47);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(11, 12);
this.label6.TabIndex = 10;
this.label6.Text = "Y";
//
// button4
//
this.button4.Location = new System.Drawing.Point(117, 13);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(52, 23);
this.button4.TabIndex = 9;
this.button4.Text = "Set";
this.button4.UseVisualStyleBackColor = true;
//
// tb_X轴相对运动坐标写入v
//
this.tb_X轴相对运动坐标写入v.Location = new System.Drawing.Point(50, 15);
this.tb_X轴相对运动坐标写入v.Name = "tb_X轴相对运动坐标写入v";
this.tb_X轴相对运动坐标写入v.Size = new System.Drawing.Size(35, 21);
this.tb_X轴相对运动坐标写入v.TabIndex = 8;
this.tb_X轴相对运动坐标写入v.Text = "0";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(15, 18);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(11, 12);
this.label7.TabIndex = 6;
this.label7.Text = "X";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(641, 154);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(11, 12);
this.label8.TabIndex = 27;
this.label8.Text = "Z";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(641, 123);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(11, 12);
this.label9.TabIndex = 26;
this.label9.Text = "Y";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(641, 93);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(11, 12);
this.label10.TabIndex = 25;
this.label10.Text = "X";
//
// lb_currentz
//
this.lb_currentz.AutoSize = true;
this.lb_currentz.Location = new System.Drawing.Point(668, 154);
this.lb_currentz.Name = "lb_currentz";
this.lb_currentz.Size = new System.Drawing.Size(11, 12);
this.lb_currentz.TabIndex = 30;
this.lb_currentz.Text = "Z";
//
// lb_currenty
//
this.lb_currenty.AutoSize = true;
this.lb_currenty.Location = new System.Drawing.Point(668, 123);
this.lb_currenty.Name = "lb_currenty";
this.lb_currenty.Size = new System.Drawing.Size(11, 12);
this.lb_currenty.TabIndex = 29;
this.lb_currenty.Text = "Y";
//
// lb_currentx
//
this.lb_currentx.AutoSize = true;
this.lb_currentx.Location = new System.Drawing.Point(668, 93);
this.lb_currentx.Name = "lb_currentx";
this.lb_currentx.Size = new System.Drawing.Size(11, 12);
this.lb_currentx.TabIndex = 28;
this.lb_currentx.Text = "X";
//
// lb_relaz
//
this.lb_relaz.AutoSize = true;
this.lb_relaz.Location = new System.Drawing.Point(779, 154);
this.lb_relaz.Name = "lb_relaz";
this.lb_relaz.Size = new System.Drawing.Size(11, 12);
this.lb_relaz.TabIndex = 36;
this.lb_relaz.Text = "Z";
//
// lb_relay
//
this.lb_relay.AutoSize = true;
this.lb_relay.Location = new System.Drawing.Point(779, 123);
this.lb_relay.Name = "lb_relay";
this.lb_relay.Size = new System.Drawing.Size(11, 12);
this.lb_relay.TabIndex = 35;
this.lb_relay.Text = "Y";
//
// lb_relax
//
this.lb_relax.AutoSize = true;
this.lb_relax.Location = new System.Drawing.Point(779, 93);
this.lb_relax.Name = "lb_relax";
this.lb_relax.Size = new System.Drawing.Size(11, 12);
this.lb_relax.TabIndex = 34;
this.lb_relax.Text = "X";
//
// label14
//
this.label14.AutoSize = true;
this.label14.Location = new System.Drawing.Point(752, 154);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(11, 12);
this.label14.TabIndex = 33;
this.label14.Text = "Z";
//
// label15
//
this.label15.AutoSize = true;
this.label15.Location = new System.Drawing.Point(752, 123);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(11, 12);
this.label15.TabIndex = 32;
this.label15.Text = "Y";
//
// label16
//
this.label16.AutoSize = true;
this.label16.Location = new System.Drawing.Point(752, 93);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(11, 12);
this.label16.TabIndex = 31;
this.label16.Text = "X";
//
// label17
//
this.label17.AutoSize = true;
this.label17.Location = new System.Drawing.Point(752, 74);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(77, 12);
this.label17.TabIndex = 38;
this.label17.Text = "相对移动距离";
//
// 当前位置
//
this.当前位置.AutoSize = true;
this.当前位置.Location = new System.Drawing.Point(626, 74);
this.当前位置.Name = "当前位置";
this.当前位置.Size = new System.Drawing.Size(53, 12);
this.当前位置.TabIndex = 37;
this.当前位置.Text = "当前位置";
//
// btn_clear
//
this.btn_clear.Location = new System.Drawing.Point(499, 468);
this.btn_clear.Name = "btn_clear";
this.btn_clear.Size = new System.Drawing.Size(89, 28);
this.btn_clear.TabIndex = 39;
this.btn_clear.Text = "清空内容";
this.btn_clear.UseVisualStyleBackColor = true;
this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);
//
// btn_保存为初始值
//
this.btn_保存为初始值.Location = new System.Drawing.Point(66, 131);
this.btn_保存为初始值.Name = "btn_保存为初始值";
this.btn_保存为初始值.Size = new System.Drawing.Size(101, 23);
this.btn_保存为初始值.TabIndex = 23;
this.btn_保存为初始值.Text = "保存为初始值";
this.btn_保存为初始值.UseVisualStyleBackColor = true;
this.btn_保存为初始值.Click += new System.EventHandler(this.btn_保存为初始值_Click);
//
// 设置ToolStripMenuItem
//
this.设置ToolStripMenuItem.Name = "设置ToolStripMenuItem";
this.设置ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
this.设置ToolStripMenuItem.Text = "设置";
this.设置ToolStripMenuItem.Click += new System.EventHandler(this.设置ToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1052, 518);
this.Controls.Add(this.btn_clear);
this.Controls.Add(this.label17);
this.Controls.Add(this.当前位置);
this.Controls.Add(this.lb_relaz);
this.Controls.Add(this.lb_relay);
this.Controls.Add(this.lb_relax);
this.Controls.Add(this.label14);
this.Controls.Add(this.label15);
this.Controls.Add(this.label16);
this.Controls.Add(this.lb_currentz);
this.Controls.Add(this.lb_currenty);
this.Controls.Add(this.lb_currentx);
this.Controls.Add(this.label8);
this.Controls.Add(this.label9);
this.Controls.Add(this.label10);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.menuStrip1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.btn_stop);
this.Controls.Add(this.tb_log);
this.Controls.Add(this.btn_start);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ModbusTCP三坐标移动PLC模拟器";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox tb_log;
private System.Windows.Forms.Button btn_start;
private System.Windows.Forms.Button btn_stop;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lb_x;
private System.Windows.Forms.TextBox tb_x_current;
private System.Windows.Forms.Button btn_SetX_Current;
private System.Windows.Forms.Button btn_SetY_Current;
private System.Windows.Forms.TextBox tb_y_current;
private System.Windows.Forms.Label lb_y;
private System.Windows.Forms.Button btn_SetZ_Current;
private System.Windows.Forms.TextBox tb_z_current;
private System.Windows.Forms.Label lb_z;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel ts1;
private System.Windows.Forms.ToolStripStatusLabel ts2;
private System.Windows.Forms.ToolStripStatusLabel ts3;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tb_speed;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox tb_Z轴相对运动坐标写入v;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox tb_Y轴相对运动坐标写入v;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox tb_X轴相对运动坐标写入v;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Button btn_movez;
private System.Windows.Forms.Button btn_movey;
private System.Windows.Forms.Button btn_movex;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label lb_currentz;
private System.Windows.Forms.Label lb_currenty;
private System.Windows.Forms.Label lb_currentx;
private System.Windows.Forms.Label lb_relaz;
private System.Windows.Forms.Label lb_relay;
private System.Windows.Forms.Label lb_relax;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label17;
private System.Windows.Forms.Label 当前位置;
private System.Windows.Forms.Button btn_clear;
private System.Windows.Forms.Button btn_保存为初始值;
private System.Windows.Forms.ToolStripMenuItem 设置ToolStripMenuItem;
}
}
config_json.cs:
namespace Modbus模拟器
{
class config_json
{
public static int speed = 5;
//当前位置地址读取:
public static string R轴当前位置a = "5500"; //读取的值单位为毫度(1000毫度=1度);
public static string X轴当前位置a = "5540"; //读取的值单位为微米(1000微米=1毫米);
public static string Y轴当前位置a = "5580";// 读取的值单位为微米(1000微米=1毫米);
public static string Z轴当前位置a = "5620"; //读取的值单位为微米(1000微米=1毫米);
//当前位置值读取:
public static int R轴当前位置v = 0; //读取的值单位为毫度(1000毫度=1度);
public static int X轴当前位置v = 2160000; //读取的值单位为微米(1000微米=1毫米);
public static int Y轴当前位置v = 0;// 读取的值单位为微米(1000微米=1毫米);
public static int Z轴当前位置v = 200000; //读取的值单位为微米(1000微米=1毫米);
//中心位置:
public static string R轴中心位置v = "0"; //读取的值单位为毫度(1000毫度=1度);
public static string X轴中心位置v = "200000"; //读取的值单位为微米(1000微米=1毫米);
public static string Y轴中心位置v = "200000";// 读取的值单位为微米(1000微米=1毫米);
public static string Z轴中心位置v = "200000"; //读取的值单位为微米(1000微米=1毫米);
//原点位置:
public static string R轴原点位置v = "0"; //读取的值单位为毫度(1000毫度=1度);
public static string X轴原点位置v = "1600000"; //读取的值单位为微米(1000微米=1毫米);
public static string Y轴原点位置v = "-400000";// 读取的值单位为微米(1000微米=1毫米);
public static string Z轴原点位置v = "230000"; //读取的值单位为微米(1000微米=1毫米);
//相对运动位置:
public static string R轴相对运动坐标写入a = "2048"; //写入的值单位为毫度(1000毫度=1度);
public static string X轴相对运动坐标写入a = "2052"; //写入的值单位为微米(1000微米=1毫米);
public static string Y轴相对运动坐标写入a = "2056"; //写入的值单位为微米(1000微米=1毫米);
public static string Z轴相对运动坐标写入a = "2060"; //写入的值单位为微米(1000微米=1毫米);
//相对运动位置:
public static int R轴相对运动坐标写入v = 50000; //写入的值单位为毫度(1000毫度=1度);
public static int X轴相对运动坐标写入v = 50000; //写入的值单位为微米(1000微米=1毫米);
public static int Y轴相对运动坐标写入v = 50000; //写入的值单位为微米(1000微米=1毫米);
public static int Z轴相对运动坐标写入v = 50000; //写入的值单位为微米(1000微米=1毫米);
//相对运动启动:
public static string R轴相对运动启动a = "2340"; //R轴相对运动坐标写入后,在此写入”1“确认后设备启动运行;
public static string X轴相对运动启动a = "2342"; // R轴相对运动坐标写入后,在此写入”1“确认后设备启动运行;
public static string Y轴相对运动启动a = "2344"; // R轴相对运动坐标写入后,在此写入”1“确认后设备启动运行;
public static string Z轴相对运动启动a = "2346"; // R轴相对运动坐标写入后,在此写入”1“确认后设备启动运行;
//相对运动完成:
public static string R轴相对运动完成a = "2350"; //读取到Ture或1代表运动完成,读取后需要写入0;
public static string X轴相对运动完成a = "2352"; // 读取到Ture或1代表运动完成;
public static string Y轴相对运动完成a = "2354"; // 读取到Ture或1代表运动完成;
public static string Z轴相对运动完成a = "2356"; // 读取到Ture或1代表运动完成;
//复位:
public static string 复位写入 = "2348";// 若需要复位在此写入”1“,X轴、R轴、Y轴、Z轴会依次复位到PLC默认零点位。
public static string R轴复位完成 = "165"; //读取到Ture或1代表复位完成;
public static string X轴复位完成 = "159"; //读取到Ture或1代表复位完成;
public static string Y轴复位完成 = "161"; //读取到Ture或1代表复位完成;
public static string Z轴复位完成 = "163";// 读取到Ture或1代表复位完成;
//复位位置设置:
public static string X轴复位位置a= "2324";
public static string Y轴复位位置a = "2328";
public static string Z轴复位位置a = "2332";
public static string R轴复位位置a = "XXXX"; //缺
public static string X轴复位位置v = "100000";
public static string Y轴复位位置v = "100000";
public static string Z轴复位位置v = "100000";
public static bool PLC_Used = true; //是否启用PLC
public static string PLC_IP = "192.168.1.7"; //PLC IP地址
public static string PLC_Port = "502"; //PLC 端口 默认:502
public static string PLC_Station = "1"; //PLC 站号 默认:1
public static string PLC_StartTest_Register = "5000"; //PLC控制是否测试可以开始 0不测试 1 测试
public static string PLC_LetTVPass_Register = "5001";
public static string PLC_Type_Register = "5002";
public static string PLC_SN_Register = "5022"; //PLC读取sn地址
public static string PLC_Adapter_Register = "4099";
public static string PLC_Light_Register = "4119"; //PLC红绿黄灯控制寄存器地址
public static string PLC_Light_GREEN = "1";
public static string PLC_Light_RED = "2";
public static string PLC_Light_YELLOW = "3";
public static string ShakeHand_OK_Code = "2"; //
public static string config_file_path = "config_modbus.json"; //
public config_json()
{
}
public static bool save(string key, string value)
{
try
{
string json = System.IO.File.ReadAllText(config_json.config_file_path);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
jsonObj[key] = value;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(config_json.config_file_path, output);
return true;
}
catch
{
return false;
}
}
public static bool config_json_readall()
{
if (System.IO.File.Exists(config_json.config_file_path) == false)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(config_json.config_file_path, true))
{
writer.WriteLine("{}");
writer.Close();
}
}
System.IO.StreamReader file = System.IO.File.OpenText(config_json.config_file_path);
Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(file);
Newtonsoft.Json.Linq.JObject jsonObject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.ReadFrom(reader);
if (jsonObject["R轴当前位置a"] != null) R轴当前位置a = (string)jsonObject["R轴当前位置a"];
if (jsonObject["X轴当前位置a"] != null) X轴当前位置a = (string)jsonObject["X轴当前位置a"];
if (jsonObject["Y轴当前位置a"] != null) Y轴当前位置a = (string)jsonObject["Y轴当前位置a"];
if (jsonObject["Z轴当前位置a"] != null) Z轴当前位置a = (string)jsonObject["Z轴当前位置a"];
if (jsonObject["X轴原点位置v"] != null) X轴原点位置v = (string)jsonObject["X轴原点位置v"];
if (jsonObject["Y轴原点位置v"] != null) Y轴原点位置v = (string)jsonObject["Y轴原点位置v"];
if (jsonObject["Z轴原点位置v"] != null) Z轴原点位置v = (string)jsonObject["Z轴原点位置v"];
if (jsonObject["X轴中心位置v"] != null) X轴中心位置v = (string)jsonObject["X轴中心位置v"];
if (jsonObject["X轴中心位置v"] != null) Y轴中心位置v = (string)jsonObject["Y轴中心位置v"];
if (jsonObject["Z轴中心位置v"] != null) Z轴中心位置v = (string)jsonObject["Z轴中心位置v"];
if (jsonObject["R轴相对运动坐标写入"] != null) R轴相对运动坐标写入a = (string)jsonObject["R轴相对运动坐标写入"];
if (jsonObject["X轴相对运动坐标写入"] != null) X轴相对运动坐标写入a = (string)jsonObject["X轴相对运动坐标写入"];
if (jsonObject["Y轴相对运动坐标写入"] != null) Y轴相对运动坐标写入a = (string)jsonObject["Y轴相对运动坐标写入"];
if (jsonObject["Z轴相对运动坐标写入"] != null) Z轴相对运动坐标写入a = (string)jsonObject["Z轴相对运动坐标写入"];
if (jsonObject["R轴相对运动启动"] != null) R轴相对运动启动a = (string)jsonObject["R轴相对运动启动"];
if (jsonObject["X轴相对运动启动"] != null) X轴相对运动启动a = (string)jsonObject["X轴相对运动启动"];
if (jsonObject["Y轴相对运动启动"] != null) Y轴相对运动启动a = (string)jsonObject["Y轴相对运动启动"];
if (jsonObject["Z轴相对运动启动"] != null) Z轴相对运动启动a = (string)jsonObject["Z轴相对运动启动"];
if (jsonObject["R轴相对运动完成"] != null) R轴相对运动完成a = (string)jsonObject["R轴相对运动完成"];
if (jsonObject["X轴相对运动完成"] != null) X轴相对运动完成a = (string)jsonObject["X轴相对运动完成"];
if (jsonObject["Y轴相对运动完成"] != null) Y轴相对运动完成a = (string)jsonObject["Y轴相对运动完成"];
if (jsonObject["Z轴相对运动完成"] != null) Z轴相对运动完成a = (string)jsonObject["Z轴相对运动完成"];
if (jsonObject["复位写入"] != null) 复位写入 = (string)jsonObject["复位写入"];
if (jsonObject["R轴复位完成"] != null) R轴复位完成 = (string)jsonObject["R轴复位完成"];
if (jsonObject["X轴复位完成"] != null) X轴复位完成 = (string)jsonObject["X轴复位完成"];
if (jsonObject["Y轴复位完成"] != null) Y轴复位完成 = (string)jsonObject["Y轴复位完成"];
if (jsonObject["Z轴复位完成"] != null) Z轴复位完成 = (string)jsonObject["Z轴复位完成"];
if (jsonObject["X轴复位位置"] != null) X轴复位位置a = (string)jsonObject["X轴复位位置"];
if (jsonObject["Y轴复位位置"] != null) Y轴复位位置a = (string)jsonObject["Y轴复位位置"];
if (jsonObject["Z轴复位位置"] != null) Z轴复位位置a = (string)jsonObject["Z轴复位位置"];
if (jsonObject["X轴复位位置v"] != null) X轴复位位置v = (string)jsonObject["X轴复位位置v"];
if (jsonObject["Y轴复位位置v"] != null) Y轴复位位置v = (string)jsonObject["Y轴复位位置v"];
if (jsonObject["Z轴复位位置v"] != null) Z轴复位位置v = (string)jsonObject["Z轴复位位置v"];
if (jsonObject["PLC_IP"] != null) PLC_IP = (string)jsonObject["PLC_IP"];
if (jsonObject["PLC_Port"] != null) PLC_Port = (string)jsonObject["PLC_Port"];
if (jsonObject["PLC_Station"] != null) PLC_Station = (string)jsonObject["PLC_Station"];
if (jsonObject["PLC_Light_Register"] != null) PLC_Light_Register = (string)jsonObject["PLC_Light_Register"];
if (jsonObject["PLC_Light_GREEN"] != null) PLC_Light_GREEN = (string)jsonObject["PLC_Light_GREEN"];
if (jsonObject["PLC_Light_RED"] != null) PLC_Light_RED = (string)jsonObject["PLC_Light_RED"];
if (jsonObject["PLC_Light_YELLOW"] != null) PLC_Light_YELLOW = (string)jsonObject["PLC_Light_YELLOW"];
if (jsonObject["PLC_Type_Regist"] != null) PLC_Type_Register = (string)jsonObject["PLC_Type_Regist"];
if (jsonObject["PLC_SN_Register"] != null) PLC_SN_Register = (string)jsonObject["PLC_SN_Register"];
if (jsonObject["PLC_StartTest_Register"] != null) PLC_StartTest_Register = (string)jsonObject["PLC_StartTest_Register"];
file.Close();
return true;
}
/// <summary>
/// 创建配置文件,使用默认值
/// </summary>
/// <returns></returns>
public static bool config_json_create()
{
System.IO.StringWriter sw = new System.IO.StringWriter();
using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
{
string newline = "";// System.Environment.NewLine;
writer.Formatting = Newtonsoft.Json.Formatting.Indented;
writer.WriteStartObject();
//是否运行程序后自动开始测试
writer.WritePropertyName("AUTO_RUN");
//writer.WriteValue("false");
writer.WriteValue("false");
writer.WriteComment(newline + "是否运行程序后自动开始测试");
writer.WritePropertyName("PLC_IP");
writer.WriteValue("192.168.1.5");
writer.WriteComment(newline + "PLC控制器设置");
writer.WritePropertyName("PLC_Port");
writer.WriteValue("502");
writer.WritePropertyName("PLC_Station");
writer.WriteValue("1");
writer.WritePropertyName("PLC_Control_Register");
writer.WriteValue("4096");
writer.WritePropertyName("PLC_Light_Register");
writer.WriteValue("4097");
writer.WriteEndObject();
writer.Flush();
}
sw.Close();
//写入文件
string file_content = sw.GetStringBuilder().ToString();
string filePath = "config.json";//这里是你的已知文件
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create,
System.IO.FileAccess.ReadWrite);
System.IO.StreamWriter stwr = new System.IO.StreamWriter(fs);
fs.SetLength(0);//首先把文件清空了。
stwr.Write(file_content);//写入内容。
stwr.Close();
return true;
}
}
}