GenerateParams.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Runtime.Remoting.Messaging;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using static System.Net.WebRequestMethods;
  17. namespace Welling_Motor_Debug_Tool
  18. {
  19. public partial class GenerateParams : Form
  20. {
  21. //存储路径文件
  22. LocalInfo localInfo = new LocalInfo();
  23. //服务器配置
  24. string IP, Port, User, PassWD, ModelPath, CfgPath;
  25. //FTP
  26. ftp myFtp = new ftp();
  27. public GenerateParams()
  28. {
  29. InitializeComponent();
  30. }
  31. private void button_NoteRead_Click(object sender, EventArgs e)
  32. {
  33. //检查本地文件删除
  34. if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName))
  35. System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName);
  36. //下载远程文件
  37. myFtp.DownloadFile("/Note/Note.txt", localInfo.LocalPath);
  38. //显示文件
  39. StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.NoteFileName);
  40. string sLine = "";
  41. ArrayList arrText = new ArrayList();//创建一个动态数组
  42. while (sLine != null)
  43. {
  44. sLine = objReader.ReadLine();
  45. arrText.Add(sLine);
  46. }
  47. objReader.Close();
  48. richTextBox_Note.Clear();
  49. foreach (string sOutput in arrText)
  50. {
  51. richTextBox_Note.AppendText(sOutput + "\r\n");
  52. }
  53. richTextBox_Note.Text = richTextBox_Note.Text.Remove(richTextBox_Note.Text.Length - 1);
  54. }
  55. private void button_NoteWrite_Click(object sender, EventArgs e)
  56. {
  57. //检查本地并删除
  58. if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName))
  59. System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName);
  60. //保存
  61. System.IO.File.WriteAllText(localInfo.LocalPath + localInfo.NoteFileName, richTextBox_Note.Text);
  62. //上传服务器
  63. if (!myFtp.DirectoryExist("/", "Note"))
  64. myFtp.MakeDir("/Note");
  65. bool result = myFtp.UploadFile(localInfo.LocalPath + localInfo.NoteFileName, "/Note");
  66. if (result)
  67. MessageBox.Show("上传完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  68. else
  69. MessageBox.Show("上传失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  70. }
  71. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
  72. {
  73. //选中备忘录时自动读取更新
  74. if (tabControl1.SelectedIndex == 2)
  75. {
  76. //检查本地文件删除
  77. if (System.IO.File.Exists(localInfo.LocalPath + localInfo.NoteFileName))
  78. System.IO.File.Delete(localInfo.LocalPath + localInfo.NoteFileName);
  79. //下载远程文件
  80. myFtp.DownloadFile("/Note/Note.txt", localInfo.LocalPath);
  81. //显示文件
  82. StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.NoteFileName);
  83. string sLine = "";
  84. ArrayList arrText = new ArrayList();//创建一个动态数组
  85. while (sLine != null)
  86. {
  87. sLine = objReader.ReadLine();
  88. arrText.Add(sLine);
  89. }
  90. objReader.Close();
  91. richTextBox_Note.Clear();
  92. foreach (string sOutput in arrText)
  93. {
  94. richTextBox_Note.AppendText(sOutput + "\r\n");
  95. }
  96. richTextBox_Note.Text = richTextBox_Note.Text.Remove(richTextBox_Note.Text.Length - 1);
  97. }
  98. }
  99. private void radioButton_MIGIC_Click(object sender, EventArgs e)
  100. {
  101. textBox_ProductTag.Text = "MM_MC1";
  102. comboBox_SysRunMode.Items.Clear();
  103. comboBox_SysRunMode.Items.Add("CITY");
  104. comboBox_SysRunMode.Items.Add("MTB");
  105. }
  106. private void radioButton_HUB_Click(object sender, EventArgs e)
  107. {
  108. textBox_ProductTag.Text = "GF_250_1";
  109. comboBox_SysRunMode.Items.Clear();
  110. comboBox_SysRunMode.Items.Add("踏频");
  111. comboBox_SysRunMode.Items.Add("力矩");
  112. }
  113. private void comboBox_SysRunMode_Click(object sender, EventArgs e)
  114. {
  115. if ((radioButton_MIGIC.Checked | radioButton_HUB.Checked) == false)
  116. {
  117. MessageBox.Show("请选择产品类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  118. }
  119. }
  120. private void button_ChangeModelFile_Click(object sender, EventArgs e)
  121. {
  122. string DataFileName = "";
  123. string FileInfo = "";
  124. if (comboBox_ModelFile.SelectedIndex == -1)
  125. {
  126. MessageBox.Show("未选择模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  127. return;
  128. }
  129. if (MessageBox.Show("确认修改模板?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  130. {
  131. return;
  132. }
  133. //填写文件名
  134. DataFileName = comboBox_ModelFile.Text;
  135. //分割参数更新参数内容
  136. string[] lines = richTextBox_AdvanceParams.Text.Split(new string[] { "\n" }, StringSplitOptions.None);
  137. try
  138. {
  139. //更新信息
  140. do
  141. {
  142. //建立字典,存放并获取每个命令的行号
  143. Dictionary<string, int> DicRowNum = new Dictionary<string, int>();
  144. DicRowNum.Add("[整车参数]", 0);
  145. DicRowNum.Add("[控制参数]", 0);
  146. DicRowNum.Add("[助力参数]", 0);
  147. DicRowNum.Add("[调试参数]", 0);
  148. DicRowNum.Add("[生产信息]", 0);
  149. Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
  150. foreach (KeyValuePair<string, int> item in __dict)
  151. {
  152. for (int i = 0; i < lines.Length; i++)
  153. {
  154. if (lines[i].Contains(item.Key))
  155. {
  156. DicRowNum[item.Key] = i;
  157. break;
  158. }
  159. }
  160. }
  161. //遍历更新
  162. foreach (string index in DicRowNum.Keys)
  163. {
  164. int rowNum = DicRowNum[index]; //行号
  165. switch (index)
  166. {
  167. case "[整车参数]":
  168. {
  169. //轮胎周长
  170. lines[rowNum + 2] = "轮胎周长," + textBox_BikeWheel.Text;
  171. //转把限速
  172. lines[rowNum + 4] = "转把限速," + textBox_BikeThroSpeed.Text;
  173. //推行限速
  174. lines[rowNum + 5] = "推行限速," + textBox_BikeWalkSpeed.Text;
  175. //前牙盘
  176. lines[rowNum + 6] = "前牙盘," + textBox_BikeFrontT.Text;
  177. //后牙盘
  178. lines[rowNum + 7] = "后牙盘," + textBox_BikeRealB.Text;
  179. //前后灯参数
  180. int LightParams = 0x0000;
  181. if (comboBox_BikeVolF.SelectedIndex == 0)
  182. LightParams |= 0x0006;
  183. else if (comboBox_BikeVolF.SelectedIndex == 1)
  184. LightParams |= 0x000C;
  185. else
  186. {
  187. MessageBox.Show("请选择前灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  188. return;
  189. }
  190. if (comboBox_BikeVolB.SelectedIndex == 0)
  191. LightParams |= 0x0600;
  192. else if (comboBox_BikeVolB.SelectedIndex == 1)
  193. LightParams |= 0x0C00;
  194. else
  195. {
  196. MessageBox.Show("请选择后灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  197. return;
  198. }
  199. if (comboBox_BikeModeB.SelectedIndex >= 0)
  200. LightParams |= ((comboBox_BikeModeB.SelectedIndex + 1) << 12);
  201. else
  202. {
  203. MessageBox.Show("请选择后灯模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  204. return;
  205. }
  206. lines[rowNum + 10] = "前后灯参数," + ((ushort)LightParams).ToString();
  207. //启动模式
  208. lines[rowNum + 12] = "启动模式," + (comboBox_BikeStartMode.SelectedIndex + 1).ToString();
  209. //开机延迟
  210. int PowerTime = 0;
  211. PowerTime |= Convert.ToUInt16(textBox_BikeAutoOffTime.Text);
  212. PowerTime |= (Convert.ToUInt16(textBox_BikePowerOffTime.Text) << 8);
  213. PowerTime |= (Convert.ToUInt16(textBox_BikePowerOnTime.Text) << 12);
  214. lines[rowNum + 13] = "开关机参数," + ((ushort)PowerTime).ToString();
  215. break;
  216. }
  217. case "[控制参数]":
  218. {
  219. //峰值电流
  220. lines[rowNum + 4] = "峰值电流," + textBox_MotorMaxCurr.Text;
  221. //过压保护
  222. lines[rowNum + 6] = "最高档位过压保护阈值," + textBox_MotorOV.Text;
  223. //欠压保护
  224. lines[rowNum + 7] = "最高档位欠压保护阈值," + textBox_MotorUV.Text;
  225. break;
  226. }
  227. case "[助力参数]":
  228. {
  229. //限速起始
  230. lines[rowNum + 92] = "车速限幅启动阈值," + textBox_AssistSpeedBegin.Text;
  231. //限速结束
  232. lines[rowNum + 93] = "车速限幅停止阈值," + textBox_AssistSpeedEnd.Text;
  233. break;
  234. }
  235. case "[调试参数]":
  236. {
  237. //运行模式
  238. lines[rowNum + 2] = "整体运行模式," + (comboBox_SysRunMode.SelectedIndex + 4).ToString();
  239. //旋转方向
  240. lines[rowNum + 5] = "旋转方向," + comboBox_SysDir.SelectedIndex.ToString();
  241. //功率限幅
  242. lines[rowNum + 23] = "功率限幅值," + textBox_SysPowerLimit.Text;
  243. break;
  244. }
  245. case "[生产信息]":
  246. {
  247. //生产商
  248. lines[rowNum + 1] = "生产商," + textBox_ProductMac.Text;
  249. //生产地
  250. lines[rowNum + 2] = "生产地," + textBox_ProductAddr.Text;
  251. //生产日期
  252. lines[rowNum + 3] = "生产日期," + textBox_ProductDate.Text;
  253. //产品标识
  254. lines[rowNum + 4] = "产品标识," + textBox_ProductTag.Text;
  255. break;
  256. }
  257. default: break;
  258. }
  259. }
  260. } while (false);
  261. //参数更新
  262. richTextBox_AdvanceParams.Clear();
  263. for (int i = 0; i < lines.Length; i++)
  264. {
  265. richTextBox_AdvanceParams.AppendText(lines[i] + "\r\n");
  266. }
  267. richTextBox_AdvanceParams.Text = richTextBox_AdvanceParams.Text.Remove(richTextBox_AdvanceParams.Text.Length - 1);
  268. }
  269. catch (System.Exception)
  270. {
  271. MessageBox.Show("参数格式错误,更新失败", "提示", MessageBoxButtons.OK);
  272. return;
  273. }
  274. //获取模板文本信息写入文件
  275. FileInfo = richTextBox_AdvanceParams.Text;
  276. if (FileInfo == string.Empty)
  277. {
  278. MessageBox.Show("无效参数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  279. return;
  280. }
  281. System.IO.File.WriteAllText(DataFileName, FileInfo);
  282. //模板文件上传服务器
  283. if (myFtp.CheckFtp() == true)
  284. {
  285. //删除远程文件
  286. myFtp.DeleteFile("/ParamsMode/" + DataFileName);
  287. //上传文件
  288. bool result1 = myFtp.UploadFile(Directory.GetCurrentDirectory() + "\\" + DataFileName, "ParamsMode");
  289. if (result1 == true)
  290. {
  291. MessageBox.Show("数据已上传!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  292. }
  293. else
  294. {
  295. MessageBox.Show("数据上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  296. }
  297. }
  298. else
  299. {
  300. MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  301. }
  302. System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\" + DataFileName);
  303. }
  304. private void button_DeletModelFile_Click(object sender, EventArgs e)
  305. {
  306. if (comboBox_ModelFile.SelectedIndex == -1)
  307. {
  308. MessageBox.Show("未选择模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  309. }
  310. else
  311. {
  312. Login loginForm = new Login();
  313. loginForm.ShowDialog();
  314. if (loginForm.textBox_Passwd.Text == loginForm.UserAccount[loginForm.comboBox_User.Text])//检验模式所有用户支持
  315. {
  316. if (!loginForm.comboBox_User.Text.Contains("工程参数配置"))
  317. {
  318. MessageBox.Show("权限不支持!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  319. return;
  320. }
  321. }
  322. else
  323. {
  324. MessageBox.Show("密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  325. return;
  326. }
  327. if (myFtp.CheckFtp() == true)
  328. {
  329. //删除远程文件
  330. myFtp.DeleteFile("/ParamsMode/" + comboBox_ModelFile.Text);
  331. comboBox_ModelFile.Items.Clear();
  332. richTextBox_AdvanceParams.Clear();
  333. MessageBox.Show("模板已删除,请刷新!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  334. }
  335. else
  336. {
  337. MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  338. }
  339. }
  340. }
  341. private void GenerateParams_Load(object sender, EventArgs e)
  342. {
  343. //更新版本信息
  344. label_Ver.Text = "版本: V" + mainForm.Version;
  345. label_BT.Text = "编译时间:" + System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString("yyyy-MM-dd HH:mm:ss");
  346. //导入网络配置
  347. if (System.IO.File.Exists(localInfo.LocalPath + localInfo.ConfigFileName)) //存在配置文件,导入配置信息
  348. {
  349. //打开文件
  350. StreamReader objReader = new StreamReader(localInfo.LocalPath + localInfo.ConfigFileName);
  351. string sLine = "";
  352. ArrayList array_CfgInfo = new ArrayList();
  353. array_CfgInfo.Clear();
  354. while (sLine != null)
  355. {
  356. sLine = objReader.ReadLine();
  357. array_CfgInfo.Add(sLine);
  358. }
  359. objReader.Close();
  360. //解析配置文件
  361. try
  362. {
  363. //Server Set IP, Port, User, PassWS, ModelPath;
  364. IP = array_CfgInfo[12].ToString().Split(':')[1];
  365. Port = array_CfgInfo[13].ToString().Split(':')[1];
  366. User = array_CfgInfo[14].ToString().Split(':')[1];
  367. PassWD = array_CfgInfo[15].ToString().Split(':')[1];
  368. ModelPath = "ParamsMode";
  369. myFtp.FtpOption(IP, Port, User, PassWD);
  370. }
  371. catch (System.Exception)
  372. {
  373. MessageBox.Show("参数格式错误,写入默认值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  374. }
  375. }
  376. else
  377. {
  378. MessageBox.Show("参数文件丢失!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  379. this.Close();
  380. }
  381. //检查网络
  382. if (myFtp.CheckFtp() == false)
  383. {
  384. label_Server_ComStatus.Text = "网络已断开";
  385. label_ServerStatus.BackColor = Color.Red;
  386. MessageBox.Show("网络断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  387. this.Close();
  388. }
  389. else
  390. {
  391. label_Server_ComStatus.Text = "网络已连接";
  392. label_ServerStatus.BackColor = Color.Green;
  393. }
  394. //加载模板文件
  395. comboBox_ModelFile.Items.Clear();
  396. string[] FileList = { "" };
  397. FileList = myFtp.GetFileNameList(ModelPath);
  398. comboBox_ModelFile.Items.Clear();
  399. foreach (var file in FileList)
  400. {
  401. if (file.Contains(".src"))
  402. {
  403. comboBox_ModelFile.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  404. }
  405. }
  406. }
  407. private void button_ModeRefresh_Click(object sender, EventArgs e)
  408. {
  409. //加载模板文件
  410. comboBox_ModelFile.Items.Clear();
  411. string[] FileList = { "" };
  412. FileList = myFtp.GetFileNameList(ModelPath);
  413. comboBox_ModelFile.Items.Clear();
  414. foreach (var file in FileList)
  415. {
  416. if (file.Contains(".src"))
  417. {
  418. comboBox_ModelFile.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  419. }
  420. }
  421. }
  422. private void comboBox_ModelFile_SelectedIndexChanged(object sender, EventArgs e)
  423. {
  424. button_ExportParams.Enabled = true;
  425. //下载文件
  426. if (System.IO.File.Exists(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text))//本地存在选定文件,先删除本地
  427. {
  428. System.IO.File.Delete(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text);
  429. }
  430. myFtp.DownloadFile("ParamsMode/" + comboBox_ModelFile.Text, localInfo.LocalPath);
  431. //打开文件
  432. StreamReader objReader = new StreamReader(localInfo.LocalPath + "\\" + comboBox_ModelFile.Text);
  433. string sLine = "";
  434. ArrayList arrText = new ArrayList();//创建一个动态数组
  435. while (sLine != null)
  436. {
  437. sLine = objReader.ReadLine();
  438. arrText.Add(sLine);
  439. }
  440. objReader.Close();
  441. //加载文件
  442. do //基础参数界面
  443. {
  444. //建立字典,存放并获取每个命令的行号
  445. Dictionary<string, int> DicRowNum = new Dictionary<string, int>();
  446. DicRowNum.Add("[整车参数]", 0);
  447. DicRowNum.Add("[控制参数]", 0);
  448. DicRowNum.Add("[助力参数]", 0);
  449. DicRowNum.Add("[调试参数]", 0);
  450. DicRowNum.Add("[生产信息]", 0);
  451. Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
  452. foreach (KeyValuePair<string, int> item in __dict)
  453. {
  454. foreach (string cmd in arrText)
  455. {
  456. if (cmd.Contains(item.Key))
  457. {
  458. DicRowNum[item.Key] = arrText.IndexOf(cmd);
  459. break;
  460. }
  461. }
  462. }
  463. //遍历加载
  464. foreach (string index in DicRowNum.Keys)
  465. {
  466. int rowNum = DicRowNum[index]; //行号
  467. switch (index)
  468. {
  469. case "[整车参数]":
  470. {
  471. //轮胎周长
  472. textBox_BikeWheel.Text = arrText[rowNum + 2].ToString().Split(',')[1];
  473. //转把限速
  474. textBox_BikeThroSpeed.Text = arrText[rowNum + 4].ToString().Split(',')[1];
  475. //推行限速
  476. textBox_BikeWalkSpeed.Text = arrText[rowNum + 5].ToString().Split(',')[1];
  477. //前牙盘
  478. textBox_BikeFrontT.Text = arrText[rowNum + 6].ToString().Split(',')[1];
  479. //后牙盘
  480. textBox_BikeRealB.Text = arrText[rowNum + 7].ToString().Split(',')[1];
  481. //前灯电压
  482. ushort LightParams = Convert.ToUInt16(arrText[rowNum + 10].ToString().Split(',')[1]);
  483. if ((LightParams & 0x00FF) == 6)
  484. comboBox_BikeVolF.SelectedIndex = 0;
  485. else if ((LightParams & 0x00FF) == 12)
  486. comboBox_BikeVolF.SelectedIndex = 1;
  487. else
  488. comboBox_BikeVolF.SelectedIndex = -1;
  489. //后灯电压
  490. if (((LightParams >> 8) & 0x0F) == 6)
  491. comboBox_BikeVolB.SelectedIndex = 0;
  492. else if (((LightParams >> 8) & 0x0F) == 12)
  493. comboBox_BikeVolB.SelectedIndex = 1;
  494. else
  495. comboBox_BikeVolB.SelectedIndex = -1;
  496. //后灯模式c
  497. comboBox_BikeModeB.SelectedIndex = (LightParams >> 12) - 1;
  498. //启动模式
  499. comboBox_BikeStartMode.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 12].ToString().Split(',')[1]) - 1;
  500. //开机延迟
  501. ushort PowerOnOffParams = Convert.ToUInt16(arrText[rowNum + 13].ToString().Split(',')[1]);
  502. textBox_BikePowerOnTime.Text = (PowerOnOffParams >> 12).ToString();
  503. //关机延迟
  504. textBox_BikePowerOffTime.Text = ((PowerOnOffParams >> 8) & 0x0F).ToString();
  505. //自动关机
  506. textBox_BikeAutoOffTime.Text = (PowerOnOffParams & 0x00FF).ToString();
  507. break;
  508. }
  509. case "[控制参数]":
  510. {
  511. //峰值电流
  512. textBox_MotorMaxCurr.Text = arrText[rowNum + 4].ToString().Split(',')[1];
  513. //过压保护
  514. textBox_MotorOV.Text = arrText[rowNum + 6].ToString().Split(',')[1];
  515. //欠压保护
  516. textBox_MotorUV.Text = arrText[rowNum + 7].ToString().Split(',')[1];
  517. break;
  518. }
  519. case "[助力参数]":
  520. {
  521. //限速起始
  522. textBox_AssistSpeedBegin.Text = arrText[rowNum + 92].ToString().Split(',')[1];
  523. //限速结束
  524. textBox_AssistSpeedEnd.Text = arrText[rowNum + 93].ToString().Split(',')[1];
  525. break;
  526. }
  527. case "[调试参数]":
  528. {
  529. //运行模式
  530. if ((Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) >= 4) && (Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) <= 6))
  531. {
  532. comboBox_SysRunMode.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 2].ToString().Split(',')[1]) - 4;
  533. }
  534. else
  535. comboBox_SysRunMode.SelectedIndex = -1;
  536. //旋转方向
  537. comboBox_SysDir.SelectedIndex = Convert.ToUInt16(arrText[rowNum + 5].ToString().Split(',')[1]);
  538. //功率限幅
  539. textBox_SysPowerLimit.Text = arrText[rowNum + 23].ToString().Split(',')[1];
  540. break;
  541. }
  542. case "[生产信息]":
  543. {
  544. //生产商
  545. textBox_ProductMac.Text= arrText[rowNum + 1].ToString().Split(',')[1];
  546. //生产地
  547. textBox_ProductAddr.Text = arrText[rowNum + 2].ToString().Split(',')[1];
  548. //生产日期
  549. textBox_ProductDate.Text = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "");
  550. //生产标识
  551. textBox_ProductTag.Text = arrText[rowNum + 4].ToString().Split(',')[1];
  552. break;
  553. }
  554. default:break;
  555. }
  556. }
  557. } while (false);
  558. do //高级参数界面
  559. {
  560. richTextBox_AdvanceParams.Clear();
  561. foreach (string sOutput in arrText)
  562. {
  563. richTextBox_AdvanceParams.AppendText(sOutput + "\r\n");
  564. }
  565. richTextBox_AdvanceParams.Text = richTextBox_AdvanceParams.Text.Remove(richTextBox_AdvanceParams.Text.Length - 2);
  566. } while (false);
  567. }
  568. private void button_GenerateModelFile_Click(object sender, EventArgs e)
  569. {
  570. string DataFileName = "";
  571. string FileInfo = "";
  572. //填写文件名
  573. Scan ModeFileName = new Scan();
  574. ModeFileName.ShowDialog();
  575. if (ModeFileName.textBox_Scan.Text == string.Empty)
  576. {
  577. MessageBox.Show("文件名为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  578. return;
  579. }
  580. DataFileName = ModeFileName.textBox_Scan.Text + ".src";
  581. //核对文件名格式
  582. //...
  583. //获取模板文本信息写入文件
  584. FileInfo = richTextBox_AdvanceParams.Text;
  585. if (FileInfo == string.Empty)
  586. {
  587. MessageBox.Show("无效参数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  588. return;
  589. }
  590. System.IO.File.WriteAllText(DataFileName, FileInfo);
  591. //模板文件上传服务器
  592. if (myFtp.CheckFtp() == true)
  593. {
  594. //上传文件
  595. bool result1 = myFtp.UploadFile(Directory.GetCurrentDirectory() + "\\" + DataFileName, "ParamsMode");
  596. if (result1 == true)
  597. {
  598. MessageBox.Show("数据已上传!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  599. }
  600. else
  601. {
  602. MessageBox.Show("数据上传失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  603. }
  604. }
  605. else
  606. {
  607. MessageBox.Show("服务器断开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  608. }
  609. System.IO.File.Delete(Directory.GetCurrentDirectory() + "\\" + DataFileName);
  610. }
  611. private void button_ExportParams_Click(object sender, EventArgs e)
  612. {
  613. //检查
  614. if ((radioButton_MIGIC.Checked | radioButton_HUB.Checked) == false)
  615. {
  616. MessageBox.Show("请选择产品类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  617. return;
  618. }
  619. foreach (Control c in groupBox1.Controls)
  620. {
  621. if (c is TextBox)
  622. {
  623. if (c.Text == "")
  624. {
  625. MessageBox.Show("信息填写不完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  626. return;
  627. }
  628. if (c.Text.Contains('_'))
  629. {
  630. if ((c.Name != "textBox_ProductTag")&& (c.Name != "textBox_SoftwareVer"))
  631. {
  632. MessageBox.Show("\"" + c.Text + "\"" + "包含字符'_'", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  633. return;
  634. }
  635. }
  636. }
  637. }
  638. //分割参数
  639. string[] lines = richTextBox_AdvanceParams.Text.Split(new string[] { "\n" }, StringSplitOptions.None);
  640. try
  641. {
  642. //更新信息
  643. do
  644. {
  645. //建立字典,存放并获取每个命令的行号
  646. Dictionary<string, int> DicRowNum = new Dictionary<string, int>();
  647. DicRowNum.Add("[整车参数]", 0);
  648. DicRowNum.Add("[控制参数]", 0);
  649. DicRowNum.Add("[助力参数]", 0);
  650. DicRowNum.Add("[调试参数]", 0);
  651. DicRowNum.Add("[生产信息]", 0);
  652. Dictionary<string, int> __dict = new Dictionary<string, int>(DicRowNum);
  653. foreach (KeyValuePair<string, int> item in __dict)
  654. {
  655. for (int i = 0; i < lines.Length; i++)
  656. {
  657. if (lines[i].Contains(item.Key))
  658. {
  659. DicRowNum[item.Key] = i;
  660. break;
  661. }
  662. }
  663. }
  664. //遍历更新
  665. foreach (string index in DicRowNum.Keys)
  666. {
  667. int rowNum = DicRowNum[index]; //行号
  668. switch (index)
  669. {
  670. case "[整车参数]":
  671. {
  672. //轮胎周长
  673. lines[rowNum + 2] = "轮胎周长," + textBox_BikeWheel.Text;
  674. //转把限速
  675. lines[rowNum + 4] = "转把限速," + textBox_BikeThroSpeed.Text;
  676. //推行限速
  677. lines[rowNum + 5] = "推行限速," + textBox_BikeWalkSpeed.Text;
  678. //前牙盘
  679. lines[rowNum + 6] = "前牙盘," + textBox_BikeFrontT.Text;
  680. //后牙盘
  681. lines[rowNum + 7] = "后牙盘," + textBox_BikeRealB.Text;
  682. //前后灯参数
  683. int LightParams = 0x0000;
  684. if (comboBox_BikeVolF.SelectedIndex == 0)
  685. LightParams |= 0x0006;
  686. else if (comboBox_BikeVolF.SelectedIndex == 1)
  687. LightParams |= 0x000C;
  688. else
  689. {
  690. MessageBox.Show("请选择前灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  691. return;
  692. }
  693. if (comboBox_BikeVolB.SelectedIndex == 0)
  694. LightParams |= 0x0600;
  695. else if (comboBox_BikeVolB.SelectedIndex == 1)
  696. LightParams |= 0x0C00;
  697. else
  698. {
  699. MessageBox.Show("请选择后灯电压!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  700. return;
  701. }
  702. if (comboBox_BikeModeB.SelectedIndex >= 0)
  703. LightParams |= ((comboBox_BikeModeB.SelectedIndex + 1) << 12);
  704. else
  705. {
  706. MessageBox.Show("请选择后灯模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  707. return;
  708. }
  709. lines[rowNum + 10] = "前后灯参数," + ((ushort)LightParams).ToString();
  710. //启动模式
  711. lines[rowNum + 12] = "启动模式," + (comboBox_BikeStartMode.SelectedIndex + 1).ToString();
  712. //开机延迟
  713. int PowerTime = 0;
  714. PowerTime |= Convert.ToUInt16(textBox_BikeAutoOffTime.Text);
  715. PowerTime |= (Convert.ToUInt16(textBox_BikePowerOffTime.Text) << 8);
  716. PowerTime |= (Convert.ToUInt16(textBox_BikePowerOnTime.Text) << 12);
  717. lines[rowNum + 13] = "开关机参数," + ((ushort)PowerTime).ToString();
  718. break;
  719. }
  720. case "[控制参数]":
  721. {
  722. //峰值电流
  723. lines[rowNum + 4] = "峰值电流," + textBox_MotorMaxCurr.Text;
  724. //过压保护
  725. lines[rowNum + 6] = "最高档位过压保护阈值," + textBox_MotorOV.Text;
  726. //欠压保护
  727. lines[rowNum + 7] = "最高档位欠压保护阈值," + textBox_MotorUV.Text;
  728. break;
  729. }
  730. case "[助力参数]":
  731. {
  732. //限速起始
  733. lines[rowNum + 92] = "车速限幅启动阈值," + textBox_AssistSpeedBegin.Text;
  734. //限速结束
  735. lines[rowNum + 93] = "车速限幅停止阈值," + textBox_AssistSpeedEnd.Text;
  736. break;
  737. }
  738. case "[调试参数]":
  739. {
  740. //运行模式
  741. lines[rowNum + 2] = "整体运行模式," + (comboBox_SysRunMode.SelectedIndex + 4).ToString();
  742. //旋转方向
  743. lines[rowNum + 5] = "旋转方向," + comboBox_SysDir.SelectedIndex.ToString();
  744. //功率限幅
  745. lines[rowNum + 23] = "功率限幅值," + textBox_SysPowerLimit.Text;
  746. break;
  747. }
  748. case "[生产信息]":
  749. {
  750. //生产商
  751. lines[rowNum + 1] = "生产商," + textBox_ProductMac.Text;
  752. //生产地
  753. lines[rowNum + 2] = "生产地," + textBox_ProductAddr.Text;
  754. //生产日期
  755. lines[rowNum + 3] = "生产日期," + textBox_ProductDate.Text;
  756. //产品标识
  757. lines[rowNum + 4] = "产品标识," + textBox_ProductTag.Text;
  758. break;
  759. }
  760. default: break;
  761. }
  762. }
  763. } while (false);
  764. //参数更新
  765. richTextBox_AdvanceParams.Clear();
  766. for (int i = 0; i < lines.Length; i++)
  767. {
  768. richTextBox_AdvanceParams.AppendText(lines[i] + "\r\n");
  769. }
  770. richTextBox_AdvanceParams.Text = richTextBox_AdvanceParams.Text.Remove(richTextBox_AdvanceParams.Text.Length - 1);
  771. }
  772. catch (System.Exception)
  773. {
  774. MessageBox.Show("参数格式错误,更新失败", "提示", MessageBoxButtons.OK);
  775. return;
  776. }
  777. //确定本地保存路径
  778. string Date = DateTime.Now.ToString("yyyy-MM-dd");
  779. string LocalPath = localInfo.LocalPath + "\\" + localInfo.CfgPathName + "\\" + Date;
  780. if (!Directory.Exists(LocalPath))
  781. {
  782. Directory.CreateDirectory(LocalPath);
  783. }
  784. //确定远程保存路径
  785. string ServerPath = "";
  786. if (radioButton_HUB.Checked)
  787. {
  788. if (!myFtp.DirectoryExist("HUB_Control_TEST/cfg", Date))
  789. myFtp.MakeDir("HUB_Control_TEST/cfg" + "/" + Date);
  790. ServerPath = "HUB_Control_TEST/cfg" + "/" + Date;
  791. }
  792. else if (radioButton_MIGIC.Checked)
  793. {
  794. if (!myFtp.DirectoryExist("MIGIC_TEST/cfg", Date))
  795. myFtp.MakeDir("MIGIC_TEST/cfg" + "/" + Date);
  796. ServerPath = "MIGIC_TEST/cfg" + "/" + Date;
  797. }
  798. //确定文件名
  799. string fileName = "";
  800. fileName = textBox_ProductName.Text + "_" + textBox_MarkCode.Text + "-" + textBox_CustomerName.Text + "-" +
  801. textBox_MarkNum.Text + "_" + textBox_OtherInfo.Text + "_" + textBox_SoftwareVer.Text + "_" +
  802. DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "-");
  803. //保存.src文件
  804. System.IO.File.WriteAllText(LocalPath + "\\" + fileName + ".src", richTextBox_AdvanceParams.Text);
  805. //检查是否存在转换工具
  806. if (!Directory.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile"))
  807. {
  808. Directory.CreateDirectory(localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  809. myFtp.DownloadFile("Tools/GenerateParamsFile/ConsoleApp2.exe", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  810. myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.xml", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  811. myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.dll", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  812. }
  813. else
  814. {
  815. //程序使用最新版本
  816. if (System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "ConsoleApp2.exe"))
  817. System.IO.File.Delete(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "ConsoleApp2.exe");
  818. myFtp.DownloadFile("Tools/GenerateParamsFile/ConsoleApp2.exe", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  819. //库文件不更新
  820. if (!System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "Newtonsoft.Json.xml"))
  821. myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.xml", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  822. if (!System.IO.File.Exists(localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "Newtonsoft.Json.dll"))
  823. myFtp.DownloadFile("Tools/GenerateParamsFile/Newtonsoft.Json.dll", localInfo.ToolsPath + "\\" + "GenerateParamsFile");
  824. }
  825. //转换.ttcfg文件
  826. ProcessStartInfo startInfo = new ProcessStartInfo();
  827. startInfo.FileName = localInfo.ToolsPath + "\\" + "GenerateParamsFile" + "\\" + "ConsoleApp2.exe";
  828. startInfo.Arguments = "\"" + LocalPath + "\\" + fileName + ".src" + "\"";
  829. System.Diagnostics.Process.Start(startInfo);
  830. Thread.Sleep(1000);
  831. //保存页面
  832. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  833. Graphics g = Graphics.FromImage(bit);
  834. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  835. bit.Save(LocalPath + "\\" + fileName + ".png");
  836. //生成pdf文件,上传服务器
  837. //上传
  838. bool result1 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".src", ServerPath);
  839. bool result2 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".ttcfg", ServerPath);
  840. bool result3 = myFtp.UploadFile(LocalPath + "\\" + fileName + ".png", ServerPath);
  841. if (result1 & result2 & result3)
  842. {
  843. MessageBox.Show("上传成功", "提示", MessageBoxButtons.OK);
  844. }
  845. else
  846. {
  847. MessageBox.Show("上传失败", "提示", MessageBoxButtons.OK);
  848. }
  849. }
  850. }
  851. }