using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.WebRequestMethods; namespace Welling_Motor_Debug_Tool { public partial class GenerateParams : Form { //存储路径文件 LocalInfo localInfo = new LocalInfo(); //服务器配置 string IP, Port, User, PassWD, ModelPath, CfgPath; //FTP ftp myFtp = new ftp(); public GenerateParams() { InitializeComponent(); } private void button_NoteRead_Click(object sender, EventArgs e) { //检查本地文件删除 if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName)) System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName); //下载远程文件 myFtp.DownloadFile("/Note/Note.txt", localInfo.LocalPath); //显示文件 StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.NoteFileName); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } objReader.Close(); richTextBox_Note.Clear(); foreach (string sOutput in arrText) { richTextBox_Note.AppendText(sOutput + "\r\n"); } richTextBox_Note.Text = richTextBox_Note.Text.Remove(richTextBox_Note.Text.Length - 1); } private void button_NoteWrite_Click(object sender, EventArgs e) { //检查本地并删除 if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName)) System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName); //保存 System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.NoteFileName, richTextBox_Note.Text); //上传服务器 if (!myFtp.DirectoryExist("/", "Note")) myFtp.MakeDir("/Note"); bool result = myFtp.UploadFile(localInfo.LocalPath + localInfo.NoteFileName, "/Note"); if (result) MessageBox.Show("上传完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("上传失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { //选中备忘录时自动读取更新 if (tabControl1.SelectedIndex == 2) { //检查本地文件删除 if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName)) System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName); //下载远程文件 myFtp.DownloadFile("/Note/Note.txt", localInfo.LocalPath); //显示文件 StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.NoteFileName); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } objReader.Close(); richTextBox_Note.Clear(); foreach (string sOutput in arrText) { richTextBox_Note.AppendText(sOutput + "\r\n"); } richTextBox_Note.Text = richTextBox_Note.Text.Remove(richTextBox_Note.Text.Length - 1); } } private void GenerateParams_Load(object sender, EventArgs e) { //导入网络配置 if (System.IO.File.Exists(localInfo.LocalPath + localInfo.ConfigFileName)) //存在配置文件,导入配置信息 { //打开文件 StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.ConfigFileName); string sLine = ""; ArrayList array_CfgInfo = new ArrayList(); array_CfgInfo.Clear(); while (sLine != null) { sLine = objReader.ReadLine(); array_CfgInfo.Add(sLine); } objReader.Close(); //解析配置文件 try { //Server Set IP, Port, User, PassWS, ModelPath; IP = array_CfgInfo[12].ToString().Split(':')[1]; Port = array_CfgInfo[13].ToString().Split(':')[1]; User = array_CfgInfo[14].ToString().Split(':')[1]; PassWD = array_CfgInfo[15].ToString().Split(':')[1]; ModelPath = "ParamsMode"; myFtp.FtpOption(IP, Port, User, PassWD); } catch (System.Exception) { MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("参数文件丢失!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); } //检查网络 if (myFtp.CheckFtp() == false) { label_Server_ComStatus.Text = "网络已断开"; label_ServerStatus.BackColor = Color.Red; MessageBox.Show("网络断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.Close(); } else { label_Server_ComStatus.Text = "网络已连接"; label_ServerStatus.BackColor = Color.Green; } //加载模板文件 comboBox_ModelFile.Items.Clear(); string[] FileList = { "" }; FileList = myFtp.GetFileNameList(ModelPath); comboBox_ModelFile.Items.Clear(); foreach (var file in FileList) { if (file.Contains(".src")) { comboBox_ModelFile.Items.Add(file.Substring(file.LastIndexOf("\\") + 1)); } } } private void button_ModeRefresh_Click(object sender, EventArgs e) { //加载模板文件 comboBox_ModelFile.Items.Clear(); string[] FileList = { "" }; FileList = myFtp.GetFileNameList(ModelPath); comboBox_ModelFile.Items.Clear(); foreach (var file in FileList) { if (file.Contains(".src")) { comboBox_ModelFile.Items.Add(file.Substring(file.LastIndexOf("\\") + 1)); } } } private void comboBox_ModelFile_SelectedIndexChanged(object sender, EventArgs e) { button_ExportParams.Enabled = true; //下载文件 if (System.IO.File.Exists(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text))//本地存在选定文件,先删除本地 { System.IO.File.Delete(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text); } myFtp.DownloadFile("ParamsMode/" + comboBox_ModelFile.Text, localInfo.LocalPath); //打开文件 StreamReader objReader = new StreamReader(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } objReader.Close(); //加载文件 do //基础参数界面 { //建立字典,存放并获取每个命令的行号 Dictionary DicRowNum = new Dictionary(); DicRowNum.Add("[整车参数]", 0); DicRowNum.Add("[控制参数]", 0); DicRowNum.Add("[助力参数]", 0); DicRowNum.Add("[调试参数]", 0); DicRowNum.Add("[生产信息]", 0); Dictionary __dict = new Dictionary(DicRowNum); foreach (KeyValuePair item in __dict) { foreach (string cmd in arrText) { if (cmd.Contains(item.Key)) { DicRowNum[item.Key] = arrText.IndexOf(cmd); break; } } } //遍历加载 foreach (string index in DicRowNum.Keys) { int rowNum = DicRowNum[index]; //行号 switch (index) { case "[整车参数]": { //轮胎周长 textBox_BikeWheel.Text = arrText[rowNum + 2].ToString().Split(',')[1]; //转把限速 textBox_BikeThroSpeed.Text = arrText[rowNum + 4].ToString().Split(',')[1]; //推行限速 textBox_BikeWalkSpeed.Text = arrText[rowNum + 5].ToString().Split(',')[1]; //前牙盘 textBox_BikeFrontT.Text = arrText[rowNum + 6].ToString().Split(',')[1]; //后牙盘 textBox_BikeRealB.Text = arrText[rowNum + 7].ToString().Split(',')[1]; //前灯电压 ushort LightParams = Convert.ToUInt16(arrText[rowNum + 10].ToString().Split(',')[1]); if ((LightParams & 0x00FF) == 6) comboBox_BikeVolF.SelectedIndex = 0; else if ((LightParams & 0x00FF) == 12) comboBox_BikeVolF.SelectedIndex = 1; else comboBox_BikeVolF.SelectedIndex = -1; //后灯电压 if (((LightParams >> 8) & 0x0F) == 6) comboBox_BikeVolB.SelectedIndex = 0; else if (((LightParams >> 8) & 0x0F) == 12) comboBox_BikeVolB.SelectedIndex = 1; else comboBox_BikeVolB.SelectedIndex = -1; //后灯模式c comboBox_BikeModeB.SelectedIndex = (LightParams >> 12) - 1; //启动模式 comboBox_BikeStartMode.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 12].ToString().Split(',')[1]) - 1; //开机延迟 ushort PowerOnOffParams = Convert.ToUInt16(arrText[rowNum + 13].ToString().Split(',')[1]); textBox_BikePowerOnTime.Text = (PowerOnOffParams >> 12).ToString(); //关机延迟 textBox_BikePowerOffTime.Text = ((PowerOnOffParams >> 8) & 0x0F).ToString(); //自动关机 textBox_BikeAutoOffTime.Text = (PowerOnOffParams & 0x00FF).ToString(); break; } case "[控制参数]": { //峰值电流 textBox_MotorMaxCurr.Text = arrText[rowNum + 4].ToString().Split(',')[1]; //过压保护 textBox_MotorOV.Text = arrText[rowNum + 6].ToString().Split(',')[1]; //欠压保护 textBox_MotorUV.Text = arrText[rowNum + 7].ToString().Split(',')[1]; break; } case "[助力参数]": { //限速起始 textBox_AssistSpeedBegin.Text = arrText[rowNum + 92].ToString().Split(',')[1]; //限速结束 textBox_AssistSpeedEnd.Text = arrText[rowNum + 93].ToString().Split(',')[1]; break; } case "[调试参数]": { //运行模式 if ((Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) >= 4) && (Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) <= 6)) { comboBox_SysRunMode.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) - 4; } else comboBox_SysRunMode.SelectedIndex = -1; //旋转方向 comboBox_SysDir.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 5].ToString().Split(',')[1]); //功率限幅 textBox_SysPowerLimit.Text = arrText[rowNum + 23].ToString().Split(',')[1]; break; } case "[生产信息]": { //生产商 textBox_ProductMac.Text= arrText[rowNum + 1].ToString().Split(',')[1]; //生产地 textBox_ProductAddr.Text = arrText[rowNum + 2].ToString().Split(',')[1]; //生产日期 textBox_ProductDate.Text = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", ""); //生产标识 textBox_ProductTag.Text = arrText[rowNum + 4].ToString().Split(',')[1]; break; } default:break; } } } while (false); do //高级参数界面 { richTextBox_AdvanceParams.Clear(); foreach (string sOutput in arrText) { richTextBox_AdvanceParams.AppendText(sOutput + "\r\n"); } richTextBox_AdvanceParams.Text = richTextBox_AdvanceParams.Text.Remove(richTextBox_AdvanceParams.Text.Length - 2); } while (false); } private void button_GenerateModelFile_Click(object sender, EventArgs e) { string DataFileName = ""; string FileInfo = ""; //填写文件名 Scan ModeFileName = new Scan(); ModeFileName.ShowDialog(); if (ModeFileName.textBox_Scan.Text == string.Empty) { MessageBox.Show("文件名为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DataFileName = ModeFileName.textBox_Scan.Text + ".src"; //核对文件名格式 //... //获取模板文本信息写入文件 FileInfo = richTextBox_AdvanceParams.Text; if (FileInfo == string.Empty) { MessageBox.Show("无效参数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } System.IO.File.WriteAllText(DataFileName, FileInfo); //模板文件上传服务器 if (myFtp.CheckFtp() == true) { //上传文件 bool result1 = myFtp.UploadFile(Directory.GetCurrentDirectory() + "\\" + DataFileName, "ParamsMode"); if (result1 == true) { MessageBox.Show("数据已上传!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("数据上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\" + DataFileName); } private void button_ExportParams_Click(object sender, EventArgs e) { //检查 if ((radioButton_MIGIC.Checked | radioButton_HUB.Checked) == false) { MessageBox.Show("请选择产品类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (Control c in groupBox1.Controls) { if (c is TextBox) { if (c.Text == "") { MessageBox.Show("信息填写不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } //分割参数 string[] lines = richTextBox_AdvanceParams.Text.Split(new string[] { "\n" }, StringSplitOptions.None); try { //更新信息 do { //建立字典,存放并获取每个命令的行号 Dictionary DicRowNum = new Dictionary(); DicRowNum.Add("[整车参数]", 0); DicRowNum.Add("[控制参数]", 0); DicRowNum.Add("[助力参数]", 0); DicRowNum.Add("[调试参数]", 0); DicRowNum.Add("[生产信息]", 0); Dictionary __dict = new Dictionary(DicRowNum); foreach (KeyValuePair item in __dict) { for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains(item.Key)) { DicRowNum[item.Key] = i; break; } } } //遍历更新 foreach (string index in DicRowNum.Keys) { int rowNum = DicRowNum[index]; //行号 switch (index) { case "[整车参数]": { //轮胎周长 lines[rowNum + 2] = "轮胎周长," + textBox_BikeWheel.Text; //转把限速 lines[rowNum + 4] = "转把限速," + textBox_BikeThroSpeed.Text; //推行限速 lines[rowNum + 5] = "推行限速," + textBox_BikeWalkSpeed.Text; //前牙盘 lines[rowNum + 6] = "前牙盘," + textBox_BikeFrontT.Text; //后牙盘 lines[rowNum + 7] = "后牙盘," + textBox_BikeRealB.Text; //前后灯参数 int LightParams = 0x0000; if (comboBox_BikeVolF.SelectedIndex == 0) LightParams |= 0x0006; else if (comboBox_BikeVolF.SelectedIndex == 1) LightParams |= 0x000C; else { MessageBox.Show("请选择前灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBox_BikeVolB.SelectedIndex == 0) LightParams |= 0x0600; else if (comboBox_BikeVolB.SelectedIndex == 1) LightParams |= 0x0C00; else { MessageBox.Show("请选择后灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBox_BikeModeB.SelectedIndex >= 0) LightParams |= ((comboBox_BikeModeB.SelectedIndex + 1) << 12); else { MessageBox.Show("请选择后灯模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } lines[rowNum + 10] = "前后灯参数," + ((ushort)LightParams).ToString(); //启动模式 lines[rowNum + 12] = "启动模式," + (comboBox_BikeStartMode.SelectedIndex + 1).ToString(); //开机延迟 int PowerTime = 0; PowerTime |= Convert.ToUInt16(textBox_BikeAutoOffTime.Text); PowerTime |= (Convert.ToUInt16(textBox_BikePowerOffTime.Text) << 8); PowerTime |= (Convert.ToUInt16(textBox_BikePowerOnTime.Text) << 12); lines[rowNum + 13] = "开关机参数," + ((ushort)PowerTime).ToString(); break; } case "[控制参数]": { //峰值电流 lines[rowNum + 4] = "峰值电流," + textBox_MotorMaxCurr.Text; //过压保护 lines[rowNum + 6] = "最高档位过压保护阈值," + textBox_MotorOV.Text; //欠压保护 lines[rowNum + 7] = "最高档位欠压保护阈值," + textBox_MotorUV.Text; break; } case "[助力参数]": { //限速起始 lines[rowNum + 92] = "车速限幅启动阈值," + textBox_AssistSpeedBegin.Text; //限速结束 lines[rowNum + 93] = "车速限幅停止阈值," + textBox_AssistSpeedEnd.Text; break; } case "[调试参数]": { //运行模式 lines[rowNum + 2] = "整体运行模式," + (comboBox_SysRunMode.SelectedIndex + 4).ToString(); //旋转方向 lines[rowNum + 5] = "旋转方向," + comboBox_SysDir.SelectedIndex.ToString(); //功率限幅 lines[rowNum + 23] = "功率限幅值," + textBox_SysPowerLimit.Text; break; } case "[生产信息]": { //生产商 lines[rowNum + 1] = "生产商," + textBox_ProductMac.Text; //生产地 lines[rowNum + 2] = "生产地," + textBox_ProductAddr.Text; //生产日期 lines[rowNum + 3] = "生产日期," + textBox_ProductDate.Text; //产品标识 lines[rowNum + 4] = "产品标识," + textBox_ProductTag.Text; break; } default: break; } } } while (false); //参数更新 richTextBox_AdvanceParams.Clear(); for (int i = 0; i < lines.Length; i++) { richTextBox_AdvanceParams.AppendText(lines[i] + "\r\n"); } richTextBox_AdvanceParams.Text = richTextBox_AdvanceParams.Text.Remove(richTextBox_AdvanceParams.Text.Length - 1); } catch (System.Exception) { MessageBox.Show("参数更新失败", "提示", MessageBoxButtons.OK); return; } //确定本地保存路径 string Date = DateTime.Now.ToString("yyyy-MM-dd"); string LocalPath = localInfo.LocalPath + "\\" + localInfo.CfgPathName + "\\" + Date; if (!Directory.Exists(LocalPath)) { Directory.CreateDirectory(LocalPath); } //确定远程保存路径 string ServerPath = ""; if (radioButton_HUB.Checked) { if (!myFtp.DirectoryExist("HUB_Control_TEST/cfg", Date)) myFtp.MakeDir("HUB_Control_TEST/cfg" + "/" + Date); ServerPath = "HUB_Control_TEST/cfg" + "/" + Date; } else if (radioButton_MIGIC.Checked) { if (!myFtp.DirectoryExist("MIGIC_TEST/cfg", Date)) myFtp.MakeDir("MIGIC_TEST/cfg" + "/" + Date); ServerPath = "MIGIC_TEST/cfg" + "/" + Date; } //确定文件名 string fileName = ""; fileName = textBox_ProductName.Text + "_" + textBox_MarkCode.Text + "-" + textBox_CustomerName.Text + "-" + textBox_MarkNum.Text + "_" + textBox_OtherInfo.Text + "_" + textBox_SoftwareVer.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "-"); //保存.src文件 System.IO.File.WriteAllText(LocalPath + "\\" + fileName + ".src", richTextBox_AdvanceParams.Text); //检查是否存在转换工具 if (!Directory.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile")) { Directory.CreateDirectory(localInfo.ToolsPath + "\\" + "GenerateParamsFile"); myFtp.DownloadFile("Tools/GenerateParamsFile/ConsoleApp2.exe", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.xml", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.dll", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); } else { if (!System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "ConsoleApp2.exe")) myFtp.DownloadFile("Tools/GenerateParamsFile/ConsoleApp2.exe", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); if (!System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "Newtonsoft.Json.xml")) myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.xml", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); if (!System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "Newtonsoft.Json.dll")) myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.dll", localInfo.ToolsPath + "\\" + "GenerateParamsFile"); } //转换.ttcfg文件 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "ConsoleApp2.exe"; startInfo.Arguments = LocalPath + "\\" + fileName + ".src"; System.Diagnostics.Process.Start(startInfo); Thread.Sleep(1000); //保存页面 Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片 bit.Save(LocalPath + "\\" + fileName + ".png"); //生成pdf文件,上传服务器 //上传 bool result1 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".src", ServerPath); bool result2 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".ttcfg", ServerPath); bool result3 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".png", ServerPath); if (result1 & result2 & result3) { MessageBox.Show("上传成功", "提示", MessageBoxButtons.OK); } else { MessageBox.Show("上传失败", "提示", MessageBoxButtons.OK); } } } }