ImportForm.cs 127 KB

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