using HslCommunication; 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.Text; 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 = "255.255.255.0"; SubnetMask.Text = "192.168.1.1"; } private 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 = "0110" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; if (FixedIP.Checked == true) { ARD2 = "0001"; SendText = "0110" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; } else { ARD2 = "0000"; SendText = "0110" + ARD + "0007" + "0e" + ipone + iptwo + wgone + wgtwo + zwone + zwtwo + ARD2; } OperateResult read = Form1.f1.modbusClient.ReadFromCoreServer(HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(SendText)); if (read.IsSuccess) { Form1.f1.Code.Text = "结果:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content, ' '); } else { MessageBox.Show("读取失败:" + read.ToMessageShowString()); } } catch (Exception ex) { MessageBox.Show("读取失败:" + ex.Message); } } } }