ImportForm.cs 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO.Ports;
  10. using System.Collections;
  11. using System.Threading;
  12. using System.Text.RegularExpressions;
  13. namespace MOTINOVA_Motor_Factory_Set
  14. {
  15. public partial class ImportForm : Form
  16. {
  17. #region 文件路径
  18. string LogSaveFileName = "";
  19. public string LogSavePath = "";
  20. public string LocalCfgFilePath = "";
  21. #endregion
  22. #region 测试参数配置
  23. //以下值在根据电机测试的配置文件中更改
  24. ushort MotorRadeSpeed = 1200; //空载转速
  25. ushort RateCadence = 100; //最高踏频
  26. ushort RateVoltage = 36000; //额定电压
  27. bool GasModeTest = true; //转把模式检测开关
  28. bool SpeedSensorTest = true; //速度传感器检测开关
  29. //以下值在软件测试文件params.cfg文件中修改
  30. ushort No_load_Current = 1000; //空载电流
  31. ushort SpeedSensorThData = 60; //车速传感器判断阈值
  32. ushort GasModeSpeed = 1200; //指拨模式测试转速
  33. #endregion
  34. #region 配置文件信息
  35. ArrayList array_CfgInfo = new ArrayList();
  36. #endregion
  37. #region 端口设置界面
  38. public static PortSettings portsettings_form = new PortSettings();
  39. #endregion
  40. #region 串口缓存
  41. public static List<byte> buffer = new List<byte>(4096);
  42. private byte[] binary_data_1 = new byte[1024];
  43. #endregion
  44. #region MC运行信息超时计数
  45. bool MC_RunInfo_Refresh = false;
  46. ushort MC_RunInfo_Refresh_Cnt = 0;
  47. #endregion
  48. #region MC故障码超时计数
  49. bool MC_ErrorCode_Refresh = false;
  50. ushort MC_ErrorCode_Refresh_Cnt = 0;
  51. #endregion
  52. #region 执行步骤
  53. public enum TestStep_Enum
  54. {
  55. Step_Init = 0,
  56. Step_SysClear = 1, //系统清除
  57. Step_Write = 2, //参数写入
  58. Step_Light = 3, //灯电压测试
  59. Step_Walk = 4, //推行测试
  60. Step_GasMode = 5, //转把测试
  61. Step_CheckVer = 6, //版本查询和确认
  62. Step_Save = 7 //数据读取和存储
  63. }
  64. TestStep_Enum TestStep;
  65. #endregion
  66. #region 反馈指令判断计时
  67. bool Ack_Flag = true;
  68. ushort Ack_Cnt = 0;
  69. #endregion
  70. #region 单步测试标志
  71. public enum StepTestResult_Enum
  72. {
  73. StepTestResult_Ready = 0,
  74. StepTestResult_Testing = 1,
  75. StepTestResult_OK = 2,
  76. StepTestResult_Fail = 3
  77. }
  78. StepTestResult_Enum StepTestFlag = StepTestResult_Enum.StepTestResult_Ready;
  79. #endregion
  80. #region 推行/转把测试计时
  81. ushort WalkProcess_Cnt = 0;
  82. ushort OK_cnt = 0;
  83. #endregion
  84. public ImportForm()
  85. {
  86. InitializeComponent();
  87. System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  88. //加载用户路径
  89. LogSavePath = StartForm.UserPath;
  90. LocalCfgFilePath = StartForm.UserPath;
  91. }
  92. #region 获取配置文件日期
  93. private void GetDateList()
  94. {
  95. string[] DateList = { "" };
  96. if (checkBox_LocalFile.Checked == true)//从本地更新日期列表
  97. {
  98. DateList = System.IO.Directory.GetDirectories(LocalCfgFilePath);
  99. }
  100. else//从服务器更新日期列表
  101. {
  102. DateList = StartForm.myFtp.GetDirectoryList(StartForm.myServerCfg.RootPath + "/cfg");
  103. }
  104. comboBox_cfgFileDate.Items.Clear();
  105. foreach (var file in DateList)
  106. {
  107. comboBox_cfgFileDate.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  108. }
  109. }
  110. #endregion
  111. #region 获取配置文件列表
  112. private void GetFileListFrom(string date)
  113. {
  114. string[] FileList = { "" };
  115. if (checkBox_LocalFile.Checked == true)//从本地更新文件列表
  116. {
  117. FileList = System.IO.Directory.GetFiles(LocalCfgFilePath + date);
  118. }
  119. else//从服务器更新文件列表
  120. {
  121. FileList = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/cfg/" + date);
  122. }
  123. comboBox_File.Items.Clear();
  124. foreach (var file in FileList)
  125. {
  126. comboBox_File.Items.Add(file.Substring(file.LastIndexOf("\\") + 1));
  127. }
  128. }
  129. #endregion
  130. #region 选择文件日期后更新文件列表
  131. private void comboBox_cfgFileDate_SelectionChangeCommitted(object sender, EventArgs e)
  132. {
  133. comboBox_File.Text = "";
  134. GetFileListFrom(comboBox_cfgFileDate.SelectedItem.ToString());
  135. }
  136. #endregion
  137. #region 选择文件后加载文件
  138. private void comboBox_File_SelectionChangeCommitted(object sender, EventArgs e)
  139. {
  140. //清空原来数据
  141. array_CfgInfo.Clear();
  142. string SelectFile = "";
  143. if (checkBox_LocalFile.Checked == false)//非本地模式从服务器下载文件
  144. {
  145. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  146. if (!File.Exists(LocalCfgFilePath + SelectFile))//检查本地是否存在选定文件
  147. {
  148. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "/" + comboBox_File.SelectedItem.ToString();
  149. StartForm.myFtp.DownloadFile(StartForm.myServerCfg.RootPath + "/cfg/" + SelectFile, LocalCfgFilePath + comboBox_cfgFileDate.SelectedItem.ToString());
  150. }
  151. }
  152. SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString();
  153. //打开选中文件
  154. StreamReader objReader = new StreamReader(LocalCfgFilePath + SelectFile);
  155. string sLine = "";
  156. while (sLine != null)
  157. {
  158. sLine = objReader.ReadLine();
  159. array_CfgInfo.Add(sLine);
  160. }
  161. objReader.Close();
  162. //解析显示配置文件
  163. richTextBox_CfgFile.Text = "";
  164. try
  165. {
  166. foreach (string sOutput in array_CfgInfo)
  167. {
  168. richTextBox_CfgFile.AppendText(sOutput + "\r\n");
  169. }
  170. }
  171. catch (System.Exception)
  172. {
  173. return;
  174. }
  175. //更新推行模式测试判断标准
  176. MotorRadeSpeed = Convert.ToUInt16(array_CfgInfo[42].ToString().Split(',')[1]);
  177. RateCadence = (ushort)(MotorRadeSpeed / 11U);
  178. RateVoltage = (ushort)(Convert.ToByte(array_CfgInfo[47].ToString().Split(',')[1]) * 1000);
  179. //更新指拨和速度传感器测试标志
  180. byte temp = Convert.ToByte(array_CfgInfo[25].ToString().Split(',')[1]);
  181. GasModeTest = temp == 0x55 ? false : temp == 0xAA ? true : false;
  182. temp = Convert.ToByte(array_CfgInfo[26].ToString().Split(',')[1]);
  183. SpeedSensorTest = temp == 0x55 || (temp == 0xAA ? false : temp == 0xEE ? false : false);
  184. //更新空载电流和车速传感器判断阈值
  185. if (array_CfgInfo.Count > 56)//旧版本无改配置项,采用默认值
  186. {
  187. No_load_Current = Convert.ToUInt16(array_CfgInfo[59].ToString().Split(',')[1]);
  188. SpeedSensorThData = Convert.ToUInt16(array_CfgInfo[60].ToString().Split(',')[1]);
  189. GasModeSpeed = Convert.ToUInt16(array_CfgInfo[61].ToString().Split(',')[1]);
  190. }
  191. }
  192. #endregion
  193. #region 修改获取文件方式,本地或服务器
  194. private void checkBox_LocalFile_CheckedChanged(object sender, EventArgs e)
  195. {
  196. comboBox_cfgFileDate.Text = "";
  197. comboBox_File.Text = "";
  198. richTextBox_CfgFile.Text = "";
  199. GetDateList();
  200. }
  201. #endregion
  202. private void ImportForm_Load(object sender, EventArgs e)
  203. {
  204. timer1.Enabled = true;
  205. timer2.Enabled = true;
  206. //装载配置文件
  207. GetDateList();
  208. //装载序列号文件
  209. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  210. StreamReader objReader = new StreamReader(cfg_file);
  211. string sLine = "";
  212. ArrayList arrText = new ArrayList();//创建一个动态数组
  213. while (sLine != null)
  214. {
  215. sLine = objReader.ReadLine();
  216. arrText.Add(sLine);
  217. }
  218. objReader.Close();
  219. label_SerialNum.Text = arrText[1].ToString();//流水号
  220. }
  221. private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e)
  222. {
  223. portsettings_form.Show();
  224. }
  225. #region CRC32校验
  226. static UInt32[] crc32_table = new UInt32[256]
  227. {
  228. 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  229. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
  230. 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7,
  231. 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
  232. 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3,
  233. 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  234. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF,
  235. 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
  236. 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB,
  237. 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1,
  238. 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  239. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
  240. 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4,
  241. 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE,
  242. 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08,
  243. 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  244. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC,
  245. 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6,
  246. 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050,
  247. 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
  248. 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  249. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637,
  250. 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1,
  251. 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
  252. 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5,
  253. 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  254. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9,
  255. 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
  256. 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD,
  257. 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7,
  258. 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  259. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
  260. 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2,
  261. 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8,
  262. 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E,
  263. 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  264. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A,
  265. 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0,
  266. 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676,
  267. 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
  268. 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  269. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668,
  270. 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  271. };
  272. static UInt32 crc32_cal(byte[] pData, UInt16 Length)
  273. {
  274. UInt32 nReg;
  275. UInt32 nTemp = 0;
  276. UInt16 i, n;
  277. nReg = 0xFFFFFFFF;
  278. for (n = 0; n < Length; n++)
  279. {
  280. nReg ^= (UInt32)pData[n];
  281. for (i = 0; i < 4; i++)
  282. {
  283. nTemp = crc32_table[((nReg >> 24) & 0xFF)];
  284. nReg <<= 8;
  285. nReg ^= nTemp;
  286. }
  287. }
  288. return nReg;
  289. }
  290. #endregion
  291. #region 命令发送
  292. private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data)
  293. {
  294. if (!portsettings_form.serialPort1.IsOpen)
  295. {
  296. MessageBox.Show("串口未连接");
  297. return;
  298. }
  299. ushort DataLength = (ushort)(Cmd & 0xFF);
  300. var SendCmdTmp = new byte[DataLength + 13];
  301. //帧头
  302. SendCmdTmp[0] = 0x55;
  303. SendCmdTmp[1] = 0xAA;
  304. //ID
  305. SendCmdTmp[2] = (byte)(ID >> 8);
  306. SendCmdTmp[3] = (byte)(ID & 0xFF);
  307. //MODE
  308. SendCmdTmp[4] = Mode;
  309. //长度
  310. SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2);
  311. //命令字
  312. SendCmdTmp[6] = (byte)(Cmd >> 8);
  313. SendCmdTmp[7] = (byte)(Cmd & 0xFF);
  314. //数据段
  315. for (UInt16 i = 0; i < (Cmd & 0xFF); i++)
  316. {
  317. SendCmdTmp[8 + i] = Data[i];
  318. }
  319. //校验
  320. UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8));
  321. SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24);
  322. SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256);
  323. SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256);
  324. SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256);
  325. //帧尾
  326. SendCmdTmp[DataLength + 12] = 0xF0;
  327. //发送数据
  328. portsettings_form.serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length);
  329. }
  330. #endregion
  331. #region 串口数据解析
  332. private void Serial_DataReceived_Process()
  333. {
  334. //串口解析
  335. byte DataNum = 0; //记录每条命令数据段的长度
  336. while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时
  337. {
  338. if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头
  339. {
  340. ushort PackageID;
  341. PackageID = (ushort)(buffer[2] * 256 + buffer[3]);
  342. if ((PackageID == 0x0710) || (PackageID == 0x0715) ||
  343. (PackageID == 0x0610) || (PackageID == 0x0615) ||
  344. (PackageID == 0x07FF) ||
  345. (PackageID == 0x0725) || (PackageID == 0x0720))
  346. {
  347. int CmdLen = buffer[5];
  348. if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环
  349. buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度
  350. DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度
  351. if (DataNum == (CmdLen - 2))
  352. {
  353. for (int i = 0; i < DataNum + 2; i++)
  354. {
  355. binary_data_1[i] = buffer[i];
  356. }
  357. if (TestStep == TestStep_Enum.Step_Save)
  358. {
  359. //数据存储
  360. var RecData = new byte[CmdLen];
  361. Array.Copy(binary_data_1, RecData, CmdLen);
  362. if ((PackageID == 0x0710) || (PackageID == 0x0715) ||
  363. (PackageID == 0x0610) || (PackageID == 0x0615) ||
  364. (PackageID == 0x07FF))
  365. {
  366. DataSave(RecData);
  367. }
  368. }
  369. DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
  370. }
  371. buffer.RemoveRange(0, CmdLen + 5);//解析完成
  372. }
  373. else
  374. {
  375. buffer.RemoveAt(0);//ID不对
  376. }
  377. }
  378. else
  379. {
  380. buffer.RemoveAt(0);//帧头不对,删除帧头高字节
  381. }
  382. }
  383. }
  384. #endregion
  385. #region 数据存储
  386. private void DataSave(byte[] Data)
  387. {
  388. string SaveData = "";
  389. //数据存储
  390. for (int i = 0; i < Data.Length; i++)
  391. {
  392. SaveData += Convert.ToString(Data[i], 16).PadLeft(2, '0').ToUpper() + " ";
  393. }
  394. SaveData += "\r\n";
  395. System.IO.File.AppendAllText(LogSaveFileName, SaveData);
  396. }
  397. #endregion
  398. //故障转换和显示
  399. List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
  400. "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
  401. "",""};
  402. private void ErrorInfoDisplay(UInt32 Code)
  403. {
  404. label_ErrorInfo.Text = "";
  405. if (Code == 0)
  406. {
  407. label_ErrorInfo.Text = "无故障";
  408. label_ErrorInfo.ForeColor = Color.Green;
  409. }
  410. else
  411. {
  412. label_ErrorInfo.ForeColor = Color.Red;
  413. for (int i = 0; i < 32; i++)
  414. {
  415. if ((Code & 0x01) == 0x01)
  416. {
  417. label_ErrorInfo.Text += ErrorInfo[i] + " ";
  418. }
  419. Code >>= 1;
  420. }
  421. }
  422. }
  423. #region 数据解析处理
  424. private void DataCmdProcess(ushort ID)
  425. {
  426. ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]);
  427. ushort DataTemp;
  428. #region 主控信息
  429. if ((ID == 0x715) || (ID == 0x710))
  430. {
  431. switch (CmdTemp)
  432. {
  433. case 0x1020://电机运行信息
  434. {
  435. unchecked
  436. {
  437. this.Invoke((EventHandler)(delegate
  438. {
  439. MC_RunInfo_Refresh = true;
  440. //车速
  441. DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
  442. textBox_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0");
  443. //输出转速
  444. DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
  445. textBox_MotorSpeed.Text = Convert.ToString(DataTemp);
  446. //母线电压
  447. DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
  448. textBox_Vol.Text = Convert.ToString(DataTemp);
  449. //母线电流
  450. DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
  451. textBox_Cur.Text = Convert.ToString(DataTemp);
  452. //踏频
  453. DataTemp = (ushort)(binary_data_1[12]);
  454. textBox_Cadence.Text = Convert.ToString(DataTemp);
  455. //踩踏力矩
  456. DataTemp = (ushort)(binary_data_1[13]);
  457. textBox_Torque.Text = Convert.ToString(DataTemp);
  458. //踩踏方向
  459. DataTemp = (ushort)(binary_data_1[14]);
  460. if (DataTemp == 0)
  461. {
  462. textBox_Dir.Text = "正向";
  463. }
  464. else if (DataTemp == 1)
  465. {
  466. textBox_Dir.Text = "反向";
  467. }
  468. else if (DataTemp == 2)
  469. {
  470. textBox_Dir.Text = "停止";
  471. }
  472. //PCB温度
  473. DataTemp = (ushort)(binary_data_1[23]);
  474. textBox_PCB.Text = Convert.ToString((int)(DataTemp - 40));
  475. //绕组温度
  476. DataTemp = (ushort)(binary_data_1[24]);
  477. textBox_Coil.Text = Convert.ToString((int)(DataTemp - 40));
  478. //MCU温度
  479. DataTemp = (ushort)(binary_data_1[25]);
  480. textBox_MCU.Text = Convert.ToString((int)(DataTemp - 40));
  481. }));
  482. }
  483. break;
  484. }
  485. case 0x1104://故障码
  486. {
  487. unchecked
  488. {
  489. this.Invoke((EventHandler)(delegate
  490. {
  491. MC_ErrorCode_Refresh = true;
  492. label_error_code.Text =
  493. Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() +
  494. Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() +
  495. Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() +
  496. Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() +
  497. "H";
  498. label_error_code.BackColor = Color.Red;
  499. UInt32 ErrorCode = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
  500. ErrorInfoDisplay(ErrorCode);
  501. }));
  502. }
  503. break;
  504. }
  505. case 0x1240://电机版本信息
  506. {
  507. unchecked
  508. {
  509. this.Invoke((EventHandler)(delegate
  510. {
  511. //电机型号
  512. richTextBox_Ver.Text += "型号:";
  513. for (ushort i = 0; i < 16; i++)
  514. {
  515. if (binary_data_1[2 + i] == 0x2E)
  516. {
  517. break;
  518. }
  519. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  520. }
  521. richTextBox_Ver.Text += "\r\n";
  522. //电机SN
  523. richTextBox_Ver.Text += "序列号:";
  524. for (ushort i = 0; i < 16; i++)
  525. {
  526. if (binary_data_1[18 + i] == 0x2E)
  527. {
  528. break;
  529. }
  530. richTextBox_Ver.Text += ((char)binary_data_1[18 + i]).ToString();
  531. }
  532. richTextBox_Ver.Text += "\r\n";
  533. //电机HW
  534. richTextBox_Ver.Text += "硬件版本:";
  535. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  536. {
  537. if (binary_data_1[34 + i] == 0x2E)
  538. {
  539. break;
  540. }
  541. if (binary_data_1[34 + i] == 'r')
  542. {
  543. binary_data_1[34 + i] = (byte)'.';
  544. }
  545. richTextBox_Ver.Text += ((char)binary_data_1[34 + i]).ToString();
  546. }
  547. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  548. {
  549. case (byte)'1':
  550. richTextBox_Ver.Text += " " + "ST" + " "; break;
  551. case (byte)'2':
  552. richTextBox_Ver.Text += " " + "APM" + " "; break;
  553. case (byte)'3':
  554. richTextBox_Ver.Text += " " + "HK" + " "; break;
  555. case (byte)'4':
  556. richTextBox_Ver.Text += " " + "GD" + " "; break;
  557. default:
  558. richTextBox_Ver.Text += " " + "NULL" + " "; break;
  559. }
  560. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  561. {
  562. richTextBox_Ver.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  563. }
  564. richTextBox_Ver.Text += "\r\n";
  565. //电机FW
  566. richTextBox_Ver.Text += "软件版本:";
  567. for (ushort i = 0; i < 16; i++)
  568. {
  569. if (binary_data_1[50 + i] == 0x2E)
  570. {
  571. break;
  572. }
  573. if (binary_data_1[50 + i] == 'r')
  574. {
  575. binary_data_1[50 + i] = (byte)'.';
  576. }
  577. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  578. }
  579. richTextBox_Ver.Text += "\r\n";
  580. }));
  581. }
  582. break;
  583. }
  584. case 0x1720://电机程序特性信息
  585. {
  586. unchecked
  587. {
  588. this.Invoke((EventHandler)(delegate
  589. {
  590. //程序特性
  591. for (ushort i = 0; i < 32; i++)
  592. {
  593. if (binary_data_1[2 + i] == 0x2E)
  594. {
  595. break;
  596. }
  597. if (binary_data_1[2 + i] == 'r')
  598. {
  599. binary_data_1[2 + i] = (byte)'.';
  600. }
  601. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  602. }
  603. richTextBox_Ver.Text += "\r\n";
  604. }));
  605. }
  606. break;
  607. }
  608. case 0xA903://反馈指令
  609. {
  610. unchecked
  611. {
  612. this.Invoke((EventHandler)(delegate
  613. {
  614. Ack_Flag = true;
  615. StepTestFlag = StepTestResult_Enum.StepTestResult_OK;
  616. }));
  617. }
  618. break;
  619. }
  620. default: break;
  621. }
  622. }
  623. #endregion
  624. #region TE
  625. else if ((ID == 0x615) || (ID == 0x610))
  626. {
  627. switch (CmdTemp)
  628. {
  629. case 0x1240://TE APP版本信息
  630. {
  631. unchecked
  632. {
  633. this.Invoke((EventHandler)(delegate
  634. {
  635. //TE 软件版本
  636. richTextBox_Ver.Text += "TE软件版本:";
  637. for (ushort i = 0; i < 16; i++)
  638. {
  639. if (binary_data_1[50 + i] == 0x2E)
  640. {
  641. break;
  642. }
  643. if (binary_data_1[50 + i] == 'r')
  644. {
  645. binary_data_1[50 + i] = (byte)'.';
  646. }
  647. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  648. }
  649. }));
  650. }
  651. break;
  652. }
  653. default: break;
  654. }
  655. }
  656. #endregion
  657. #region //电池信息
  658. else if ((ID == 0x725) || (ID == 0x720))
  659. {
  660. label_BMS_COM.BackColor = Color.Green;
  661. label_BMS_COM.Text = "OK";
  662. }
  663. #endregion
  664. }
  665. #endregion
  666. //20ms定时器
  667. private void timer1_Tick(object sender, EventArgs e)
  668. {
  669. //串口数据解析
  670. Serial_DataReceived_Process();
  671. }
  672. #region 非独占性延时函数
  673. public static void Delay_ms(int milliSecond)
  674. {
  675. int start = Environment.TickCount;
  676. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  677. {
  678. Application.DoEvents();
  679. }
  680. }
  681. #endregion
  682. //电机运行信息清除
  683. private void MC_RunInfo_Clear()
  684. {
  685. foreach (Control c in groupBox9.Controls)
  686. {
  687. if (c is TextBox)
  688. {
  689. c.Text = "---";
  690. }
  691. }
  692. }
  693. private void ImportForm_FormClosing(object sender, FormClosingEventArgs e)
  694. {
  695. portsettings_form.serialPort1.Close();
  696. portsettings_form.comboBox_ComIndex.Enabled = true;
  697. portsettings_form.button_ComOpen.Text = "连接";
  698. portsettings_form.g_blnIsOpen = false;
  699. }
  700. //开关灯
  701. private void LightProcess()
  702. {
  703. var CtrlCode = new byte[2];
  704. CtrlCode[0] = 0x00;
  705. if (button_LightCtl.Text == "开灯")
  706. {
  707. button_LightCtl.Text = "关灯";
  708. CtrlCode[1] = 0xF1;
  709. button_LightCtl.BackColor = Color.Yellow;
  710. button_LightCtl.ForeColor = Color.Black;
  711. }
  712. else
  713. {
  714. button_LightCtl.Text = "开灯";
  715. CtrlCode[1] = 0xF0;
  716. button_LightCtl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  717. button_LightCtl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  718. }
  719. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  720. }
  721. //开关灯按钮
  722. private void button_LightCtl_Click(object sender, EventArgs e)
  723. {
  724. LightProcess();
  725. }
  726. //电机启动/停止控制
  727. private void Walk_Process(byte GearSt)
  728. {
  729. var Code = new byte[2];
  730. if (button_Walk.Text == "启动")
  731. {
  732. button_Walk.Text = "停止";
  733. //发送指令使电机进入配置模式
  734. Code[0] = 0x01;
  735. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  736. Thread.Sleep(200);
  737. //发送指令进入设定档位
  738. Code[0] = GearSt;
  739. if (button_LightCtl.Text == "关灯")
  740. Code[1] = 0xF1;
  741. else
  742. Code[1] = 0xF0;
  743. button_Walk.BackColor = Color.Yellow;
  744. button_Walk.ForeColor = Color.Black;
  745. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  746. }
  747. else
  748. {
  749. button_Walk.Text = "启动";
  750. //发送指令关闭档位模式
  751. Code[0] = 0x00;
  752. if (button_LightCtl.Text == "关灯")
  753. Code[1] = 0xF1;
  754. else
  755. Code[1] = 0xF0;
  756. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  757. Thread.Sleep(200);
  758. //发送指令使电机退出配置模式
  759. Code[0] = 0x00;
  760. button_Walk.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  761. button_Walk.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  762. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  763. }
  764. }
  765. //电机启动/停止按钮
  766. private void button_Walk_Click(object sender, EventArgs e)
  767. {
  768. Walk_Process(0x22);
  769. }
  770. //查询版本信息
  771. private void button_Check_Ver_Click(object sender, EventArgs e)
  772. {
  773. richTextBox_Ver.Text = "";
  774. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  775. Delay_ms(200);
  776. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  777. }
  778. //按钮初始化
  779. private void label_Init()
  780. {
  781. radioButton_Light_OK.Checked = false;//灯电压确认按钮
  782. radioButton_Light_OK.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  783. radioButton_Light_OK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  784. radioButton_Light_NG.Checked = false;
  785. radioButton_Light_NG.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  786. radioButton_Light_NG.ForeColor= System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  787. label_error_code.BackColor = Color.Green;//故障码
  788. label_error_code.Text = "OK";
  789. label_Sta_Clear.BackColor = Color.Red;//系统清除
  790. label_Sta_Clear.Text = "NG";
  791. label_Sta_Write.BackColor = Color.Red;//参数写入
  792. label_Sta_Write.Text = "NG";
  793. label_sta_light.BackColor = Color.Red;//灯电压
  794. label_sta_light.Text = "NG";
  795. label_sensor_vol.BackColor = Color.Red;//电压检测
  796. label_sensor_vol.Text = "NG";
  797. label_sensor_cur.BackColor = Color.Red;//电流检测
  798. label_sensor_cur.Text = "NG";
  799. label_sensor_speed.BackColor = Color.Red;//车速检测
  800. label_sensor_speed.Text = "NG";
  801. label_sensor_cadence.BackColor = Color.Red;//踏频检测
  802. label_sensor_cadence.Text = "NG";
  803. label_sensor_motor.BackColor = Color.Red;//电机转速检测
  804. label_sensor_motor.Text = "NG";
  805. label_sensor_PCB_temp.BackColor = Color.Red;//PCB温度检测
  806. label_sensor_PCB_temp.Text = "NG";
  807. label_sensor_coil_temp.BackColor = Color.Red;//绕组温度检测
  808. label_sensor_coil_temp.Text = "NG";
  809. label_sensor_MCU_temp.BackColor = Color.Red;//MCU温度检测
  810. label_sensor_MCU_temp.Text = "NG";
  811. label_Sensor_Break1.BackColor = Color.Red;//断电刹把
  812. label_Sensor_Break1.Text = "NG";
  813. label_Sensor_Break2.BackColor = Color.Red;//断电刹把
  814. label_Sensor_Break2.Text = "NG";
  815. label_Sensor_Gas.BackColor = Color.Red;//转把
  816. label_Sensor_Gas.Text = "NG";
  817. label_FW_Version.BackColor = Color.Red;//版本信息
  818. label_FW_Version.Text = "NG";
  819. label_BMS_COM.BackColor = Color.Red;//BMS通信
  820. label_BMS_COM.Text = "NG";
  821. label_Result.BackColor = Color.Red;//测试结果
  822. label_Result.Text = "不合格";
  823. richTextBox_Ver.Text = "";//版本信息
  824. richTextBox_TestRecord.Text = "";//测试过程记录
  825. }
  826. //测试失败存储测试界面
  827. private void ErrorSaveLog()
  828. {
  829. //页面存储
  830. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  831. Graphics g = Graphics.FromImage(bit);
  832. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  833. LogSaveFileName = LogSavePath + "\\errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  834. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  835. System.IO.Directory.CreateDirectory(LogSaveFileName);
  836. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  837. bit.Save(LogSaveFileName);
  838. //测试页面上传服务器
  839. //if (StartForm.myFtp.IsNetConnected == true)
  840. //{
  841. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  842. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/", DateNow) == false)
  843. // {
  844. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  845. // }
  846. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  847. // if (result == true)
  848. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  849. //}
  850. }
  851. //执行一次测试任务
  852. private void Thread_Task()
  853. {
  854. //开始测试
  855. richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n";
  856. richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n";
  857. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n";
  858. //公有变量
  859. var Code = new byte[32];//发送的指令数据
  860. ushort DataTemp;
  861. short DataTemp_Int16;
  862. string ResultPathName = "";
  863. #region Step1:发送指令,系统清除
  864. TestStep = TestStep_Enum.Step_SysClear;
  865. label_Step.Text = "系统清除,请等待!";
  866. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送系统清除指令" + "\r\n";
  867. Array.Clear(Code, 0, 32);
  868. Code[0] = (byte)'C';
  869. Code[1] = (byte)'L';
  870. Code[2] = (byte)'E';
  871. Code[3] = (byte)'A';
  872. Code[4] = (byte)'R';
  873. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  874. Ack_Flag = false;
  875. Ack_Cnt = 0;
  876. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  877. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing);//等待测试结束
  878. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  879. {
  880. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除成功" + "\r\n";
  881. label_Sta_Clear.BackColor = Color.Green;
  882. label_Sta_Clear.Text = "OK";
  883. }
  884. else//失败
  885. {
  886. label_Step.Text = "系统清除失败,测试结束!";
  887. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除失败,测试结束" + "\r\n";
  888. button_Start.Enabled = true;
  889. ErrorSaveLog();
  890. return;
  891. }
  892. #endregion
  893. //系统清除完成后会重启,等待3s
  894. Delay_ms(3000);
  895. #region Step2:发送指令,依次写入参数
  896. TestStep = TestStep_Enum.Step_Write;
  897. label_Step.Text = "参数写入中,请等待!";
  898. #region Step2.1:写入电机型号和序列号
  899. string Mode = textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' '));
  900. string SN = textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1);
  901. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机型号" + "\r\n";
  902. Array.Clear(Code, 0, 32);
  903. for (ushort i = 0; i < ((Mode.Length > 16) ? 16 : Mode.Length); i++)
  904. {
  905. Code[i] = (byte)Mode[i];
  906. }
  907. if (Mode.Length < 16)
  908. {
  909. Code[Mode.Length] = (byte)'.';
  910. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  911. {
  912. Code[Mode.Length + 1 + i] = 0x20;
  913. }
  914. }
  915. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, Code);
  916. Ack_Flag = false;
  917. Ack_Cnt = 0;
  918. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  919. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  920. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  921. {
  922. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入成功" + "\r\n";
  923. }
  924. else//失败
  925. {
  926. label_Step.Text = "参数写入失败,测试结束!";
  927. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入失败,测试结束" + "\r\n";
  928. button_Start.Enabled = true;
  929. ErrorSaveLog();
  930. return;
  931. }
  932. Delay_ms(200);
  933. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机序列号" + "\r\n";
  934. Array.Clear(Code, 0, 32);
  935. for (ushort i = 0; i < ((SN.Length > 16) ? 16 : SN.Length); i++)
  936. {
  937. Code[i] = (byte)SN[i];
  938. }
  939. if (SN.Length < 16)
  940. {
  941. Code[SN.Length] = (byte)'.';
  942. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  943. {
  944. Code[SN.Length + 1 + i] = 0x20;
  945. }
  946. }
  947. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, Code);
  948. Ack_Flag = false;
  949. Ack_Cnt = 0;
  950. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  951. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  952. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  953. {
  954. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入成功" + "\r\n";
  955. }
  956. else//失败
  957. {
  958. label_Step.Text = "参数写入失败,测试结束!";
  959. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入失败,测试结束" + "\r\n";
  960. button_Start.Enabled = true;
  961. ErrorSaveLog();
  962. return;
  963. }
  964. #endregion
  965. #region Step2.2:写入用户参数1
  966. Delay_ms(200);
  967. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数1" + "\r\n";
  968. Array.Clear(Code, 0, 32);
  969. //指拨模式
  970. Code[0] = Convert.ToByte(array_CfgInfo[25].ToString().Split(',')[1]);
  971. //启动模式
  972. Code[1] = Convert.ToByte(array_CfgInfo[1].ToString().Split(',')[1]);
  973. //停机时间
  974. DataTemp = Convert.ToUInt16(array_CfgInfo[2].ToString().Split(',')[1]);
  975. Code[2] = (byte)(DataTemp & 0xFF);
  976. Code[3] = (byte)(DataTemp >> 8);
  977. //限速
  978. Code[4] = Convert.ToByte(array_CfgInfo[3].ToString().Split(',')[1]);
  979. //下降速度
  980. Code[5] = Convert.ToByte(array_CfgInfo[4].ToString().Split(',')[1]);
  981. //前飞
  982. Code[6] = Convert.ToByte(array_CfgInfo[5].ToString().Split(',')[1]);
  983. //后飞
  984. Code[7] = Convert.ToByte(array_CfgInfo[6].ToString().Split(',')[1]);
  985. //限流
  986. Code[8] = Convert.ToByte(array_CfgInfo[7].ToString().Split(',')[1]);
  987. //温度预警
  988. Code[9] = (byte)(Convert.ToByte(array_CfgInfo[8].ToString().Split(',')[1]) + 40);
  989. //温度保护
  990. Code[10] = (byte)(Convert.ToByte(array_CfgInfo[9].ToString().Split(',')[1]) + 40);
  991. //无PBU
  992. Code[11] = Convert.ToByte(array_CfgInfo[10].ToString().Split(',')[1]);
  993. //轮胎周长
  994. Code[12] = Convert.ToByte(array_CfgInfo[11].ToString().Split(',')[1]);
  995. //电机系列号
  996. Code[13] = Convert.ToByte(array_CfgInfo[12].ToString().Split(',')[1]);
  997. //ECO助力增益
  998. Code[14] = Convert.ToByte(array_CfgInfo[13].ToString().Split(',')[1]);
  999. //ECO加速
  1000. Code[15] = Convert.ToByte(array_CfgInfo[14].ToString().Split(',')[1]);
  1001. //NORM助力增益
  1002. Code[16] = Convert.ToByte(array_CfgInfo[15].ToString().Split(',')[1]);
  1003. //NORM加速
  1004. Code[17] = Convert.ToByte(array_CfgInfo[16].ToString().Split(',')[1]);
  1005. //SPORT助力增益
  1006. Code[18] = Convert.ToByte(array_CfgInfo[17].ToString().Split(',')[1]);
  1007. //SPORT加速
  1008. Code[19] = Convert.ToByte(array_CfgInfo[18].ToString().Split(',')[1]);
  1009. //TURBO助力增益
  1010. Code[20] = Convert.ToByte(array_CfgInfo[19].ToString().Split(',')[1]);
  1011. //TURBO加速
  1012. Code[21] = Convert.ToByte(array_CfgInfo[20].ToString().Split(',')[1]);
  1013. //SMART助力增益
  1014. Code[22] = Convert.ToByte(array_CfgInfo[21].ToString().Split(',')[1]);
  1015. //SMART加速
  1016. Code[23] = Convert.ToByte(array_CfgInfo[22].ToString().Split(',')[1]);
  1017. //速度传感器信号个数
  1018. Code[24] = Convert.ToByte(array_CfgInfo[23].ToString().Split(',')[1]);
  1019. //踏频启动信号个数
  1020. Code[25] = Convert.ToByte(array_CfgInfo[24].ToString().Split(',')[1]);
  1021. //速度传感器
  1022. Code[26] = Convert.ToByte(array_CfgInfo[26].ToString().Split(',')[1]);
  1023. //周长微调
  1024. Code[27] = (byte)Convert.ToInt16(array_CfgInfo[27].ToString().Split(',')[1]);
  1025. //低压保护阈值
  1026. DataTemp = Convert.ToUInt16(array_CfgInfo[28].ToString().Split(',')[1]);
  1027. Code[28] = (byte)(DataTemp & 0xFF);
  1028. Code[29] = (byte)(DataTemp >> 8);
  1029. //推行限速
  1030. DataTemp = Convert.ToByte(array_CfgInfo[29].ToString().Split(',')[1]);
  1031. Code[30] = (byte)Convert.ToByte(DataTemp);
  1032. //推行马达转速
  1033. Code[31] = (byte)Convert.ToByte(array_CfgInfo[30].ToString().Split(',')[1]);
  1034. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, Code);
  1035. Ack_Flag = false;
  1036. Ack_Cnt = 0;
  1037. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1038. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1039. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1040. {
  1041. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入成功" + "\r\n";
  1042. }
  1043. else//失败
  1044. {
  1045. label_Step.Text = "参数写入失败,测试结束!";
  1046. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入失败,测试结束" + "\r\n";
  1047. button_Start.Enabled = true;
  1048. ErrorSaveLog();
  1049. return;
  1050. }
  1051. #endregion
  1052. #region Step2.3:写入用户参数2
  1053. Delay_ms(200);
  1054. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数2" + "\r\n";
  1055. Array.Clear(Code, 0, 32);
  1056. //俯仰角零偏
  1057. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[34].ToString().Split(',')[1]) * 10);
  1058. Code[0] = (byte)(DataTemp_Int16 & 0xFF);
  1059. Code[1] = (byte)(DataTemp_Int16 >> 8);
  1060. //横滚角零偏
  1061. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[35].ToString().Split(',')[1]) * 10);
  1062. Code[2] = (byte)(DataTemp_Int16 & 0xFF);
  1063. Code[3] = (byte)(DataTemp_Int16 >> 8);
  1064. //支持姿态传感器
  1065. Code[4] = Convert.ToByte(array_CfgInfo[33].ToString().Split(',')[1]);
  1066. //尾灯模式
  1067. Code[5] = Convert.ToByte(array_CfgInfo[36].ToString().Split(',')[1]);
  1068. //前灯电压
  1069. Code[6] = Convert.ToByte(array_CfgInfo[37].ToString().Split(',')[1]);
  1070. //后灯电压
  1071. Code[7] = Convert.ToByte(array_CfgInfo[38].ToString().Split(',')[1]);
  1072. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, Code);
  1073. Ack_Flag = false;
  1074. Ack_Cnt = 0;
  1075. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1076. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1077. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1078. {
  1079. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入成功" + "\r\n";
  1080. }
  1081. else//失败
  1082. {
  1083. label_Step.Text = "参数写入失败,测试结束!";
  1084. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入失败,测试结束" + "\r\n";
  1085. button_Start.Enabled = true;
  1086. ErrorSaveLog();
  1087. return;
  1088. }
  1089. #endregion
  1090. #region Step2.4:写入马达参数
  1091. Delay_ms(200);
  1092. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入马达参数" + "\r\n";
  1093. Array.Clear(Code, 0, 32);
  1094. //额定功率
  1095. DataTemp = Convert.ToUInt16(array_CfgInfo[41].ToString().Split(',')[1]);
  1096. Code[0] = (byte)(DataTemp & 0xFF);
  1097. Code[1] = (byte)(DataTemp >> 8);
  1098. //额定转速
  1099. DataTemp = Convert.ToUInt16(array_CfgInfo[42].ToString().Split(',')[1]);
  1100. Code[2] = (byte)(DataTemp & 0xFF);
  1101. Code[3] = (byte)(DataTemp >> 8);
  1102. //定子电阻
  1103. DataTemp = Convert.ToUInt16(array_CfgInfo[43].ToString().Split(',')[1]);
  1104. Code[4] = (byte)(DataTemp & 0xFF);
  1105. Code[5] = (byte)(DataTemp >> 8);
  1106. //Lq
  1107. DataTemp = Convert.ToUInt16(array_CfgInfo[44].ToString().Split(',')[1]);
  1108. Code[6] = (byte)(DataTemp & 0xFF);
  1109. Code[7] = (byte)(DataTemp >> 8);
  1110. //Ld
  1111. DataTemp = Convert.ToUInt16(array_CfgInfo[45].ToString().Split(',')[1]);
  1112. Code[8] = (byte)(DataTemp & 0xFF);
  1113. Code[9] = (byte)(DataTemp >> 8);
  1114. //反电动势
  1115. DataTemp = Convert.ToUInt16(array_CfgInfo[46].ToString().Split(',')[1]);
  1116. Code[10] = (byte)(DataTemp & 0xFF);
  1117. Code[11] = (byte)(DataTemp >> 8);
  1118. //额定电压
  1119. Code[12] = Convert.ToByte(array_CfgInfo[47].ToString().Split(',')[1]);
  1120. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, Code);
  1121. Ack_Flag = false;
  1122. Ack_Cnt = 0;
  1123. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1124. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1125. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1126. {
  1127. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入成功" + "\r\n";
  1128. }
  1129. else//失败
  1130. {
  1131. label_Step.Text = "参数写入失败,测试结束!";
  1132. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入失败,测试结束" + "\r\n";
  1133. button_Start.Enabled = true;
  1134. ErrorSaveLog();
  1135. return;
  1136. }
  1137. #endregion
  1138. #region Step2.5:写入生产信息
  1139. Delay_ms(200);
  1140. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入生产信息" + "\r\n";
  1141. Array.Clear(Code, 0, 32);
  1142. string MAC_Info = array_CfgInfo[50].ToString().Split(',')[1];
  1143. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1144. {
  1145. Code[i] = (byte)MAC_Info[i];
  1146. }
  1147. MAC_Info = array_CfgInfo[51].ToString().Split(',')[1];
  1148. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1149. {
  1150. Code[8 + i] = (byte)MAC_Info[i];
  1151. }
  1152. MAC_Info = array_CfgInfo[52].ToString().Split(',')[1];
  1153. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1154. {
  1155. Code[16 + i] = (byte)MAC_Info[i];
  1156. }
  1157. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, Code);
  1158. Ack_Flag = false;
  1159. Ack_Cnt = 0;
  1160. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1161. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1162. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1163. {
  1164. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入成功" + "\r\n";
  1165. }
  1166. else//失败
  1167. {
  1168. label_Step.Text = "参数写入失败,测试结束!";
  1169. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入失败,测试结束" + "\r\n";
  1170. button_Start.Enabled = true;
  1171. ErrorSaveLog();
  1172. return;
  1173. }
  1174. #endregion
  1175. #region Step2.6:写入校验密钥
  1176. Delay_ms(200);
  1177. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入校验密钥" + "\r\n";
  1178. Array.Clear(Code, 0, 32);
  1179. if (array_CfgInfo[53].ToString().EndsWith(",") == false)
  1180. {
  1181. string KeyString = array_CfgInfo[53].ToString().Split(',')[1];
  1182. for (ushort i = 0; i < ((KeyString.Length > 8) ? 8 : KeyString.Length); i++)
  1183. {
  1184. Code[i] = (byte)KeyString[i];
  1185. }
  1186. }
  1187. else
  1188. {
  1189. Code.Initialize();
  1190. }
  1191. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, Code);
  1192. Ack_Flag = false;
  1193. Ack_Cnt = 0;
  1194. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1195. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1196. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1197. {
  1198. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入成功" + "\r\n";
  1199. }
  1200. else//失败
  1201. {
  1202. label_Step.Text = "参数写入失败,测试结束!";
  1203. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入失败,测试结束" + "\r\n";
  1204. button_Start.Enabled = true;
  1205. ErrorSaveLog();
  1206. return;
  1207. }
  1208. #endregion
  1209. #region Step2.7:写入自定义信息1、2、3
  1210. //自定义信息1
  1211. Delay_ms(200);
  1212. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息1" + "\r\n";
  1213. Array.Clear(Code, 0, 32);
  1214. if (array_CfgInfo[54].ToString().EndsWith(",") == false)
  1215. {
  1216. string UserInfo1String = array_CfgInfo[54].ToString().Split(',')[1];
  1217. for (ushort i = 0; i < (UserInfo1String.Length > 16 ? 16 : UserInfo1String.Length); i++)
  1218. {
  1219. Code[i] = (byte)UserInfo1String[i];
  1220. }
  1221. }
  1222. else
  1223. {
  1224. Array.Clear(Code, 0, 16);
  1225. }
  1226. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, Code);
  1227. Ack_Flag = false;
  1228. Ack_Cnt = 0;
  1229. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1230. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1231. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1232. {
  1233. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入成功" + "\r\n";
  1234. }
  1235. else//失败
  1236. {
  1237. label_Step.Text = "参数写入失败,测试结束!";
  1238. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入失败,测试结束" + "\r\n";
  1239. button_Start.Enabled = true;
  1240. ErrorSaveLog();
  1241. return;
  1242. }
  1243. //自定义信息2
  1244. Delay_ms(200);
  1245. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息2" + "\r\n";
  1246. Array.Clear(Code, 0, 32);
  1247. if (array_CfgInfo[55].ToString().EndsWith(",") == false)
  1248. {
  1249. string UserInfo2String = array_CfgInfo[55].ToString().Split(',')[1];
  1250. for (ushort i = 0; i < (UserInfo2String.Length > 16 ? 16 : UserInfo2String.Length); i++)
  1251. {
  1252. Code[i] = (byte)UserInfo2String[i];
  1253. }
  1254. }
  1255. else
  1256. {
  1257. Array.Clear(Code, 0, 16);
  1258. }
  1259. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, Code);
  1260. Ack_Flag = false;
  1261. Ack_Cnt = 0;
  1262. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1263. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1264. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1265. {
  1266. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入成功" + "\r\n";
  1267. }
  1268. else//失败
  1269. {
  1270. label_Step.Text = "参数写入失败,测试结束!";
  1271. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入失败,测试结束" + "\r\n";
  1272. button_Start.Enabled = true;
  1273. ErrorSaveLog();
  1274. return;
  1275. }
  1276. //自定义信息3
  1277. Delay_ms(200);
  1278. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息3" + "\r\n";
  1279. Array.Clear(Code, 0, 32);
  1280. if (array_CfgInfo[56].ToString().EndsWith(",") == false)
  1281. {
  1282. string UserInfo3String = array_CfgInfo[56].ToString().Split(',')[1];
  1283. for (ushort i = 0; i < (UserInfo3String.Length > 16 ? 16 : UserInfo3String.Length); i++)
  1284. {
  1285. Code[i] = (byte)UserInfo3String[i];
  1286. }
  1287. }
  1288. else
  1289. {
  1290. Array.Clear(Code, 0, 16);
  1291. }
  1292. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, Code);
  1293. Ack_Flag = false;
  1294. Ack_Cnt = 0;
  1295. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1296. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1297. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1298. {
  1299. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入成功" + "\r\n";
  1300. }
  1301. else//失败
  1302. {
  1303. label_Step.Text = "参数写入失败,测试结束!";
  1304. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入失败,测试结束" + "\r\n";
  1305. button_Start.Enabled = true;
  1306. ErrorSaveLog();
  1307. return;
  1308. }
  1309. #endregion
  1310. //参数写入完成
  1311. label_Sta_Write.BackColor = Color.Green;
  1312. label_Sta_Write.Text = "OK";
  1313. #endregion
  1314. #region Step3:灯电压测试
  1315. TestStep = TestStep_Enum.Step_Light;
  1316. label_Step.Text = "灯电压测试,请确认电压!";
  1317. button_LightCtl.Text = "开灯";
  1318. LightProcess();
  1319. radioButton_Light_OK.Checked = false;
  1320. radioButton_Light_OK.BackColor = Color.Green;
  1321. radioButton_Light_OK.ForeColor = Color.White;
  1322. radioButton_Light_OK.Enabled = true;
  1323. radioButton_Light_NG.Checked = false;
  1324. radioButton_Light_NG.Enabled = true;
  1325. radioButton_Light_NG.BackColor = Color.Red;
  1326. radioButton_Light_NG.ForeColor = Color.White;
  1327. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送开灯指令,请确认灯电压是否正确……" + "\r\n";
  1328. while ((radioButton_Light_OK.Checked == false) && (radioButton_Light_NG.Checked == false)) ;
  1329. if (radioButton_Light_OK.Checked == true)
  1330. {
  1331. label_sta_light.BackColor = Color.Green;
  1332. radioButton_Light_NG.BackColor = Color.Gray;
  1333. label_sta_light.Text = "OK";
  1334. radioButton_Light_OK.Enabled = false;
  1335. radioButton_Light_NG.Enabled = false;
  1336. }
  1337. else if (radioButton_Light_NG.Checked == true)
  1338. {
  1339. label_sta_light.BackColor = Color.Red;
  1340. radioButton_Light_OK.BackColor = Color.Gray;
  1341. label_sta_light.Text = "NG";
  1342. label_Step.Text = "灯电压错误,测试结束!";
  1343. button_LightCtl.Text = "关灯";
  1344. LightProcess();
  1345. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压错误,测试结束" + "\r\n";
  1346. button_Start.Enabled = true;
  1347. radioButton_Light_OK.Enabled = false;
  1348. radioButton_Light_NG.Enabled = false;
  1349. ErrorSaveLog();
  1350. return;
  1351. }
  1352. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压确认完成" + "\r\n";
  1353. button_LightCtl.Text = "关灯";
  1354. LightProcess();
  1355. #endregion
  1356. #region Step4:推行测试
  1357. TestStep = TestStep_Enum.Step_Walk;
  1358. label_Step.Text = "推行测试,电机开始自动运转!";
  1359. button_Walk.Text = "启动";
  1360. Walk_Process(0x22);
  1361. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机进入推行模式,请注意安全!" + "\r\n";
  1362. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测开始" + "\r\n";
  1363. OK_cnt = 0;
  1364. WalkProcess_Cnt = 0;
  1365. while (WalkProcess_Cnt < 20)//开始判断传感器,超时20s
  1366. {
  1367. //车速传感器
  1368. if (SpeedSensorTest == false)
  1369. {
  1370. if (label_sensor_speed.BackColor == Color.Red)
  1371. {
  1372. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过车速传感器测试" + "\r\n";
  1373. label_sensor_speed.BackColor = Color.Green;
  1374. label_sensor_speed.Text = "OK";
  1375. OK_cnt++;
  1376. }
  1377. }
  1378. else
  1379. {
  1380. try
  1381. {
  1382. DataTemp = (ushort)(Convert.ToDecimal(textBox_BikeSpeed.Text) * 10);
  1383. }
  1384. catch (System.Exception ex)
  1385. {
  1386. MessageBox.Show(ex.Message);
  1387. label_Step.Text = "推行测试异常,测试结束!";
  1388. button_Start.Enabled = true;
  1389. ErrorSaveLog();
  1390. return;
  1391. }
  1392. if ((DataTemp > (SpeedSensorThData - 50)) && (DataTemp < (SpeedSensorThData + 50)))
  1393. {
  1394. if (label_sensor_speed.BackColor == Color.Red)
  1395. {
  1396. label_sensor_speed.BackColor = Color.Green;
  1397. label_sensor_speed.Text = "OK";
  1398. OK_cnt++;
  1399. }
  1400. }
  1401. }
  1402. //马达转速
  1403. try
  1404. {
  1405. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1406. }
  1407. catch (System.Exception ex)
  1408. {
  1409. MessageBox.Show(ex.Message);
  1410. label_Step.Text = "推行测试异常,测试结束!";
  1411. button_Start.Enabled = true;
  1412. ErrorSaveLog();
  1413. return;
  1414. }
  1415. if ((DataTemp > (MotorRadeSpeed - 200)) && (DataTemp < (MotorRadeSpeed + 200)))
  1416. {
  1417. if (label_sensor_motor.BackColor == Color.Red)
  1418. {
  1419. label_sensor_motor.BackColor = Color.Green;
  1420. label_sensor_motor.Text = "OK";
  1421. OK_cnt++;
  1422. }
  1423. }
  1424. //母线电压
  1425. try
  1426. {
  1427. DataTemp = Convert.ToUInt16(textBox_Vol.Text);
  1428. }
  1429. catch (System.Exception ex)
  1430. {
  1431. MessageBox.Show(ex.Message);
  1432. label_Step.Text = "推行测试异常,测试结束!";
  1433. button_Start.Enabled = true;
  1434. ErrorSaveLog();
  1435. return;
  1436. }
  1437. if ((DataTemp > (RateVoltage - 1000)) && (DataTemp < (RateVoltage + 1000)))
  1438. {
  1439. if (label_sensor_vol.BackColor == Color.Red)
  1440. {
  1441. label_sensor_vol.BackColor = Color.Green;
  1442. label_sensor_vol.Text = "OK";
  1443. OK_cnt++;
  1444. }
  1445. }
  1446. //母线电流
  1447. try
  1448. {
  1449. DataTemp = Convert.ToUInt16(textBox_Cur.Text);
  1450. }
  1451. catch (System.Exception ex)
  1452. {
  1453. MessageBox.Show(ex.Message);
  1454. label_Step.Text = "推行测试异常,测试结束!";
  1455. button_Start.Enabled = true;
  1456. ErrorSaveLog();
  1457. return;
  1458. }
  1459. if ((DataTemp > No_load_Current - 600) && (DataTemp < No_load_Current + 600))
  1460. {
  1461. if (label_sensor_cur.BackColor == Color.Red)
  1462. {
  1463. label_sensor_cur.BackColor = Color.Green;
  1464. label_sensor_cur.Text = "OK";
  1465. OK_cnt++;
  1466. }
  1467. }
  1468. //踏频
  1469. try
  1470. {
  1471. DataTemp = Convert.ToUInt16(textBox_Cadence.Text);
  1472. }
  1473. catch (System.Exception ex)
  1474. {
  1475. MessageBox.Show(ex.Message);
  1476. label_Step.Text = "推行测试异常,测试结束!";
  1477. button_Start.Enabled = true;
  1478. ErrorSaveLog();
  1479. return;
  1480. }
  1481. if ((DataTemp > (RateCadence - 20)) && (DataTemp < (RateCadence + 20)))
  1482. {
  1483. if (label_sensor_cadence.BackColor == Color.Red)
  1484. {
  1485. label_sensor_cadence.BackColor = Color.Green;
  1486. label_sensor_cadence.Text = "OK";
  1487. OK_cnt++;
  1488. }
  1489. }
  1490. //PCB温度
  1491. try
  1492. {
  1493. DataTemp_Int16 = Convert.ToInt16(textBox_PCB.Text);
  1494. }
  1495. catch (System.Exception ex)
  1496. {
  1497. MessageBox.Show(ex.Message);
  1498. label_Step.Text = "推行测试异常,测试结束!";
  1499. button_Start.Enabled = true;
  1500. ErrorSaveLog();
  1501. return;
  1502. }
  1503. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1504. {
  1505. if (label_sensor_PCB_temp.BackColor == Color.Red)
  1506. {
  1507. label_sensor_PCB_temp.BackColor = Color.Green;
  1508. label_sensor_PCB_temp.Text = "OK";
  1509. OK_cnt++;
  1510. }
  1511. }
  1512. //绕组温度
  1513. try
  1514. {
  1515. DataTemp_Int16 = Convert.ToInt16(textBox_Coil.Text);
  1516. }
  1517. catch (System.Exception ex)
  1518. {
  1519. MessageBox.Show(ex.Message);
  1520. label_Step.Text = "推行测试异常,测试结束!";
  1521. button_Start.Enabled = true;
  1522. ErrorSaveLog();
  1523. return;
  1524. }
  1525. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1526. {
  1527. if (label_sensor_coil_temp.BackColor == Color.Red)
  1528. {
  1529. label_sensor_coil_temp.BackColor = Color.Green;
  1530. label_sensor_coil_temp.Text = "OK";
  1531. OK_cnt++;
  1532. }
  1533. }
  1534. //MCU温度
  1535. try
  1536. {
  1537. DataTemp_Int16 = Convert.ToInt16(textBox_MCU.Text);
  1538. }
  1539. catch (System.Exception ex)
  1540. {
  1541. MessageBox.Show(ex.Message);
  1542. label_Step.Text = "推行测试异常,测试结束!";
  1543. button_Start.Enabled = true;
  1544. ErrorSaveLog();
  1545. return;
  1546. }
  1547. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1548. {
  1549. if (label_sensor_MCU_temp.BackColor == Color.Red)
  1550. {
  1551. label_sensor_MCU_temp.BackColor = Color.Green;
  1552. label_sensor_MCU_temp.Text = "OK";
  1553. OK_cnt++;
  1554. }
  1555. }
  1556. //测试完成,退出
  1557. if (OK_cnt == 8)
  1558. {
  1559. break;
  1560. }
  1561. }
  1562. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测结束" + "\r\n";
  1563. //断电开关1
  1564. label_Step.Text = "请在10s内捏下断电刹把 1!";
  1565. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 1 检测开始,请捏下断电刹把 1" + "\r\n";
  1566. WalkProcess_Cnt = 0;
  1567. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1568. {
  1569. try
  1570. {
  1571. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1572. }
  1573. catch (System.Exception ex)
  1574. {
  1575. MessageBox.Show(ex.Message);
  1576. label_Step.Text = "推行测试异常,测试结束!";
  1577. button_Start.Enabled = true;
  1578. ErrorSaveLog();
  1579. return;
  1580. }
  1581. if (DataTemp < 100)
  1582. {
  1583. if (label_Sensor_Break1.BackColor == Color.Red)
  1584. {
  1585. label_Sensor_Break1.BackColor = Color.Green;
  1586. label_Sensor_Break1.Text = "OK";
  1587. OK_cnt++;
  1588. }
  1589. break;
  1590. }
  1591. }
  1592. label_Step.Text = "请松开断电刹把 1";
  1593. WalkProcess_Cnt = 0;
  1594. while (WalkProcess_Cnt < 10)//等待电机再次启动,如果超时提示错误
  1595. {
  1596. try
  1597. {
  1598. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1599. }
  1600. catch (System.Exception ex)
  1601. {
  1602. MessageBox.Show(ex.Message);
  1603. label_Step.Text = "推行测试异常,测试结束!";
  1604. button_Start.Enabled = true;
  1605. ErrorSaveLog();
  1606. return;
  1607. }
  1608. if (DataTemp > 300)//电机转速超过300rpm,再次启动测试
  1609. break;
  1610. }
  1611. if (WalkProcess_Cnt >= 10)//表示超时退出,电机未启动
  1612. {
  1613. label_Step.Text = "推行测试异常,测试结束!";
  1614. button_Start.Enabled = true;
  1615. ErrorSaveLog();
  1616. return;
  1617. }
  1618. //断电开关2
  1619. label_Step.Text = "请在10s内捏下断电刹把 2!";
  1620. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 2 检测开始,请捏下断电刹把 2" + "\r\n";
  1621. WalkProcess_Cnt = 0;
  1622. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1623. {
  1624. try
  1625. {
  1626. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1627. }
  1628. catch (System.Exception ex)
  1629. {
  1630. MessageBox.Show(ex.Message);
  1631. label_Step.Text = "推行测试异常,测试结束!";
  1632. button_Start.Enabled = true;
  1633. ErrorSaveLog();
  1634. return;
  1635. }
  1636. if (DataTemp < 100)
  1637. {
  1638. if (label_Sensor_Break2.BackColor == Color.Red)
  1639. {
  1640. label_Sensor_Break2.BackColor = Color.Green;
  1641. label_Sensor_Break2.Text = "OK";
  1642. OK_cnt++;
  1643. }
  1644. break;
  1645. }
  1646. }
  1647. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把检测结束" + "\r\n";
  1648. label_Step.Text = "请松开断电刹把 2!";
  1649. button_Walk.Text = "停止";
  1650. Walk_Process(0x00);
  1651. Delay_ms(1000);
  1652. //判断测试结果
  1653. if (OK_cnt < 10)
  1654. {
  1655. label_Step.Text = "推行测试异常,测试结束!";
  1656. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1657. button_Start.Enabled = true;
  1658. ErrorSaveLog();
  1659. return;
  1660. }
  1661. //判断BMS通信是否正常
  1662. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "检测BMS通信状态" + "\r\n";
  1663. if (label_BMS_COM.Text != "OK")
  1664. {
  1665. label_Step.Text = "BMS通信异常,测试结束!";
  1666. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1667. button_Start.Enabled = true;
  1668. ErrorSaveLog();
  1669. return;
  1670. }
  1671. #endregion
  1672. #region Step5:转把模式测试(非必需)
  1673. if (GasModeTest == false)
  1674. {
  1675. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过指拨模式测试" + "\r\n";
  1676. label_Sensor_Gas.BackColor = Color.Green;
  1677. label_Sensor_Gas.Text = "OK";
  1678. }
  1679. else
  1680. {
  1681. TestStep = TestStep_Enum.Step_GasMode;
  1682. label_Step.Text = "指拨模式测试,请在10s内转动指拨!";
  1683. button_Walk.Text = "启动";
  1684. Walk_Process(0x04);
  1685. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试开始,请转动指拨" + "\r\n";
  1686. OK_cnt = 0;
  1687. WalkProcess_Cnt = 0;
  1688. while (WalkProcess_Cnt < 10)//10s内判断电机是否启动
  1689. {
  1690. try
  1691. {
  1692. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1693. }
  1694. catch (System.Exception ex)
  1695. {
  1696. MessageBox.Show(ex.Message);
  1697. label_Step.Text = "推行测试异常,测试结束!";
  1698. button_Start.Enabled = true;
  1699. ErrorSaveLog();
  1700. return;
  1701. }
  1702. if ((DataTemp > (GasModeSpeed - 100)) && (DataTemp < (GasModeSpeed + 100)))
  1703. {
  1704. label_Sensor_Gas.BackColor = Color.Green;
  1705. label_Sensor_Gas.Text = "OK";
  1706. OK_cnt++;
  1707. break;
  1708. }
  1709. }
  1710. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试结束" + "\r\n";
  1711. Delay_ms(1000);
  1712. button_Walk.Text = "停止";
  1713. Walk_Process(0x00);
  1714. //判断测试结果
  1715. if (OK_cnt < 1)
  1716. {
  1717. label_Step.Text = "指拨模式测试异常,测试结束!";
  1718. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试失败,测试结束" + "\r\n";
  1719. button_Start.Enabled = true;
  1720. ErrorSaveLog();
  1721. return;
  1722. }
  1723. }
  1724. #endregion
  1725. #region Step6:版本查询和确认
  1726. TestStep = TestStep_Enum.Step_CheckVer;
  1727. label_Step.Text = "版本查询和确认";
  1728. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  1729. richTextBox_Ver.Text = "";
  1730. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1731. Delay_ms(200);
  1732. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  1733. Delay_ms(200);
  1734. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  1735. string FW_Version_Result = "";
  1736. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  1737. string TE_FW_Ver_Result = "";
  1738. try
  1739. {
  1740. FW_Version_Result = richTextBox_Ver.Lines[3].Split(':')[1];
  1741. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_Ver.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  1742. TE_FW_Ver_Result = richTextBox_Ver.Lines[5].Split(':')[1];
  1743. }
  1744. catch (System.Exception)
  1745. {
  1746. label_Step.Text = "软件版本信息不完整!";
  1747. }
  1748. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  1749. {
  1750. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息错误" + "\r\n";
  1751. label_Step.Text = "软件版本错误,测试结束!";
  1752. button_Start.Enabled = true;
  1753. ErrorSaveLog();
  1754. return;
  1755. }
  1756. else
  1757. {
  1758. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  1759. {
  1760. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  1761. {
  1762. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE软件版本错误" + "\r\n";
  1763. label_Step.Text = "TE软件版本错误,测试结束!";
  1764. button_Start.Enabled = true;
  1765. ErrorSaveLog();
  1766. return;
  1767. }
  1768. }
  1769. label_FW_Version.BackColor = Color.Green;
  1770. label_FW_Version.Text = "OK";
  1771. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  1772. }
  1773. Delay_ms(1000);
  1774. #endregion
  1775. #region 按照产品信息设置测试文件路径
  1776. ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  1777. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  1778. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  1779. #endregion
  1780. #region Step7:参数查询和保存
  1781. TestStep = TestStep_Enum.Step_Save;
  1782. label_Step.Text = "参数导出";
  1783. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始导出电机内部参数" + "\r\n";
  1784. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\ParamsLog" + ".ttlog";
  1785. //开始发送指令
  1786. byte[] SendData = new byte[32];
  1787. //查询校验密钥
  1788. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  1789. Delay_ms(100);
  1790. //查询版本信息
  1791. //SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  1792. //Delay_ms(100);
  1793. //SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  1794. //Delay_ms(100);
  1795. //查询用户字符串1
  1796. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  1797. Delay_ms(100);
  1798. //查询用户字符串2
  1799. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  1800. Delay_ms(100);
  1801. //查询用户字符串3
  1802. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  1803. Delay_ms(100);
  1804. //查询用户参数1
  1805. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  1806. Delay_ms(100);
  1807. //查询马达参数
  1808. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  1809. Delay_ms(100);
  1810. //查询历史信息
  1811. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  1812. Delay_ms(100);
  1813. //查询生产信息
  1814. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  1815. Delay_ms(100);
  1816. //查询力矩传感器零点 Volans
  1817. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);
  1818. Delay_ms(100);
  1819. //查询运行信息
  1820. SendData[0] = 0x00;
  1821. SendData[1] = 0xF0;
  1822. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1823. Delay_ms(100);
  1824. SendData[0] = 0x01;
  1825. SendData[1] = 0xF0;
  1826. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1827. Delay_ms(100);
  1828. SendData[0] = 0x02;
  1829. SendData[1] = 0xF0;
  1830. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1831. Delay_ms(100);
  1832. SendData[0] = 0x03;
  1833. SendData[1] = 0xF0;
  1834. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1835. Delay_ms(100);
  1836. SendData[0] = 0x04;
  1837. SendData[1] = 0xF0;
  1838. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1839. Delay_ms(100);
  1840. SendData[0] = 0x33;
  1841. SendData[1] = 0xF0;
  1842. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  1843. Delay_ms(100);
  1844. //查询力矩传感器校正信息 Volans
  1845. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
  1846. Delay_ms(100);
  1847. //导出故障日志
  1848. UInt32 Address_Begin;
  1849. UInt32 Addres_End;
  1850. Address_Begin = 0x0801F800;
  1851. Addres_End = 0x0801FBFF;
  1852. UInt32 ReadCount = 0;
  1853. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  1854. for (ushort i = 0; i < ReadCount; i++)
  1855. {
  1856. var SendByte = new byte[8];
  1857. UInt32 Address_Temp_Begin;
  1858. UInt32 Address_Temp_End;
  1859. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  1860. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  1861. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  1862. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  1863. SendByte[3] = (byte)(Address_Temp_Begin);
  1864. Address_Temp_End = Address_Temp_Begin + 127;
  1865. SendByte[4] = (byte)(Address_Temp_End >> 24);
  1866. SendByte[5] = (byte)(Address_Temp_End >> 16);
  1867. SendByte[6] = (byte)(Address_Temp_End >> 8);
  1868. SendByte[7] = (byte)(Address_Temp_End);
  1869. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  1870. Delay_ms(100);
  1871. }
  1872. //查询骑行信息
  1873. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  1874. Delay_ms(100);
  1875. //查询用户参数2
  1876. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  1877. Delay_ms(100);
  1878. //查询力矩传感器零点值 Pegasi
  1879. SendData[0] = 1;
  1880. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  1881. Delay_ms(100);
  1882. SendData[0] = 2;
  1883. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  1884. Delay_ms(100);
  1885. SendData[0] = 3;
  1886. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  1887. Delay_ms(100);
  1888. //查询校正系数 Pegasi
  1889. SendData[0] = 1;
  1890. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  1891. Delay_ms(100);
  1892. SendData[0] = 2;
  1893. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  1894. Delay_ms(100);
  1895. SendData[0] = 3;
  1896. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  1897. Delay_ms(100);
  1898. //查询启动值 Pegasi
  1899. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  1900. Delay_ms(100);
  1901. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机参数导出完成" + "\r\n";
  1902. #endregion
  1903. //显示测试结果
  1904. label_Result.BackColor = Color.Green;
  1905. label_Result.Text = "合格";
  1906. label_Step.Text = "合格,测试结束!";
  1907. label_Step.ForeColor = Color.Green;
  1908. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "合格,测试结束" + "\r\n";
  1909. button_Start.Enabled = true;
  1910. //导出的参数上传服务器
  1911. //if (StartForm.myFtp.IsNetConnected == true)
  1912. //{
  1913. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1914. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  1915. // {
  1916. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  1917. // }
  1918. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  1919. // {
  1920. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1921. // }
  1922. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1923. // if (result == true)
  1924. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数上传服务器成功" + "\r\n";
  1925. //}
  1926. //保存测试信息
  1927. //页面存储
  1928. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  1929. Graphics g = Graphics.FromImage(bit);
  1930. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  1931. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试页面" + ".png";
  1932. bit.Save(LogSaveFileName);
  1933. //测试页面上传服务器
  1934. //if(StartForm.myFtp.IsNetConnected==true)
  1935. //{
  1936. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1937. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  1938. // {
  1939. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  1940. // }
  1941. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  1942. // {
  1943. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1944. // }
  1945. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1946. // if (result == true)
  1947. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  1948. //}
  1949. //保存测试记录
  1950. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".txt";
  1951. richTextBox_TestRecord.SaveFile(LogSaveFileName, RichTextBoxStreamType.PlainText);
  1952. //测试记录上传服务器
  1953. //if (StartForm.myFtp.IsNetConnected == true)
  1954. //{
  1955. // string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  1956. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  1957. // {
  1958. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  1959. // }
  1960. // if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  1961. // {
  1962. // StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1963. // }
  1964. // bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  1965. // if (result == true)
  1966. // richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试记录上传服务器成功" + "\r\n";
  1967. //}
  1968. }
  1969. //开始测试
  1970. private void button_Start_Click(object sender, EventArgs e)
  1971. {
  1972. TestStart();
  1973. }
  1974. //测试子函数
  1975. private void TestStart()
  1976. {
  1977. //检查是否有故障
  1978. if (label_error_code.Text != "OK")
  1979. {
  1980. label_Step.Text = "电机存在故障!";
  1981. ErrorSaveLog();
  1982. return;
  1983. }
  1984. //标签初始化
  1985. label_Init();
  1986. if (!portsettings_form.serialPort1.IsOpen)
  1987. {
  1988. label_Step.Text = "串口未连接!";
  1989. return;
  1990. }
  1991. #region 检查配置文件
  1992. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  1993. {
  1994. label_Step.Text = "无可用配置文件!";
  1995. return;
  1996. }
  1997. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  1998. {
  1999. label_Step.Text = "请选择配置文件!";
  2000. return;
  2001. }
  2002. #endregion
  2003. #region 检查产品信息是否符合格式要求
  2004. try
  2005. {
  2006. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  2007. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  2008. Regex regex = new Regex(@"^M[12][0-9](0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)[A-Z]\d{4}[A-Z]$");
  2009. if (regex.IsMatch(Sn) == false)
  2010. {
  2011. label_Step.Text = "SN格式错误!";
  2012. label_Step.ForeColor = Color.Red;
  2013. return;
  2014. }
  2015. }
  2016. catch (System.Exception ex)
  2017. {
  2018. label_Step.Text = "标签格式错误!";
  2019. label_Step.ForeColor = Color.Red;
  2020. return;
  2021. }
  2022. #endregion
  2023. //测试状态更新
  2024. TestStep = TestStep_Enum.Step_Init;
  2025. label_Step.Text = "准备开始测试";
  2026. label_Step.ForeColor = Color.Red;
  2027. Delay_ms(500);
  2028. //流水号累加和存储
  2029. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  2030. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2031. StreamReader objReader = new StreamReader(cfg_file);
  2032. string sLine = "";
  2033. ArrayList arrText = new ArrayList();//创建一个动态数组
  2034. while (sLine != null)
  2035. {
  2036. sLine = objReader.ReadLine();
  2037. arrText.Add(sLine);
  2038. }
  2039. arrText.RemoveAt(arrText.Count - 1);
  2040. objReader.Close();
  2041. arrText[1] = label_SerialNum.Text;
  2042. using (StreamWriter file = new StreamWriter(cfg_file))
  2043. {
  2044. foreach (var entry in arrText)
  2045. {
  2046. file.WriteLine(entry);
  2047. }
  2048. }
  2049. button_Start.Enabled = false;
  2050. //创建线程,执行测试任务
  2051. Thread th = new Thread(Thread_Task);
  2052. th.Start();
  2053. }
  2054. //1s计时
  2055. private void timer2_Tick(object sender, EventArgs e)
  2056. {
  2057. label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  2058. //MC故障信息超时清除
  2059. if (MC_ErrorCode_Refresh == false)
  2060. {
  2061. MC_ErrorCode_Refresh_Cnt++;
  2062. if (MC_ErrorCode_Refresh_Cnt > 5)
  2063. {
  2064. label_error_code.Text = "OK";
  2065. label_error_code.BackColor = Color.Green;
  2066. label_ErrorInfo.Text = "无故障";
  2067. label_ErrorInfo.ForeColor = Color.Green;
  2068. MC_ErrorCode_Refresh_Cnt = 0;
  2069. }
  2070. }
  2071. else
  2072. {
  2073. MC_ErrorCode_Refresh_Cnt = 0;
  2074. }
  2075. MC_ErrorCode_Refresh = false;
  2076. //MC_RunInfo超时清除
  2077. if (MC_RunInfo_Refresh == false)
  2078. {
  2079. MC_RunInfo_Refresh_Cnt++;
  2080. if (MC_RunInfo_Refresh_Cnt > 5)
  2081. {
  2082. MC_RunInfo_Clear();
  2083. MC_RunInfo_Refresh_Cnt = 0;
  2084. }
  2085. }
  2086. else
  2087. {
  2088. MC_RunInfo_Refresh_Cnt = 0;
  2089. }
  2090. MC_RunInfo_Refresh = false;
  2091. //等待反馈指令
  2092. if (Ack_Flag == false)
  2093. {
  2094. Ack_Cnt++;
  2095. if (Ack_Cnt > 2)
  2096. {
  2097. Ack_Flag = true;
  2098. StepTestFlag = StepTestResult_Enum.StepTestResult_Fail;
  2099. }
  2100. }
  2101. //推行测试计时
  2102. if ((TestStep == TestStep_Enum.Step_Walk) || (TestStep == TestStep_Enum.Step_GasMode))
  2103. {
  2104. WalkProcess_Cnt++;
  2105. }
  2106. }
  2107. //打开系统开关
  2108. private void 开ToolStripMenuItem_Click(object sender, EventArgs e)
  2109. {
  2110. byte[] Data = new byte[1];
  2111. if (portsettings_form.serialPort1.IsOpen)
  2112. {
  2113. //开机
  2114. Data[0] = 0xF1;
  2115. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2116. 开ToolStripMenuItem.Checked = true;
  2117. 关ToolStripMenuItem.Checked = false;
  2118. }
  2119. else
  2120. {
  2121. MessageBox.Show("串口未连接");
  2122. return;
  2123. }
  2124. }
  2125. //关闭系统开关
  2126. private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
  2127. {
  2128. byte[] Data = new byte[1];
  2129. if (portsettings_form.serialPort1.IsOpen)
  2130. {
  2131. //关机
  2132. Data[0] = 0xF0;
  2133. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2134. 开ToolStripMenuItem.Checked = false;
  2135. 关ToolStripMenuItem.Checked = true;
  2136. }
  2137. else
  2138. {
  2139. MessageBox.Show("串口未连接");
  2140. return;
  2141. }
  2142. }
  2143. //清除流水号
  2144. private void label8_DoubleClick(object sender, EventArgs e)
  2145. {
  2146. //流水号清零
  2147. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2148. {
  2149. label_SerialNum.Text = "0";
  2150. //存储
  2151. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2152. StreamReader objReader = new StreamReader(cfg_file);
  2153. string sLine = "";
  2154. ArrayList arrText = new ArrayList();//创建一个动态数组
  2155. while (sLine != null)
  2156. {
  2157. sLine = objReader.ReadLine();
  2158. arrText.Add(sLine);
  2159. }
  2160. arrText.RemoveAt(arrText.Count - 1);
  2161. objReader.Close();
  2162. arrText[1] = label_SerialNum.Text;
  2163. using (StreamWriter file = new StreamWriter(cfg_file))
  2164. {
  2165. foreach (var entry in arrText)
  2166. {
  2167. file.WriteLine(entry);
  2168. }
  2169. }
  2170. }
  2171. }
  2172. private void richTextBox_TestRecord_TextChanged(object sender, EventArgs e)
  2173. {
  2174. richTextBox_TestRecord.SelectionStart = richTextBox_TestRecord.Text.Length;
  2175. richTextBox_TestRecord.ScrollToCaret();
  2176. }
  2177. private void textBox_PDinfo_Click(object sender, EventArgs e)
  2178. {
  2179. textBox_PDinfo.SelectAll();
  2180. }
  2181. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  2182. {
  2183. if (e.KeyCode == Keys.Enter)
  2184. {
  2185. TestStart();
  2186. }
  2187. }
  2188. }
  2189. }