}//关闭设备 private async void BtnAddZigBee_Click(object sender, RoutedEventArgs e) { #region 配置串口 var selection = ConnectDevices.SelectedItems; if (selection.Count <= 0) { return; } DeviceInformation entry = (DeviceInformation)selection[0]; try { serialPort = await SerialDevice.FromIdAsync(entry.Id); if (serialPort == null) { return; } serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 9600; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 8; serialPort.Handshake = SerialHandshake.None; // Create cancellation token object to close I/O operations when closing the device ReadCancellationTokenSource = new CancellationTokenSource(); Listen(); } catch (Exception) { } #endregion DeviceModel Device = new DeviceModel { NetPort = await DevicesMethod.GetPortNum() + 1, Type = "照明系统", IPAddress = "Zigbee设备" }; ViewDevModel DevObj = ModelConverter.DataToView(Device); Obj.Add(Device); DevicesMethod.UpdateDevices(Obj); gridView.Items.Add(DevObj); CreateLine(Device.NetPort);//绑定新通道 btnAddZigBee.IsEnabled = false; }
}//向云上传数据 #endregion #region 设备模型相关 private async void BtnAdd_Click(object sender, RoutedEventArgs e) { string[] data = cmbIp.SelectedItem.ToString().Trim().Split(Convert.ToChar("/")); DeviceModel Device = new DeviceModel { NetPort = await DevicesMethod.GetPortNum() + 1, Type = data[1], IPAddress = data[0] }; ViewDevModel DevObj = ModelConverter.DataToView(Device); Obj.Add(Device); DevicesMethod.UpdateDevices(Obj); gridView.Items.Add(DevObj); CreateLine(Device.NetPort); //绑定新通道 SendAllow(Device.NetPort); //发送端口允许 }
}//左键选择,暂时取消 private async void GridView_RightTapped(object sender, RightTappedRoutedEventArgs e) { var Info = (e.OriginalSource as FrameworkElement)?.DataContext as ViewDevModel; DeviceModel DataInfo = ModelConverter.ViewToData(Info); ContentDialog detail_dialog = new ContentDialog() { Title = DataInfo.Type + "设备详情:", Content = "设备地址:" + DataInfo.IPAddress + ":" + DataInfo.NetPort, PrimaryButtonText = "发送命令", SecondaryButtonText = "删除设备", CloseButtonText = "返回", FullSizeDesired = true, }; ContentDialogResult Reslut = await detail_dialog.ShowAsync(); if (Reslut == ContentDialogResult.Primary) { if (DataInfo.IPAddress == "Zigbee设备")//关闭串口服务 { WriteCmdByte(new byte[] { 0x02 }); //SendCmd(Convert.ToInt16("9003"), "0000+B"); } else { if (FanFlag == false) { SendCmd(Convert.ToInt16(DataInfo.NetPort), "0000+A"); FanFlag = true; } else { SendCmd(Convert.ToInt16(DataInfo.NetPort), "0000+B"); FanFlag = false; } } //SendCmd(DataInfo.NetPort, "这是一条测试信息/"); } if (Reslut == ContentDialogResult.Secondary) { if (DataInfo.IPAddress == "Zigbee设备")//关闭串口服务 { try { CancelReadTask(); CloseDevice(); ListAvailablePorts(); } catch (Exception) { } btnAddZigBee.IsEnabled = true; } DisposeLine(DataInfo.NetPort);//移除监听服务 for (int i = 0; i < Obj.Count; i++) { if (Obj[i].NetPort == DataInfo.NetPort) { Obj.Remove(Obj[i]); } } gridView.Items.Remove(Info); DevicesMethod.UpdateDevices(Obj); } }//右键选择