using HslCommunication; using HslCommunication.ModBus; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection.Emit; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace C_Windows_1 { public partial class configIP : Form { public configIP() { InitializeComponent(); } private void configIP_Load(object sender, EventArgs e) { IPAddress.Text = "192.168.1.XXX"; IPGateway.Text = "192.168.1.1"; SubnetMask.Text = "255.255.255.0"; } private async void Save_Click(object sender, EventArgs e) { try { string str = IPAddress.Text; string[] arr = str.Split('.'); string a1 = arr[0]; string a2 = arr[1]; string a3 = arr[2]; string a4 = arr[3]; string ipone = Convert.ToInt32(a1).ToString("x2") + Convert.ToInt32(a2).ToString("x2"); string iptwo = Convert.ToInt32(a3).ToString("x2") + Convert.ToInt32(a4).ToString("x2"); //IP网关分段 string WG = IPGateway.Text; string[] WGS = WG.Split('.'); string wg1 = WGS[0]; string wg2 = WGS[1]; string wg3 = WGS[2]; string wg4 = WGS[3]; //string wgone = wg1 + wg2; //string wgtwo = wg3 + wg4; string wgone = Convert.ToInt32(wg1).ToString("x2") + Convert.ToInt32(wg2).ToString("x2"); string wgtwo = Convert.ToInt32(wg3).ToString("x2") + Convert.ToInt32(wg4).ToString("x2"); //子网掩码分段 string ZW = SubnetMask.Text; string[] zws = ZW.Split('.'); string zw1 = zws[0]; string zw2 = zws[1]; string zw3 = zws[2]; string zw4 = zws[3]; //string zwone = zw1 + zw2; //string zwtwo = zw3 + zw4; string zwone = Convert.ToInt32(zw1).ToString("x2") + Convert.ToInt32(zw2).ToString("x2"); string zwtwo = Convert.ToInt32(zw3).ToString("x2") + Convert.ToInt32(zw4).ToString("x2"); //写入IP地址 int a = 435; string ARD = "0435"; string ARD2 = "0000"; string SendText = "01" + "10" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; if (FixedIP.Checked == true) { ARD2 = "0001"; SendText = Form1.station+"10" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; } else { ARD2 = "0000"; SendText = Form1.station+"10" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; } await Form1.f1.modbusClient.ReadAsync("0", (ushort)1); OperateResult read =await Form1.f1.modbusClient.ReadFromCoreServerAsync(HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(SendText)); if (read.IsSuccess) { // Form1.f1.Code.Text = "结果:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content, ' '); //Form1.f1.connectflag = 0; //Form1.f1.modbusClient.ConnectClose(); ////Thread.Sleep(200); //Form1.f1.modbusClient = new ModbusRtuOverTcp(IPAddress.Text, 9999, Convert.ToByte(Form1.station2)); //Thread.Sleep(200); //if (Form1.f1.modbusClient.ConnectServer().IsSuccess) //{ // Form1.f1.connectflag = 1; // Form1.f1.TCPconnect.Enabled = false; // Form1.f1.PortNum.Text = Form1.f1.modbusClient.IpAddress; // Form1.f1.label6.Text = Form1.f1.modbusClient.Port.ToString(); // Form1.f1.UDPResults.Text = Form1.station2.ToString() + ":" + IPAddress.Text; // Form1.f1.label6.ForeColor = Color.Green; // MessageBox.Show("IP修改成功"); //} Form1.f1.connectflag = 0; Form1.f1.TCPconnect.Enabled = true; Form1.f1.PortNum.Text = Form1.f1.modbusClient.IpAddress; Form1.f1.label6.Text = Form1.f1.modbusClient.Port.ToString(); Form1.f1.UDPResults.Text = Form1.station2.ToString() + ":" + IPAddress.Text; Form1.f1.label6.ForeColor = Color.Red; MessageBox.Show("IP修改成功"); } else { MessageBox.Show("读取失败:" + read.ToMessageShowString()); } } catch (Exception ex) { MessageBox.Show("读取失败:" + ex.Message); } } } }