123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913 |
- 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 radioButton_MIGIC_Click(object sender, EventArgs e)
- {
- textBox_ProductTag.Text = "MM_MC1";
- comboBox_SysRunMode.Items.Clear();
- comboBox_SysRunMode.Items.Add("CITY");
- comboBox_SysRunMode.Items.Add("MTB");
- }
- private void radioButton_HUB_Click(object sender, EventArgs e)
- {
- textBox_ProductTag.Text = "GF_250_1";
- comboBox_SysRunMode.Items.Clear();
- comboBox_SysRunMode.Items.Add("踏频");
- comboBox_SysRunMode.Items.Add("力矩");
- }
- private void comboBox_SysRunMode_Click(object sender, EventArgs e)
- {
- if ((radioButton_MIGIC.Checked | radioButton_HUB.Checked) == false)
- {
- MessageBox.Show("请选择产品类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void button_ChangeModelFile_Click(object sender, EventArgs e)
- {
- string DataFileName = "";
- string FileInfo = "";
- if (comboBox_ModelFile.SelectedIndex == -1)
- {
- MessageBox.Show("未选择模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (MessageBox.Show("确认修改模板?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
- {
- return;
- }
- //填写文件名
- DataFileName = comboBox_ModelFile.Text;
- //分割参数更新参数内容
- string[] lines = richTextBox_AdvanceParams.Text.Split(new string[] { "\n" }, StringSplitOptions.None);
- try
- {
- //更新信息
- do
- {
- //建立字典,存放并获取每个命令的行号
- Dictionary<string, int> DicRowNum = new Dictionary<string, int>();
- DicRowNum.Add("[整车参数]", 0);
- DicRowNum.Add("[控制参数]", 0);
- DicRowNum.Add("[助力参数]", 0);
- DicRowNum.Add("[调试参数]", 0);
- DicRowNum.Add("[生产信息]", 0);
- Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
- foreach (KeyValuePair<string, int> 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;
- }
- //获取模板文本信息写入文件
- 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)
- {
- //删除远程文件
- myFtp.DeleteFile("/ParamsMode/" + DataFileName);
- //上传文件
- 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_DeletModelFile_Click(object sender, EventArgs e)
- {
- if (comboBox_ModelFile.SelectedIndex == -1)
- {
- MessageBox.Show("未选择模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- else
- {
- Login loginForm = new Login();
- loginForm.ShowDialog();
- if (loginForm.textBox_Passwd.Text == loginForm.UserAccount[loginForm.comboBox_User.Text])//检验模式所有用户支持
- {
- if (!loginForm.comboBox_User.Text.Contains("工程参数配置"))
- {
- MessageBox.Show("权限不支持!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- }
- else
- {
- MessageBox.Show("密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (myFtp.CheckFtp() == true)
- {
- //删除远程文件
- myFtp.DeleteFile("/ParamsMode/" + comboBox_ModelFile.Text);
- comboBox_ModelFile.Items.Clear();
- richTextBox_AdvanceParams.Clear();
- MessageBox.Show("模板已删除,请刷新!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- else
- {
- MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- }
- private void GenerateParams_Load(object sender, EventArgs e)
- {
- //更新版本信息
- label_Ver.Text = "版本: V" + mainForm.Version;
- label_BT.Text = "编译时间:" + System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString("yyyy-MM-dd HH:mm:ss");
- //导入网络配置
- 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<string, int> DicRowNum = new Dictionary<string, int>();
- DicRowNum.Add("[整车参数]", 0);
- DicRowNum.Add("[控制参数]", 0);
- DicRowNum.Add("[助力参数]", 0);
- DicRowNum.Add("[调试参数]", 0);
- DicRowNum.Add("[生产信息]", 0);
- Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
- foreach (KeyValuePair<string, int> 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;
- }
- if (c.Text.Contains('_'))
- {
- if ((c.Name != "textBox_ProductTag")&& (c.Name != "textBox_SoftwareVer"))
- {
- MessageBox.Show("\"" + c.Text + "\"" + "包含字符'_'", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- }
- }
- //分割参数
- string[] lines = richTextBox_AdvanceParams.Text.Split(new string[] { "\n" }, StringSplitOptions.None);
- try
- {
- //更新信息
- do
- {
- //建立字典,存放并获取每个命令的行号
- Dictionary<string, int> DicRowNum = new Dictionary<string, int>();
- DicRowNum.Add("[整车参数]", 0);
- DicRowNum.Add("[控制参数]", 0);
- DicRowNum.Add("[助力参数]", 0);
- DicRowNum.Add("[调试参数]", 0);
- DicRowNum.Add("[生产信息]", 0);
- Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
- foreach (KeyValuePair<string, int> 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"))
- System.IO.File.Delete(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);
- }
-
- }
- }
- }
|