public void DisconnectAllDevice() { foreach (var kp in SV.DeviceDictonary) { DeviceProxy deviceProxy = kp.Value; if (deviceProxy.IsConnected) { int status = deviceProxy.Disconnect(); if (status == 0) { deviceProxy.IsConnected = false; } else { Debug.WriteLine("设备关闭失败。"); } } } }
/// <summary> /// 点击保存按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { #region 判断数据有效性 // 检测后台是不是正在运行 if (SV.DeviceDictonary.Any(p => p.Value.IsConnected == true)) { var warning = MessageBox.Show(this, "设备正在运行,修改IP地址会停止设备,要继续吗?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (warning != DialogResult.Yes) { return; } } #region 停止设备连接 foreach (var dic in SV.DeviceDictonary) { DeviceProxy dev = dic.Value; if (dev.IsConnected == true) { dev.Disconnect(); dev.IsConnected = false; } } #endregion if (!CheckInput()) { return; } #endregion // 保存第一个 if (txtIP1.IPAddress != IPAddress.None) { DeviceSetting entity1 = new DeviceSetting(); entity1.DeviceIpAddress = txtIP1.IPAddress.ToString(); entity1.DevicePort = txtPort1.Text; entity1.Usage = POCO.DeviceUsageEnum.入库; svc.SaveOrUpdate(entity1); if (SettingUpdated != null) { SettingUpdated(entity1); } } // 保存第二个 if (txtIP2.IPAddress != IPAddress.None) { DeviceSetting entity2 = new DeviceSetting(); entity2.DeviceIpAddress = txtIP2.IPAddress.ToString(); entity2.DevicePort = txtPort2.Text; entity2.Usage = POCO.DeviceUsageEnum.洗涤; svc.SaveOrUpdate(entity2); if (SettingUpdated != null) { SettingUpdated(entity2); } } // 保存第三个 if (txtIP3.IPAddress != IPAddress.None) { DeviceSetting entity3 = new DeviceSetting(); entity3.DeviceIpAddress = txtIP3.IPAddress.ToString(); entity3.DevicePort = txtPort3.Text; entity3.Usage = POCO.DeviceUsageEnum.烘干; svc.SaveOrUpdate(entity3); if (SettingUpdated != null) { SettingUpdated(entity3); } } // 保存第四个 if (txtIP4.IPAddress != IPAddress.None) { DeviceSetting entity4 = new DeviceSetting(); entity4.DeviceIpAddress = txtIP4.IPAddress.ToString(); entity4.DevicePort = txtPort4.Text; entity4.Usage = POCO.DeviceUsageEnum.出库; svc.SaveOrUpdate(entity4); if (SettingUpdated != null) { SettingUpdated(entity4); } } MessageBox.Show("保存成功"); }