/// <summary> /// 从json配置文件中导入上次默认配置 /// </summary> /// <param name="my_Config">获取json配置的对象</param> private void Json_load(My_config my_Config) { //从json配置文件中导入上次默认配置 comboBox1.Text = my_Config.Serial_config.intBaudRate; comboBox2.Text = my_Config.Serial_config.intDataBits + " bit"; comboBox3.Text = my_Config.Serial_config.intStopBits + " bit"; comboBox4.Text = my_Config.Serial_config.intParity; }
/// <summary> /// 写入数据到当前路径的Config_json.json文件里 /// </summary> /// <param name="my_config">符合固定模型的数据</param> /// <returns>成功写入返回true</returns> public static bool WriteFile(My_config my_config) { string reader = JsonConvert.SerializeObject(my_config); //Console.WriteLine("hahaha:" + reader); try { File.WriteAllText(srpath, reader, Encoding.UTF8); return(true); } catch (Exception e) { Console.WriteLine("失败原因:" + e.Message); return(false); } }
/// <summary> /// 颜色选择水绿色后把校验项写入json /// </summary> /// <param name="i"></param> private void Json_write_check_value(int i) { My_config my_Config = ReadFile(); var config = new My_config(); var checkCode_value = new List_format(); checkCode_value = Add_check_value(this.listView1, checkCode_value, listview_check_value, i); //赋值config config = new My_config { Command_config = my_Config.Command_config, Serial_config = my_Config.Serial_config, Command_Name = my_Config.Command_Name, List_Name = my_Config.List_Name, List_Value = my_Config.List_Value, CheckCode_value = checkCode_value }; //把config烧写进json文件 WriteFile(config); //调用WriteFile方法把配置写入json文件中 }
/// <summary> /// 删除最新的1列数据并保存在json中 /// </summary> private void Json_clean() { My_config my_Config = ReadFile(); var config = new My_config(); var list_Name = new List_format(); var list_Value = new List_format(); var checkCode_value = new List_format(); //listview 列表表头 list_Name = Clean_config_Columns_listView(this.listView1, list_Name, listview_name); //listview 列表表值 list_Value = Clean_config_Value_listView(this.listView1, list_Value, listview_value); //校验内容 checkCode_value = Clean_check_value(this.listView1, checkCode_value, listview_check_value); //赋值config config = new My_config { Command_config = my_Config.Command_config, Serial_config = my_Config.Serial_config, Command_Name = my_Config.Command_Name, List_Name = list_Name, List_Value = list_Value, CheckCode_value = checkCode_value }; //把config烧写进json文件 WriteFile(config); //调用WriteFile方法把配置写入json文件中 for (int i = 0; i < listview_check_value.Count; i++) { if (listview_check_value[i] != null) { this.listView1.Items[0].SubItems[i + 1].BackColor = Color.Aqua; } } }
/// <summary> /// 把配置写入json文件的方法 /// </summary> private void Json_write() { var config = new My_config(); var serial_config = new Serial_config(); serial_config = new Serial_config() { intBaudRate = comboBox1.Text, intDataBits = comboBox2.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0], intStopBits = comboBox3.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0], intParity = comboBox4.Text }; config = new My_config { Command_config = my_Config.Command_config, Serial_config = serial_config, Command_Name = my_Config.Command_Name, List_Name = my_Config.List_Name, List_Value = my_Config.List_Value, CheckCode_value = my_Config.CheckCode_value }; WriteFile(config); }
/// <summary> /// 把新配置写入json文件中 /// </summary> private void Json_write() { My_config my_Config = ReadFile(); var config = new My_config(); var command_config = new Command_config(); var FrameHead = new General_format(); var FrameTail = new General_format(); var FrameData = new General_format(); var FrameCommand = new General_format(); var FrameOne = new General_format(); var FramTwo = new General_format(); var FramThree = new General_format(); var command_name = new General_format(); var list_Name = new List_format(); var list_Value = new List_format(); //listview 列表表头 list_Name = Add_config_Columns_listView(this.listView1, list_Name, listview_name, this.comboBox1); //listview 列表表值 list_Value = Add_config_Value_listView(this.listView1, list_Value, listview_value, this.comboBox2); //选项名 //根据实际个数来写入配置:选项名称 中 switch (comboBox1.SelectedIndex) { //选择“帧头”选项时 case 1: FrameHead = Add_config_cmb(comboBox2, FrameHead, list_FrameHead); command_config = new Command_config { strFrameHead = FrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“时序”的时候,显示框内容就是时序位 case 2: command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = this.comboBox2.Text, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“命令码”的时候 case 3: FrameCommand = Add_config_cmb(comboBox2, FrameCommand, list_FrameCommand); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = FrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“数据”的时候 case 4: FrameData = Add_config_cmb(comboBox2, FrameData, list_FrameData); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = FrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“校验”的时候 case 5: command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = comboBox2.Text, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“帧尾”的时候 case 6: FrameTail = Add_config_cmb(comboBox2, FrameTail, list_FrameTail); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = FrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“预留1”的时候 case 7: FrameOne = Add_config_cmb(comboBox2, FrameOne, list_FrameOne); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = FrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“预留2”的时候 case 8: FramTwo = Add_config_cmb(comboBox2, FramTwo, list_FrameTwo); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = FramTwo, strFramThree = my_Config.Command_config.strFramThree }; break; //选择“预留3”的时候 case 9: FramThree = Add_config_cmb(comboBox2, FramThree, list_FramThree); command_config = new Command_config { strFrameHead = my_Config.Command_config.strFrameHead, strFrameTail = my_Config.Command_config.strFrameTail, strFrameCheckCode = my_Config.Command_config.strFrameCheckCode, intFrameSequential = my_Config.Command_config.intFrameSequential, strFrameData = my_Config.Command_config.strFrameData, strFrameCommand = my_Config.Command_config.strFrameCommand, strFrameOne = my_Config.Command_config.strFrameOne, strFramTwo = my_Config.Command_config.strFramTwo, strFramThree = FramThree }; break; } //选项名称 command_name = Add_config_cmb(comboBox1, command_name, list_name, 6); //赋值config config = new My_config { Command_config = command_config, Serial_config = my_Config.Serial_config, Command_Name = command_name, List_Name = list_Name, List_Value = list_Value, CheckCode_value = my_Config.CheckCode_value }; //把config烧写进json文件 if (!WriteFile(config)) //调用WriteFile方法把配置写入json文件中 { MessageBox.Show("保存失败", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } //写完数据后显示已经标记的颜色 for (int i = 0; i < listview_check_value.Count; i++) { if (listview_check_value[i] != null) { this.listView1.Items[0].SubItems[i + 1].BackColor = Color.Aqua; } } }
/// <summary> /// 从json配置文件中导入上次默认配置 /// </summary> /// <param name="my_Config">获取json配置的对象</param> private void Json_load() { My_config my_Config = ReadFile(); //load 选项名 list_name = new List <string>(new string[] { "请输入..", "帧头", "时序", "命令码", "数据", "校验", "帧尾", my_Config.Command_Name.strOtherOne, my_Config.Command_Name.strOtherTwo, my_Config.Command_Name.strOtherThree }); /*_list = _list.Concat(list_name).ToList(); */ Combox_add(this.comboBox1, list_name); this.comboBox1.SelectedIndex = 0; //load 各个选项的值 //帧头: //list_FrameHead.Add("请输入.."); list_FrameHead = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFrameHead.strOtherOne, my_Config.Command_config.strFrameHead.strOtherTwo, my_Config.Command_config.strFrameHead.strOtherThree }); //帧尾 //list_FrameTail.Add("请输入.."); list_FrameTail = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFrameTail.strOtherOne, my_Config.Command_config.strFrameTail.strOtherTwo, my_Config.Command_config.strFrameTail.strOtherThree }); //数据 //list_FrameData.Add("请输入.."); list_FrameData = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFrameData.strOtherOne, my_Config.Command_config.strFrameData.strOtherTwo, my_Config.Command_config.strFrameData.strOtherThree }); //命令码 //list_FrameCommand.Add("请输入.."); list_FrameCommand = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFrameCommand.strOtherOne, my_Config.Command_config.strFrameCommand.strOtherTwo, my_Config.Command_config.strFrameCommand.strOtherThree }); //预留1 //list_FrameOne.Add("请输入.."); list_FrameOne = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFrameOne.strOtherOne, my_Config.Command_config.strFrameOne.strOtherTwo, my_Config.Command_config.strFrameOne.strOtherThree }); //预留2 //list_FrameTwo.Add("请输入.."); list_FrameTwo = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFramTwo.strOtherOne, my_Config.Command_config.strFramTwo.strOtherTwo, my_Config.Command_config.strFramTwo.strOtherThree }); //预留3 //list_FramThree.Add("请输入.."); list_FramThree = new List <string>(new string[] { "请输入..", my_Config.Command_config.strFramThree.strOtherOne, my_Config.Command_config.strFramThree.strOtherTwo, my_Config.Command_config.strFramThree.strOtherThree }); //listview表头名 listview_name = new List <string>(new string[] { "报文名:", my_Config.List_Name.one, my_Config.List_Name.two, my_Config.List_Name.three, my_Config.List_Name.four, my_Config.List_Name.five, my_Config.List_Name.six, my_Config.List_Name.seven, my_Config.List_Name.eight, my_Config.List_Name.nine, my_Config.List_Name.ten }); //listview表内容 listview_value = new List <string>(new string[] { "数据值:", my_Config.List_Value.one, my_Config.List_Value.two, my_Config.List_Value.three, my_Config.List_Value.four, my_Config.List_Value.five, my_Config.List_Value.six, my_Config.List_Value.seven, my_Config.List_Value.eight, my_Config.List_Value.nine, my_Config.List_Value.ten }); if (listview_name != null) { Listview_Columns_add(this.listView1, listview_name); } if (listview_value != null) { Listview_value_add(this.listView1, listview_value); } listview_check_value = new List <string>(new string[] { my_Config.CheckCode_value.one, my_Config.CheckCode_value.two, my_Config.CheckCode_value.three, my_Config.CheckCode_value.four, my_Config.CheckCode_value.five, my_Config.CheckCode_value.six, my_Config.CheckCode_value.seven, my_Config.CheckCode_value.eight, my_Config.CheckCode_value.nine, my_Config.CheckCode_value.ten }); for (int i = 0; i < listview_check_value.Count; i++) { if (listview_check_value[i] != null) { this.listView1.Items[0].SubItems[i + 1].BackColor = Color.Aqua; } } }