ImportForm.cs 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  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. //电机型号
  537. richTextBox_Ver.Text += "型号:";
  538. for (ushort i = 0; i < 16; i++)
  539. {
  540. if (binary_data_1[2 + i] == 0x2E)
  541. {
  542. break;
  543. }
  544. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  545. }
  546. richTextBox_Ver.Text += "\r\n";
  547. //电机SN
  548. richTextBox_Ver.Text += "序列号:";
  549. for (ushort i = 0; i < 16; i++)
  550. {
  551. if (binary_data_1[18 + i] == 0x2E)
  552. {
  553. break;
  554. }
  555. richTextBox_Ver.Text += ((char)binary_data_1[18 + i]).ToString();
  556. }
  557. richTextBox_Ver.Text += "\r\n";
  558. //电机HW
  559. richTextBox_Ver.Text += "硬件版本:";
  560. for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束
  561. {
  562. if (binary_data_1[34 + i] == 0x2E)
  563. {
  564. break;
  565. }
  566. if (binary_data_1[34 + i] == 'r')
  567. {
  568. binary_data_1[34 + i] = (byte)'.';
  569. }
  570. richTextBox_Ver.Text += ((char)binary_data_1[34 + i]).ToString();
  571. }
  572. switch (binary_data_1[34 + 10])//第10位代表MCU品牌
  573. {
  574. case (byte)'1':
  575. richTextBox_Ver.Text += " " + "ST" + " "; break;
  576. case (byte)'2':
  577. richTextBox_Ver.Text += " " + "APM" + " "; break;
  578. case (byte)'3':
  579. richTextBox_Ver.Text += " " + "HK" + " "; break;
  580. case (byte)'4':
  581. richTextBox_Ver.Text += " " + "GD" + " "; break;
  582. default:
  583. richTextBox_Ver.Text += " " + "NULL" + " "; break;
  584. }
  585. for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID
  586. {
  587. richTextBox_Ver.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值
  588. }
  589. richTextBox_Ver.Text += "\r\n";
  590. //电机FW
  591. richTextBox_Ver.Text += "软件版本:";
  592. for (ushort i = 0; i < 16; i++)
  593. {
  594. if (binary_data_1[50 + i] == 0x2E)
  595. {
  596. break;
  597. }
  598. if (binary_data_1[50 + i] == 'r')
  599. {
  600. binary_data_1[50 + i] = (byte)'.';
  601. }
  602. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  603. }
  604. richTextBox_Ver.Text += "\r\n";
  605. }));
  606. }
  607. break;
  608. }
  609. case 0x1720://电机程序特性信息
  610. {
  611. unchecked
  612. {
  613. this.Invoke((EventHandler)(delegate
  614. {
  615. //程序特性
  616. for (ushort i = 0; i < 32; i++)
  617. {
  618. if (binary_data_1[2 + i] == 0x2E)
  619. {
  620. break;
  621. }
  622. if (binary_data_1[2 + i] == 'r')
  623. {
  624. binary_data_1[2 + i] = (byte)'.';
  625. }
  626. richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
  627. }
  628. richTextBox_Ver.Text += "\r\n";
  629. }));
  630. }
  631. break;
  632. }
  633. case 0xA903://反馈指令
  634. {
  635. unchecked
  636. {
  637. this.Invoke((EventHandler)(delegate
  638. {
  639. Ack_Flag = true;
  640. StepTestFlag = StepTestResult_Enum.StepTestResult_OK;
  641. }));
  642. }
  643. break;
  644. }
  645. default: break;
  646. }
  647. }
  648. #endregion
  649. #region TE
  650. else if ((ID == 0x615) || (ID == 0x610))
  651. {
  652. switch (CmdTemp)
  653. {
  654. case 0x1240://TE APP版本信息
  655. {
  656. unchecked
  657. {
  658. this.Invoke((EventHandler)(delegate
  659. {
  660. //TE 软件版本
  661. richTextBox_Ver.Text += "TE软件版本:";
  662. for (ushort i = 0; i < 16; i++)
  663. {
  664. if (binary_data_1[50 + i] == 0x2E)
  665. {
  666. break;
  667. }
  668. if (binary_data_1[50 + i] == 'r')
  669. {
  670. binary_data_1[50 + i] = (byte)'.';
  671. }
  672. richTextBox_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
  673. }
  674. }));
  675. }
  676. break;
  677. }
  678. default: break;
  679. }
  680. }
  681. #endregion
  682. #region //电池信息
  683. else if ((ID == 0x725) || (ID == 0x720))
  684. {
  685. label_BMS_COM.BackColor = Color.Green;
  686. label_BMS_COM.Text = "OK";
  687. }
  688. #endregion
  689. }
  690. #endregion
  691. //20ms定时器
  692. private void timer1_Tick(object sender, EventArgs e)
  693. {
  694. //串口数据解析
  695. Serial_DataReceived_Process();
  696. }
  697. #region 非独占性延时函数
  698. public static void Delay_ms(int milliSecond)
  699. {
  700. int start = Environment.TickCount;
  701. while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
  702. {
  703. Application.DoEvents();
  704. }
  705. }
  706. #endregion
  707. //电机运行信息清除
  708. private void MC_RunInfo_Clear()
  709. {
  710. foreach (Control c in groupBox9.Controls)
  711. {
  712. if (c is TextBox)
  713. {
  714. c.Text = "---";
  715. }
  716. }
  717. }
  718. private void ImportForm_FormClosing(object sender, FormClosingEventArgs e)
  719. {
  720. portsettings_form.serialPort1.Close();
  721. portsettings_form.comboBox_ComIndex.Enabled = true;
  722. portsettings_form.button_Enter.Text = "连接";
  723. portsettings_form.g_blnIsOpen = false;
  724. }
  725. //开关灯
  726. private void LightProcess()
  727. {
  728. var CtrlCode = new byte[2];
  729. CtrlCode[0] = 0x00;
  730. if (button_LightCtl.Text == "开灯")
  731. {
  732. button_LightCtl.Text = "关灯";
  733. CtrlCode[1] = 0xF1;
  734. button_LightCtl.BackColor = Color.Yellow;
  735. button_LightCtl.ForeColor = Color.Black;
  736. }
  737. else
  738. {
  739. button_LightCtl.Text = "开灯";
  740. CtrlCode[1] = 0xF0;
  741. button_LightCtl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  742. button_LightCtl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  743. }
  744. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
  745. }
  746. //开关灯按钮
  747. private void button_LightCtl_Click(object sender, EventArgs e)
  748. {
  749. LightProcess();
  750. }
  751. //电机启动/停止控制
  752. private void Walk_Process(byte GearSt)
  753. {
  754. var Code = new byte[2];
  755. if (button_Walk.Text == "启动")
  756. {
  757. button_Walk.Text = "停止";
  758. //发送指令使电机进入配置模式
  759. Code[0] = 0x01;
  760. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  761. Thread.Sleep(200);
  762. //发送指令进入设定档位
  763. Code[0] = GearSt;
  764. if (button_LightCtl.Text == "关灯")
  765. Code[1] = 0xF1;
  766. else
  767. Code[1] = 0xF0;
  768. button_Walk.BackColor = Color.Yellow;
  769. button_Walk.ForeColor = Color.Black;
  770. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  771. }
  772. else
  773. {
  774. button_Walk.Text = "启动";
  775. //发送指令关闭档位模式
  776. Code[0] = 0x00;
  777. if (button_LightCtl.Text == "关灯")
  778. Code[1] = 0xF1;
  779. else
  780. Code[1] = 0xF0;
  781. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
  782. Thread.Sleep(200);
  783. //发送指令使电机退出配置模式
  784. Code[0] = 0x00;
  785. button_Walk.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  786. button_Walk.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  787. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
  788. }
  789. }
  790. //电机启动/停止按钮
  791. private void button_Walk_Click(object sender, EventArgs e)
  792. {
  793. Walk_Process(0x22);
  794. }
  795. //查询版本信息
  796. private void button_Check_Ver_Click(object sender, EventArgs e)
  797. {
  798. richTextBox_Ver.Text = "";
  799. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  800. Delay_ms(200);
  801. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  802. }
  803. //按钮初始化
  804. private void label_Init()
  805. {
  806. radioButton_Light_OK.Checked = false;//灯电压确认按钮
  807. radioButton_Light_OK.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  808. radioButton_Light_OK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  809. radioButton_Light_NG.Checked = false;
  810. radioButton_Light_NG.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
  811. radioButton_Light_NG.ForeColor= System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(113)))), ((int)(((byte)(185)))));
  812. label_error_code.BackColor = Color.Green;//故障码
  813. label_error_code.Text = "OK";
  814. label_Sta_Clear.BackColor = Color.Red;//系统清除
  815. label_Sta_Clear.Text = "NG";
  816. label_Sta_Write.BackColor = Color.Red;//参数写入
  817. label_Sta_Write.Text = "NG";
  818. label_sta_light.BackColor = Color.Red;//灯电压
  819. label_sta_light.Text = "NG";
  820. label_sensor_vol.BackColor = Color.Red;//电压检测
  821. label_sensor_vol.Text = "NG";
  822. label_sensor_cur.BackColor = Color.Red;//电流检测
  823. label_sensor_cur.Text = "NG";
  824. label_sensor_speed.BackColor = Color.Red;//车速检测
  825. label_sensor_speed.Text = "NG";
  826. label_sensor_cadence.BackColor = Color.Red;//踏频检测
  827. label_sensor_cadence.Text = "NG";
  828. label_sensor_motor.BackColor = Color.Red;//电机转速检测
  829. label_sensor_motor.Text = "NG";
  830. label_sensor_PCB_temp.BackColor = Color.Red;//PCB温度检测
  831. label_sensor_PCB_temp.Text = "NG";
  832. label_sensor_coil_temp.BackColor = Color.Red;//绕组温度检测
  833. label_sensor_coil_temp.Text = "NG";
  834. label_sensor_MCU_temp.BackColor = Color.Red;//MCU温度检测
  835. label_sensor_MCU_temp.Text = "NG";
  836. label_Sensor_Break1.BackColor = Color.Red;//断电刹把
  837. label_Sensor_Break1.Text = "NG";
  838. label_Sensor_Break2.BackColor = Color.Red;//断电刹把
  839. label_Sensor_Break2.Text = "NG";
  840. label_Sensor_Gas.BackColor = Color.Red;//转把
  841. label_Sensor_Gas.Text = "NG";
  842. label_FW_Version.BackColor = Color.Red;//版本信息
  843. label_FW_Version.Text = "NG";
  844. label_BMS_COM.BackColor = Color.Red;//BMS通信
  845. label_BMS_COM.Text = "NG";
  846. label_Result.BackColor = Color.Red;//测试结果
  847. label_Result.Text = "不合格";
  848. richTextBox_Ver.Text = "";//版本信息
  849. richTextBox_TestRecord.Text = "";//测试过程记录
  850. }
  851. //测试失败存储测试界面
  852. private void ErrorSaveLog()
  853. {
  854. //页面存储
  855. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  856. Graphics g = Graphics.FromImage(bit);
  857. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  858. LogSaveFileName = LogSavePath + "\\errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\";
  859. if (System.IO.Directory.Exists(LogSaveFileName) == false)
  860. System.IO.Directory.CreateDirectory(LogSaveFileName);
  861. LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
  862. bit.Save(LogSaveFileName);
  863. //测试页面上传服务器
  864. if (StartForm.myFtp.IsNetConnected == true)
  865. {
  866. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  867. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/", DateNow) == false)
  868. {
  869. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  870. }
  871. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/errorlog/" + DateNow);
  872. if (result == true)
  873. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n";
  874. }
  875. }
  876. //执行一次测试任务
  877. private void Thread_Task()
  878. {
  879. //开始测试
  880. richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n";
  881. richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n";
  882. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n";
  883. //公有变量
  884. var Code = new byte[32];//发送的指令数据
  885. ushort DataTemp;
  886. short DataTemp_Int16;
  887. string ResultPathName = "";
  888. //开机
  889. Code[0] = 0xF1;
  890. SendCmd(0x7FF, 0x16, 0x2201, Code);
  891. 开ToolStripMenuItem.Checked = true;
  892. 关ToolStripMenuItem.Checked = false;
  893. Delay_ms(500);
  894. //检查是否有故障
  895. if (label_error_code.Text != "OK")
  896. {
  897. label_Step.Text = "电机存在故障!";
  898. button_Start.Enabled = true;
  899. //关机
  900. Code[0] = 0xF0;
  901. SendCmd(0x7FF, 0x16, 0x2201, Code);
  902. 开ToolStripMenuItem.Checked = false;
  903. 关ToolStripMenuItem.Checked = true;
  904. //关闭串口
  905. portsettings_form.closePort();
  906. //存储测试数据
  907. ErrorSaveLog();
  908. return;
  909. }
  910. #region Step1:发送指令,系统清除
  911. if (执行ToolStripMenuItem.Checked == true)
  912. {
  913. TestStep = TestStep_Enum.Step_SysClear;
  914. label_Step.Text = "系统清除,请等待!";
  915. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送系统清除指令" + "\r\n";
  916. Array.Clear(Code, 0, 32);
  917. Code[0] = (byte)'C';
  918. Code[1] = (byte)'L';
  919. Code[2] = (byte)'E';
  920. Code[3] = (byte)'A';
  921. Code[4] = (byte)'R';
  922. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
  923. Ack_Flag = false;
  924. Ack_Cnt = 0;
  925. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  926. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  927. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  928. {
  929. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除成功" + "\r\n";
  930. label_Sta_Clear.BackColor = Color.Green;
  931. label_Sta_Clear.Text = "OK";
  932. }
  933. else//失败
  934. {
  935. label_Step.Text = "系统清除失败,测试结束!";
  936. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "系统清除失败,测试结束" + "\r\n";
  937. button_Start.Enabled = true;
  938. //关机
  939. Code[0] = 0xF0;
  940. SendCmd(0x7FF, 0x16, 0x2201, Code);
  941. 开ToolStripMenuItem.Checked = false;
  942. 关ToolStripMenuItem.Checked = true;
  943. //关闭串口
  944. portsettings_form.closePort();
  945. //存储测试数据
  946. ErrorSaveLog();
  947. return;
  948. }
  949. #endregion
  950. //系统清除完成后会重启,等待3s
  951. Delay_ms(2000);
  952. }
  953. else if (不执行ToolStripMenuItem.Checked == true)
  954. {
  955. label_Sta_Clear.BackColor = Color.Yellow;
  956. label_Sta_Clear.Text = "N/A";
  957. }
  958. #region Step2:发送指令,依次写入参数
  959. TestStep = TestStep_Enum.Step_Write;
  960. label_Step.Text = "参数写入中,请等待!";
  961. #region Step2.1:写入电机型号和序列号
  962. string Mode = textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' '));
  963. string SN = textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1);
  964. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机型号" + "\r\n";
  965. Array.Clear(Code, 0, 32);
  966. for (ushort i = 0; i < ((Mode.Length > 16) ? 16 : Mode.Length); i++)
  967. {
  968. Code[i] = (byte)Mode[i];
  969. }
  970. if (Mode.Length < 16)
  971. {
  972. Code[Mode.Length] = (byte)'.';
  973. for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
  974. {
  975. Code[Mode.Length + 1 + i] = 0x20;
  976. }
  977. }
  978. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, Code);
  979. Ack_Flag = false;
  980. Ack_Cnt = 0;
  981. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  982. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  983. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  984. {
  985. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入成功" + "\r\n";
  986. }
  987. else//失败
  988. {
  989. label_Step.Text = "参数写入失败,测试结束!";
  990. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机型号写入失败,测试结束" + "\r\n";
  991. //关机
  992. Code[0] = 0xF0;
  993. SendCmd(0x7FF, 0x16, 0x2201, Code);
  994. 开ToolStripMenuItem.Checked = false;
  995. 关ToolStripMenuItem.Checked = true;
  996. button_Start.Enabled = true;
  997. //关闭串口
  998. portsettings_form.closePort();
  999. //存储测试数据
  1000. ErrorSaveLog();
  1001. return;
  1002. }
  1003. Delay_ms(200);
  1004. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入电机序列号" + "\r\n";
  1005. Array.Clear(Code, 0, 32);
  1006. for (ushort i = 0; i < ((SN.Length > 16) ? 16 : SN.Length); i++)
  1007. {
  1008. Code[i] = (byte)SN[i];
  1009. }
  1010. if (SN.Length < 16)
  1011. {
  1012. Code[SN.Length] = (byte)'.';
  1013. for (ushort i = 0; i < 16 - SN.Length - 1; i++)
  1014. {
  1015. Code[SN.Length + 1 + i] = 0x20;
  1016. }
  1017. }
  1018. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, Code);
  1019. Ack_Flag = false;
  1020. Ack_Cnt = 0;
  1021. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1022. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1023. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1024. {
  1025. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入成功" + "\r\n";
  1026. }
  1027. else//失败
  1028. {
  1029. label_Step.Text = "参数写入失败,测试结束!";
  1030. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机序列号写入失败,测试结束" + "\r\n";
  1031. //关机
  1032. Code[0] = 0xF0;
  1033. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1034. 开ToolStripMenuItem.Checked = false;
  1035. 关ToolStripMenuItem.Checked = true;
  1036. button_Start.Enabled = true;
  1037. //关闭串口
  1038. portsettings_form.closePort();
  1039. //存储测试数据
  1040. ErrorSaveLog();
  1041. return;
  1042. }
  1043. #endregion
  1044. #region Step2.2:写入用户参数1
  1045. Delay_ms(200);
  1046. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数1" + "\r\n";
  1047. Array.Clear(Code, 0, 32);
  1048. //指拨模式
  1049. Code[0] = Convert.ToByte(array_CfgInfo[25].ToString().Split(',')[1]);
  1050. //启动模式
  1051. Code[1] = Convert.ToByte(array_CfgInfo[1].ToString().Split(',')[1]);
  1052. //停机时间
  1053. DataTemp = Convert.ToUInt16(array_CfgInfo[2].ToString().Split(',')[1]);
  1054. Code[2] = (byte)(DataTemp & 0xFF);
  1055. Code[3] = (byte)(DataTemp >> 8);
  1056. //限速
  1057. Code[4] = Convert.ToByte(array_CfgInfo[3].ToString().Split(',')[1]);
  1058. //下降速度
  1059. Code[5] = Convert.ToByte(array_CfgInfo[4].ToString().Split(',')[1]);
  1060. //前飞
  1061. Code[6] = Convert.ToByte(array_CfgInfo[5].ToString().Split(',')[1]);
  1062. //后飞
  1063. Code[7] = Convert.ToByte(array_CfgInfo[6].ToString().Split(',')[1]);
  1064. //限流
  1065. Code[8] = Convert.ToByte(array_CfgInfo[7].ToString().Split(',')[1]);
  1066. //温度预警
  1067. Code[9] = (byte)(Convert.ToByte(array_CfgInfo[8].ToString().Split(',')[1]) + 40);
  1068. //温度保护
  1069. Code[10] = (byte)(Convert.ToByte(array_CfgInfo[9].ToString().Split(',')[1]) + 40);
  1070. //无PBU
  1071. Code[11] = Convert.ToByte(array_CfgInfo[10].ToString().Split(',')[1]);
  1072. //轮胎周长
  1073. Code[12] = Convert.ToByte(array_CfgInfo[11].ToString().Split(',')[1]);
  1074. //电机系列号
  1075. Code[13] = Convert.ToByte(array_CfgInfo[12].ToString().Split(',')[1]);
  1076. //ECO助力增益
  1077. Code[14] = Convert.ToByte(array_CfgInfo[13].ToString().Split(',')[1]);
  1078. //ECO加速
  1079. Code[15] = Convert.ToByte(array_CfgInfo[14].ToString().Split(',')[1]);
  1080. //NORM助力增益
  1081. Code[16] = Convert.ToByte(array_CfgInfo[15].ToString().Split(',')[1]);
  1082. //NORM加速
  1083. Code[17] = Convert.ToByte(array_CfgInfo[16].ToString().Split(',')[1]);
  1084. //SPORT助力增益
  1085. Code[18] = Convert.ToByte(array_CfgInfo[17].ToString().Split(',')[1]);
  1086. //SPORT加速
  1087. Code[19] = Convert.ToByte(array_CfgInfo[18].ToString().Split(',')[1]);
  1088. //TURBO助力增益
  1089. Code[20] = Convert.ToByte(array_CfgInfo[19].ToString().Split(',')[1]);
  1090. //TURBO加速
  1091. Code[21] = Convert.ToByte(array_CfgInfo[20].ToString().Split(',')[1]);
  1092. //SMART助力增益
  1093. Code[22] = Convert.ToByte(array_CfgInfo[21].ToString().Split(',')[1]);
  1094. //SMART加速
  1095. Code[23] = Convert.ToByte(array_CfgInfo[22].ToString().Split(',')[1]);
  1096. //速度传感器信号个数
  1097. Code[24] = Convert.ToByte(array_CfgInfo[23].ToString().Split(',')[1]);
  1098. //踏频启动信号个数
  1099. Code[25] = Convert.ToByte(array_CfgInfo[24].ToString().Split(',')[1]);
  1100. //速度传感器
  1101. Code[26] = Convert.ToByte(array_CfgInfo[26].ToString().Split(',')[1]);
  1102. //周长微调
  1103. Code[27] = (byte)Convert.ToInt16(array_CfgInfo[27].ToString().Split(',')[1]);
  1104. //低压保护阈值
  1105. DataTemp = Convert.ToUInt16(array_CfgInfo[28].ToString().Split(',')[1]);
  1106. Code[28] = (byte)(DataTemp & 0xFF);
  1107. Code[29] = (byte)(DataTemp >> 8);
  1108. //推行限速
  1109. DataTemp = Convert.ToByte(array_CfgInfo[29].ToString().Split(',')[1]);
  1110. Code[30] = (byte)Convert.ToByte(DataTemp);
  1111. //推行马达转速
  1112. Code[31] = (byte)Convert.ToByte(array_CfgInfo[30].ToString().Split(',')[1]);
  1113. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, Code);
  1114. Ack_Flag = false;
  1115. Ack_Cnt = 0;
  1116. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1117. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1118. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1119. {
  1120. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入成功" + "\r\n";
  1121. }
  1122. else//失败
  1123. {
  1124. label_Step.Text = "参数写入失败,测试结束!";
  1125. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数1写入失败,测试结束" + "\r\n";
  1126. //关机
  1127. Code[0] = 0xF0;
  1128. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1129. 开ToolStripMenuItem.Checked = false;
  1130. 关ToolStripMenuItem.Checked = true;
  1131. button_Start.Enabled = true;
  1132. //关闭串口
  1133. portsettings_form.closePort();
  1134. //存储测试数据
  1135. ErrorSaveLog();
  1136. return;
  1137. }
  1138. #endregion
  1139. #region Step2.3:写入用户参数2
  1140. Delay_ms(200);
  1141. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入用户参数2" + "\r\n";
  1142. Array.Clear(Code, 0, 32);
  1143. //俯仰角零偏
  1144. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[34].ToString().Split(',')[1]) * 10);
  1145. Code[0] = (byte)(DataTemp_Int16 & 0xFF);
  1146. Code[1] = (byte)(DataTemp_Int16 >> 8);
  1147. //横滚角零偏
  1148. DataTemp_Int16 = (short)(decimal.Parse(array_CfgInfo[35].ToString().Split(',')[1]) * 10);
  1149. Code[2] = (byte)(DataTemp_Int16 & 0xFF);
  1150. Code[3] = (byte)(DataTemp_Int16 >> 8);
  1151. //支持姿态传感器
  1152. Code[4] = Convert.ToByte(array_CfgInfo[33].ToString().Split(',')[1]);
  1153. //尾灯模式
  1154. Code[5] = Convert.ToByte(array_CfgInfo[36].ToString().Split(',')[1]);
  1155. //前灯电压
  1156. Code[6] = Convert.ToByte(array_CfgInfo[37].ToString().Split(',')[1]);
  1157. //后灯电压
  1158. Code[7] = Convert.ToByte(array_CfgInfo[38].ToString().Split(',')[1]);
  1159. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, Code);
  1160. Ack_Flag = false;
  1161. Ack_Cnt = 0;
  1162. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1163. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1164. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1165. {
  1166. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入成功" + "\r\n";
  1167. }
  1168. else//失败
  1169. {
  1170. label_Step.Text = "参数写入失败,测试结束!";
  1171. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "用户参数2写入失败,测试结束" + "\r\n";
  1172. //关机
  1173. Code[0] = 0xF0;
  1174. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1175. 开ToolStripMenuItem.Checked = false;
  1176. 关ToolStripMenuItem.Checked = true;
  1177. button_Start.Enabled = true;
  1178. //关闭串口
  1179. portsettings_form.closePort();
  1180. //存储测试数据
  1181. ErrorSaveLog();
  1182. return;
  1183. }
  1184. #endregion
  1185. #region Step2.4:写入马达参数
  1186. Delay_ms(200);
  1187. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入马达参数" + "\r\n";
  1188. Array.Clear(Code, 0, 32);
  1189. //额定功率
  1190. DataTemp = Convert.ToUInt16(array_CfgInfo[41].ToString().Split(',')[1]);
  1191. Code[0] = (byte)(DataTemp & 0xFF);
  1192. Code[1] = (byte)(DataTemp >> 8);
  1193. //额定转速
  1194. DataTemp = Convert.ToUInt16(array_CfgInfo[42].ToString().Split(',')[1]);
  1195. Code[2] = (byte)(DataTemp & 0xFF);
  1196. Code[3] = (byte)(DataTemp >> 8);
  1197. //定子电阻
  1198. DataTemp = Convert.ToUInt16(array_CfgInfo[43].ToString().Split(',')[1]);
  1199. Code[4] = (byte)(DataTemp & 0xFF);
  1200. Code[5] = (byte)(DataTemp >> 8);
  1201. //Lq
  1202. DataTemp = Convert.ToUInt16(array_CfgInfo[44].ToString().Split(',')[1]);
  1203. Code[6] = (byte)(DataTemp & 0xFF);
  1204. Code[7] = (byte)(DataTemp >> 8);
  1205. //Ld
  1206. DataTemp = Convert.ToUInt16(array_CfgInfo[45].ToString().Split(',')[1]);
  1207. Code[8] = (byte)(DataTemp & 0xFF);
  1208. Code[9] = (byte)(DataTemp >> 8);
  1209. //反电动势
  1210. DataTemp = Convert.ToUInt16(array_CfgInfo[46].ToString().Split(',')[1]);
  1211. Code[10] = (byte)(DataTemp & 0xFF);
  1212. Code[11] = (byte)(DataTemp >> 8);
  1213. //额定电压
  1214. Code[12] = Convert.ToByte(array_CfgInfo[47].ToString().Split(',')[1]);
  1215. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, Code);
  1216. Ack_Flag = false;
  1217. Ack_Cnt = 0;
  1218. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1219. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1220. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1221. {
  1222. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入成功" + "\r\n";
  1223. }
  1224. else//失败
  1225. {
  1226. label_Step.Text = "参数写入失败,测试结束!";
  1227. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "马达参数写入失败,测试结束" + "\r\n";
  1228. //关机
  1229. Code[0] = 0xF0;
  1230. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1231. 开ToolStripMenuItem.Checked = false;
  1232. 关ToolStripMenuItem.Checked = true;
  1233. button_Start.Enabled = true;
  1234. //关闭串口
  1235. portsettings_form.closePort();
  1236. //存储测试数据
  1237. ErrorSaveLog();
  1238. return;
  1239. }
  1240. #endregion
  1241. #region Step2.5:写入生产信息
  1242. Delay_ms(200);
  1243. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入生产信息" + "\r\n";
  1244. Array.Clear(Code, 0, 32);
  1245. string MAC_Info = array_CfgInfo[50].ToString().Split(',')[1];
  1246. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1247. {
  1248. Code[i] = (byte)MAC_Info[i];
  1249. }
  1250. MAC_Info = array_CfgInfo[51].ToString().Split(',')[1];
  1251. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1252. {
  1253. Code[8 + i] = (byte)MAC_Info[i];
  1254. }
  1255. MAC_Info = array_CfgInfo[52].ToString().Split(',')[1];
  1256. for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
  1257. {
  1258. Code[16 + i] = (byte)MAC_Info[i];
  1259. }
  1260. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, Code);
  1261. Ack_Flag = false;
  1262. Ack_Cnt = 0;
  1263. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1264. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1265. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1266. {
  1267. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入成功" + "\r\n";
  1268. }
  1269. else//失败
  1270. {
  1271. label_Step.Text = "参数写入失败,测试结束!";
  1272. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "生产信息写入失败,测试结束" + "\r\n";
  1273. //关机
  1274. Code[0] = 0xF0;
  1275. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1276. 开ToolStripMenuItem.Checked = false;
  1277. 关ToolStripMenuItem.Checked = true;
  1278. button_Start.Enabled = true;
  1279. //关闭串口
  1280. portsettings_form.closePort();
  1281. //存储测试数据
  1282. ErrorSaveLog();
  1283. return;
  1284. }
  1285. #endregion
  1286. #region Step2.6:写入校验密钥
  1287. Delay_ms(200);
  1288. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入校验密钥" + "\r\n";
  1289. Array.Clear(Code, 0, 32);
  1290. if (array_CfgInfo[53].ToString().EndsWith(",") == false)
  1291. {
  1292. string KeyString = array_CfgInfo[53].ToString().Split(',')[1];
  1293. for (ushort i = 0; i < ((KeyString.Length > 8) ? 8 : KeyString.Length); i++)
  1294. {
  1295. Code[i] = (byte)KeyString[i];
  1296. }
  1297. }
  1298. else
  1299. {
  1300. Code.Initialize();
  1301. }
  1302. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, Code);
  1303. Ack_Flag = false;
  1304. Ack_Cnt = 0;
  1305. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1306. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1307. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1308. {
  1309. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入成功" + "\r\n";
  1310. }
  1311. else//失败
  1312. {
  1313. label_Step.Text = "参数写入失败,测试结束!";
  1314. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校验密钥写入失败,测试结束" + "\r\n";
  1315. //关机
  1316. Code[0] = 0xF0;
  1317. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1318. 开ToolStripMenuItem.Checked = false;
  1319. 关ToolStripMenuItem.Checked = true;
  1320. button_Start.Enabled = true;
  1321. //关闭串口
  1322. portsettings_form.closePort();
  1323. //存储测试数据
  1324. ErrorSaveLog();
  1325. return;
  1326. }
  1327. #endregion
  1328. #region Step2.7:写入自定义信息1、2、3
  1329. //自定义信息1
  1330. Delay_ms(200);
  1331. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息1" + "\r\n";
  1332. Array.Clear(Code, 0, 32);
  1333. if (array_CfgInfo[54].ToString().EndsWith(",") == false)
  1334. {
  1335. string UserInfo1String = array_CfgInfo[54].ToString().Split(',')[1];
  1336. for (ushort i = 0; i < (UserInfo1String.Length > 16 ? 16 : UserInfo1String.Length); i++)
  1337. {
  1338. Code[i] = (byte)UserInfo1String[i];
  1339. }
  1340. }
  1341. else
  1342. {
  1343. Array.Clear(Code, 0, 16);
  1344. }
  1345. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, Code);
  1346. Ack_Flag = false;
  1347. Ack_Cnt = 0;
  1348. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1349. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1350. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1351. {
  1352. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入成功" + "\r\n";
  1353. }
  1354. else//失败
  1355. {
  1356. label_Step.Text = "参数写入失败,测试结束!";
  1357. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息1写入失败,测试结束" + "\r\n";
  1358. //关机
  1359. Code[0] = 0xF0;
  1360. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1361. 开ToolStripMenuItem.Checked = false;
  1362. 关ToolStripMenuItem.Checked = true;
  1363. button_Start.Enabled = true;
  1364. //关闭串口
  1365. portsettings_form.closePort();
  1366. //存储测试数据
  1367. ErrorSaveLog();
  1368. return;
  1369. }
  1370. //自定义信息2
  1371. Delay_ms(200);
  1372. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息2" + "\r\n";
  1373. Array.Clear(Code, 0, 32);
  1374. if (array_CfgInfo[55].ToString().EndsWith(",") == false)
  1375. {
  1376. string UserInfo2String = array_CfgInfo[55].ToString().Split(',')[1];
  1377. for (ushort i = 0; i < (UserInfo2String.Length > 16 ? 16 : UserInfo2String.Length); i++)
  1378. {
  1379. Code[i] = (byte)UserInfo2String[i];
  1380. }
  1381. }
  1382. else
  1383. {
  1384. Array.Clear(Code, 0, 16);
  1385. }
  1386. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, Code);
  1387. Ack_Flag = false;
  1388. Ack_Cnt = 0;
  1389. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1390. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1391. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1392. {
  1393. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入成功" + "\r\n";
  1394. }
  1395. else//失败
  1396. {
  1397. label_Step.Text = "参数写入失败,测试结束!";
  1398. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息2写入失败,测试结束" + "\r\n";
  1399. //关机
  1400. Code[0] = 0xF0;
  1401. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1402. 开ToolStripMenuItem.Checked = false;
  1403. 关ToolStripMenuItem.Checked = true;
  1404. button_Start.Enabled = true;
  1405. //关闭串口
  1406. portsettings_form.closePort();
  1407. //存储测试数据
  1408. ErrorSaveLog();
  1409. return;
  1410. }
  1411. //自定义信息3
  1412. Delay_ms(200);
  1413. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "写入自定义信息3" + "\r\n";
  1414. Array.Clear(Code, 0, 32);
  1415. if (array_CfgInfo[56].ToString().EndsWith(",") == false)
  1416. {
  1417. string UserInfo3String = array_CfgInfo[56].ToString().Split(',')[1];
  1418. for (ushort i = 0; i < (UserInfo3String.Length > 16 ? 16 : UserInfo3String.Length); i++)
  1419. {
  1420. Code[i] = (byte)UserInfo3String[i];
  1421. }
  1422. }
  1423. else
  1424. {
  1425. Array.Clear(Code, 0, 16);
  1426. }
  1427. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, Code);
  1428. Ack_Flag = false;
  1429. Ack_Cnt = 0;
  1430. StepTestFlag = StepTestResult_Enum.StepTestResult_Testing;
  1431. while (StepTestFlag == StepTestResult_Enum.StepTestResult_Testing) ;//等待测试结束
  1432. if (StepTestFlag == StepTestResult_Enum.StepTestResult_OK)//成功
  1433. {
  1434. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入成功" + "\r\n";
  1435. }
  1436. else//失败
  1437. {
  1438. label_Step.Text = "参数写入失败,测试结束!";
  1439. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "自定义信息3写入失败,测试结束" + "\r\n";
  1440. //关机
  1441. Code[0] = 0xF0;
  1442. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1443. 开ToolStripMenuItem.Checked = false;
  1444. 关ToolStripMenuItem.Checked = true;
  1445. button_Start.Enabled = true;
  1446. //关闭串口
  1447. portsettings_form.closePort();
  1448. //存储测试数据
  1449. ErrorSaveLog();
  1450. return;
  1451. }
  1452. #endregion
  1453. //参数写入完成
  1454. label_Sta_Write.BackColor = Color.Green;
  1455. label_Sta_Write.Text = "OK";
  1456. #endregion
  1457. #region Step3:灯电压测试
  1458. TestStep = TestStep_Enum.Step_Light;
  1459. label_Step.Text = "灯电压测试,请确认电压!";
  1460. button_LightCtl.Text = "开灯";
  1461. LightProcess();
  1462. radioButton_Light_OK.Checked = false;
  1463. radioButton_Light_OK.BackColor = Color.Green;
  1464. radioButton_Light_OK.ForeColor = Color.White;
  1465. radioButton_Light_OK.Enabled = true;
  1466. radioButton_Light_NG.Checked = false;
  1467. radioButton_Light_NG.Enabled = true;
  1468. radioButton_Light_NG.BackColor = Color.Red;
  1469. radioButton_Light_NG.ForeColor = Color.White;
  1470. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送开灯指令,请确认灯电压是否正确……" + "\r\n";
  1471. while ((radioButton_Light_OK.Checked == false) && (radioButton_Light_NG.Checked == false)) ;
  1472. if (radioButton_Light_OK.Checked == true)
  1473. {
  1474. label_sta_light.BackColor = Color.Green;
  1475. radioButton_Light_NG.BackColor = Color.Gray;
  1476. label_sta_light.Text = "OK";
  1477. radioButton_Light_OK.Enabled = false;
  1478. radioButton_Light_NG.Enabled = false;
  1479. }
  1480. else if (radioButton_Light_NG.Checked == true)
  1481. {
  1482. label_sta_light.BackColor = Color.Red;
  1483. radioButton_Light_OK.BackColor = Color.Gray;
  1484. label_sta_light.Text = "NG";
  1485. label_Step.Text = "灯电压错误,测试结束!";
  1486. button_LightCtl.Text = "关灯";
  1487. LightProcess();
  1488. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压错误,测试结束" + "\r\n";
  1489. //关机
  1490. Code[0] = 0xF0;
  1491. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1492. 开ToolStripMenuItem.Checked = false;
  1493. 关ToolStripMenuItem.Checked = true;
  1494. button_Start.Enabled = true;
  1495. radioButton_Light_OK.Enabled = false;
  1496. radioButton_Light_NG.Enabled = false;
  1497. //关闭串口
  1498. portsettings_form.closePort();
  1499. //存储测试数据
  1500. ErrorSaveLog();
  1501. return;
  1502. }
  1503. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "灯电压确认完成" + "\r\n";
  1504. button_LightCtl.Text = "关灯";
  1505. LightProcess();
  1506. #endregion
  1507. #region Step4:推行测试
  1508. TestStep = TestStep_Enum.Step_Walk;
  1509. label_Step.Text = "推行测试,电机开始自动运转!";
  1510. button_Walk.Text = "启动";
  1511. Walk_Process(0x22);
  1512. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机进入推行模式,请注意安全!" + "\r\n";
  1513. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测开始" + "\r\n";
  1514. OK_cnt = 0;
  1515. WalkProcess_Cnt = 0;
  1516. while (WalkProcess_Cnt < 20)//开始判断传感器,超时20s
  1517. {
  1518. //车速传感器
  1519. if (SpeedSensorTest == false)
  1520. {
  1521. if (label_sensor_speed.BackColor == Color.Red)
  1522. {
  1523. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过车速传感器测试" + "\r\n";
  1524. label_sensor_speed.BackColor = Color.Green;
  1525. label_sensor_speed.Text = "OK";
  1526. OK_cnt++;
  1527. }
  1528. }
  1529. else
  1530. {
  1531. try
  1532. {
  1533. DataTemp = (ushort)(Convert.ToDecimal(textBox_BikeSpeed.Text) * 10);
  1534. }
  1535. catch (System.Exception ex)
  1536. {
  1537. MessageBox.Show(ex.Message);
  1538. label_Step.Text = "推行测试异常,测试结束!";
  1539. //关机
  1540. Code[0] = 0xF0;
  1541. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1542. 开ToolStripMenuItem.Checked = false;
  1543. 关ToolStripMenuItem.Checked = true;
  1544. button_Start.Enabled = true;
  1545. //关闭串口
  1546. portsettings_form.closePort();
  1547. //存储测试数据
  1548. ErrorSaveLog();
  1549. return;
  1550. }
  1551. if ((DataTemp > (SpeedSensorThData - 50)) && (DataTemp < (SpeedSensorThData + 50)))
  1552. {
  1553. if (label_sensor_speed.BackColor == Color.Red)
  1554. {
  1555. label_sensor_speed.BackColor = Color.Green;
  1556. label_sensor_speed.Text = "OK";
  1557. OK_cnt++;
  1558. }
  1559. }
  1560. }
  1561. //马达转速
  1562. try
  1563. {
  1564. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1565. }
  1566. catch (System.Exception ex)
  1567. {
  1568. MessageBox.Show(ex.Message);
  1569. label_Step.Text = "推行测试异常,测试结束!";
  1570. //关机
  1571. Code[0] = 0xF0;
  1572. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1573. 开ToolStripMenuItem.Checked = false;
  1574. 关ToolStripMenuItem.Checked = true;
  1575. button_Start.Enabled = true;
  1576. //关闭串口
  1577. portsettings_form.closePort();
  1578. //存储测试数据
  1579. ErrorSaveLog();
  1580. return;
  1581. }
  1582. if ((DataTemp > (MotorRadeSpeed - 200)) && (DataTemp < (MotorRadeSpeed + 200)))
  1583. {
  1584. if (label_sensor_motor.BackColor == Color.Red)
  1585. {
  1586. label_sensor_motor.BackColor = Color.Green;
  1587. label_sensor_motor.Text = "OK";
  1588. OK_cnt++;
  1589. }
  1590. }
  1591. //母线电压
  1592. try
  1593. {
  1594. DataTemp = Convert.ToUInt16(textBox_Vol.Text);
  1595. }
  1596. catch (System.Exception ex)
  1597. {
  1598. MessageBox.Show(ex.Message);
  1599. label_Step.Text = "推行测试异常,测试结束!";
  1600. //关机
  1601. Code[0] = 0xF0;
  1602. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1603. 开ToolStripMenuItem.Checked = false;
  1604. 关ToolStripMenuItem.Checked = true;
  1605. button_Start.Enabled = true;
  1606. //关闭串口
  1607. portsettings_form.closePort();
  1608. //存储测试数据
  1609. ErrorSaveLog();
  1610. return;
  1611. }
  1612. if ((DataTemp > (RateVoltage - 1000)) && (DataTemp < (RateVoltage + 1000)))
  1613. {
  1614. if (label_sensor_vol.BackColor == Color.Red)
  1615. {
  1616. label_sensor_vol.BackColor = Color.Green;
  1617. label_sensor_vol.Text = "OK";
  1618. OK_cnt++;
  1619. }
  1620. }
  1621. //母线电流
  1622. try
  1623. {
  1624. DataTemp = Convert.ToUInt16(textBox_Cur.Text);
  1625. }
  1626. catch (System.Exception ex)
  1627. {
  1628. MessageBox.Show(ex.Message);
  1629. label_Step.Text = "推行测试异常,测试结束!";
  1630. //关机
  1631. Code[0] = 0xF0;
  1632. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1633. 开ToolStripMenuItem.Checked = false;
  1634. 关ToolStripMenuItem.Checked = true;
  1635. button_Start.Enabled = true;
  1636. //关闭串口
  1637. portsettings_form.closePort();
  1638. //存储测试数据
  1639. ErrorSaveLog();
  1640. return;
  1641. }
  1642. if ((DataTemp > No_load_Current - 600) && (DataTemp < No_load_Current + 600))
  1643. {
  1644. if (label_sensor_cur.BackColor == Color.Red)
  1645. {
  1646. label_sensor_cur.BackColor = Color.Green;
  1647. label_sensor_cur.Text = "OK";
  1648. OK_cnt++;
  1649. }
  1650. }
  1651. //踏频
  1652. try
  1653. {
  1654. DataTemp = Convert.ToUInt16(textBox_Cadence.Text);
  1655. }
  1656. catch (System.Exception ex)
  1657. {
  1658. MessageBox.Show(ex.Message);
  1659. label_Step.Text = "推行测试异常,测试结束!";
  1660. //关机
  1661. Code[0] = 0xF0;
  1662. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1663. 开ToolStripMenuItem.Checked = false;
  1664. 关ToolStripMenuItem.Checked = true;
  1665. button_Start.Enabled = true;
  1666. //关闭串口
  1667. portsettings_form.closePort();
  1668. //存储测试数据
  1669. ErrorSaveLog();
  1670. return;
  1671. }
  1672. if ((DataTemp > (RateCadence - 20)) && (DataTemp < (RateCadence + 20)))
  1673. {
  1674. if (label_sensor_cadence.BackColor == Color.Red)
  1675. {
  1676. label_sensor_cadence.BackColor = Color.Green;
  1677. label_sensor_cadence.Text = "OK";
  1678. OK_cnt++;
  1679. }
  1680. }
  1681. //PCB温度
  1682. try
  1683. {
  1684. DataTemp_Int16 = Convert.ToInt16(textBox_PCB.Text);
  1685. }
  1686. catch (System.Exception ex)
  1687. {
  1688. MessageBox.Show(ex.Message);
  1689. label_Step.Text = "推行测试异常,测试结束!";
  1690. //关机
  1691. Code[0] = 0xF0;
  1692. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1693. 开ToolStripMenuItem.Checked = false;
  1694. 关ToolStripMenuItem.Checked = true;
  1695. button_Start.Enabled = true;
  1696. //关闭串口
  1697. portsettings_form.closePort();
  1698. //存储测试数据
  1699. ErrorSaveLog();
  1700. return;
  1701. }
  1702. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1703. {
  1704. if (label_sensor_PCB_temp.BackColor == Color.Red)
  1705. {
  1706. label_sensor_PCB_temp.BackColor = Color.Green;
  1707. label_sensor_PCB_temp.Text = "OK";
  1708. OK_cnt++;
  1709. }
  1710. }
  1711. //绕组温度
  1712. try
  1713. {
  1714. DataTemp_Int16 = Convert.ToInt16(textBox_Coil.Text);
  1715. }
  1716. catch (System.Exception ex)
  1717. {
  1718. MessageBox.Show(ex.Message);
  1719. label_Step.Text = "推行测试异常,测试结束!";
  1720. //关机
  1721. Code[0] = 0xF0;
  1722. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1723. 开ToolStripMenuItem.Checked = false;
  1724. 关ToolStripMenuItem.Checked = true;
  1725. button_Start.Enabled = true;
  1726. //关闭串口
  1727. portsettings_form.closePort();
  1728. //存储测试数据
  1729. ErrorSaveLog();
  1730. return;
  1731. }
  1732. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1733. {
  1734. if (label_sensor_coil_temp.BackColor == Color.Red)
  1735. {
  1736. label_sensor_coil_temp.BackColor = Color.Green;
  1737. label_sensor_coil_temp.Text = "OK";
  1738. OK_cnt++;
  1739. }
  1740. }
  1741. //MCU温度
  1742. try
  1743. {
  1744. DataTemp_Int16 = Convert.ToInt16(textBox_MCU.Text);
  1745. }
  1746. catch (System.Exception ex)
  1747. {
  1748. MessageBox.Show(ex.Message);
  1749. label_Step.Text = "推行测试异常,测试结束!";
  1750. //关机
  1751. Code[0] = 0xF0;
  1752. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1753. 开ToolStripMenuItem.Checked = false;
  1754. 关ToolStripMenuItem.Checked = true;
  1755. button_Start.Enabled = true;
  1756. //关闭串口
  1757. portsettings_form.closePort();
  1758. //存储测试数据
  1759. ErrorSaveLog();
  1760. return;
  1761. }
  1762. if ((DataTemp_Int16 > -20) && (DataTemp_Int16 < 60))
  1763. {
  1764. if (label_sensor_MCU_temp.BackColor == Color.Red)
  1765. {
  1766. label_sensor_MCU_temp.BackColor = Color.Green;
  1767. label_sensor_MCU_temp.Text = "OK";
  1768. OK_cnt++;
  1769. }
  1770. }
  1771. //测试完成,退出
  1772. if (OK_cnt == 8)
  1773. {
  1774. break;
  1775. }
  1776. }
  1777. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器自动检测结束" + "\r\n";
  1778. //断电开关1
  1779. label_Step.Text = "请在10s内捏下断电刹把 1!";
  1780. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 1 检测开始,请捏下断电刹把 1" + "\r\n";
  1781. WalkProcess_Cnt = 0;
  1782. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1783. {
  1784. try
  1785. {
  1786. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1787. }
  1788. catch (System.Exception ex)
  1789. {
  1790. MessageBox.Show(ex.Message);
  1791. label_Step.Text = "推行测试异常,测试结束!";
  1792. //关机
  1793. Code[0] = 0xF0;
  1794. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1795. 开ToolStripMenuItem.Checked = false;
  1796. 关ToolStripMenuItem.Checked = true;
  1797. button_Start.Enabled = true;
  1798. //关闭串口
  1799. portsettings_form.closePort();
  1800. //存储测试数据
  1801. ErrorSaveLog();
  1802. return;
  1803. }
  1804. if (DataTemp < 100)
  1805. {
  1806. if (label_Sensor_Break1.BackColor == Color.Red)
  1807. {
  1808. label_Sensor_Break1.BackColor = Color.Green;
  1809. label_Sensor_Break1.Text = "OK";
  1810. OK_cnt++;
  1811. }
  1812. break;
  1813. }
  1814. }
  1815. label_Step.Text = "请松开断电刹把 1";
  1816. WalkProcess_Cnt = 0;
  1817. while (WalkProcess_Cnt < 10)//等待电机再次启动,如果超时提示错误
  1818. {
  1819. try
  1820. {
  1821. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1822. }
  1823. catch (System.Exception ex)
  1824. {
  1825. MessageBox.Show(ex.Message);
  1826. label_Step.Text = "推行测试异常,测试结束!";
  1827. //关机
  1828. Code[0] = 0xF0;
  1829. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1830. 开ToolStripMenuItem.Checked = false;
  1831. 关ToolStripMenuItem.Checked = true;
  1832. button_Start.Enabled = true;
  1833. //关闭串口
  1834. portsettings_form.closePort();
  1835. //存储测试数据
  1836. ErrorSaveLog();
  1837. return;
  1838. }
  1839. if (DataTemp > 300)//电机转速超过300rpm,再次启动测试
  1840. break;
  1841. }
  1842. if (WalkProcess_Cnt >= 10)//表示超时退出,电机未启动
  1843. {
  1844. label_Step.Text = "推行测试异常,测试结束!";
  1845. //关机
  1846. Code[0] = 0xF0;
  1847. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1848. 开ToolStripMenuItem.Checked = false;
  1849. 关ToolStripMenuItem.Checked = true;
  1850. button_Start.Enabled = true;
  1851. //关闭串口
  1852. portsettings_form.closePort();
  1853. //存储测试数据
  1854. ErrorSaveLog();
  1855. return;
  1856. }
  1857. //断电开关2
  1858. label_Step.Text = "请在10s内捏下断电刹把 2!";
  1859. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把 2 检测开始,请捏下断电刹把 2" + "\r\n";
  1860. WalkProcess_Cnt = 0;
  1861. while (WalkProcess_Cnt < 15)//10s内判断电机是否停止
  1862. {
  1863. try
  1864. {
  1865. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1866. }
  1867. catch (System.Exception ex)
  1868. {
  1869. MessageBox.Show(ex.Message);
  1870. label_Step.Text = "推行测试异常,测试结束!";
  1871. //关机
  1872. Code[0] = 0xF0;
  1873. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1874. 开ToolStripMenuItem.Checked = false;
  1875. 关ToolStripMenuItem.Checked = true;
  1876. button_Start.Enabled = true;
  1877. //关闭串口
  1878. portsettings_form.closePort();
  1879. //存储测试数据
  1880. ErrorSaveLog();
  1881. return;
  1882. }
  1883. if (DataTemp < 100)
  1884. {
  1885. if (label_Sensor_Break2.BackColor == Color.Red)
  1886. {
  1887. label_Sensor_Break2.BackColor = Color.Green;
  1888. label_Sensor_Break2.Text = "OK";
  1889. OK_cnt++;
  1890. }
  1891. break;
  1892. }
  1893. }
  1894. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "断电刹把检测结束" + "\r\n";
  1895. label_Step.Text = "请松开断电刹把 2!";
  1896. button_Walk.Text = "停止";
  1897. Walk_Process(0x00);
  1898. Delay_ms(1000);
  1899. //判断测试结果
  1900. if (OK_cnt < 10)
  1901. {
  1902. label_Step.Text = "推行测试异常,测试结束!";
  1903. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1904. //关机
  1905. Code[0] = 0xF0;
  1906. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1907. 开ToolStripMenuItem.Checked = false;
  1908. 关ToolStripMenuItem.Checked = true;
  1909. button_Start.Enabled = true;
  1910. //关闭串口
  1911. portsettings_form.closePort();
  1912. //存储测试数据
  1913. ErrorSaveLog();
  1914. return;
  1915. }
  1916. //判断BMS通信是否正常
  1917. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "检测BMS通信状态" + "\r\n";
  1918. if (label_BMS_COM.Text != "OK")
  1919. {
  1920. label_Step.Text = "BMS通信异常,测试结束!";
  1921. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "推行测试失败,测试结束" + "\r\n";
  1922. //关机
  1923. Code[0] = 0xF0;
  1924. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1925. 开ToolStripMenuItem.Checked = false;
  1926. 关ToolStripMenuItem.Checked = true;
  1927. button_Start.Enabled = true;
  1928. //关闭串口
  1929. portsettings_form.closePort();
  1930. //存储测试数据
  1931. ErrorSaveLog();
  1932. return;
  1933. }
  1934. #endregion
  1935. #region Step5:转把模式测试(非必需)
  1936. if (GasModeTest == false)
  1937. {
  1938. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "跳过指拨模式测试" + "\r\n";
  1939. label_Sensor_Gas.BackColor = Color.Green;
  1940. label_Sensor_Gas.Text = "OK";
  1941. }
  1942. else
  1943. {
  1944. TestStep = TestStep_Enum.Step_GasMode;
  1945. label_Step.Text = "指拨模式测试,请在10s内转动指拨!";
  1946. button_Walk.Text = "启动";
  1947. Walk_Process(0x04);
  1948. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试开始,请转动指拨" + "\r\n";
  1949. OK_cnt = 0;
  1950. WalkProcess_Cnt = 0;
  1951. while (WalkProcess_Cnt < 10)//10s内判断电机是否启动
  1952. {
  1953. try
  1954. {
  1955. DataTemp = Convert.ToUInt16(textBox_MotorSpeed.Text);
  1956. }
  1957. catch (System.Exception ex)
  1958. {
  1959. MessageBox.Show(ex.Message);
  1960. label_Step.Text = "推行测试异常,测试结束!";
  1961. //关机
  1962. Code[0] = 0xF0;
  1963. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1964. 开ToolStripMenuItem.Checked = false;
  1965. 关ToolStripMenuItem.Checked = true;
  1966. button_Start.Enabled = true;
  1967. //关闭串口
  1968. portsettings_form.closePort();
  1969. //存储测试数据
  1970. ErrorSaveLog();
  1971. return;
  1972. }
  1973. if ((DataTemp > (GasModeSpeed - 100)) && (DataTemp < (GasModeSpeed + 100)))
  1974. {
  1975. label_Sensor_Gas.BackColor = Color.Green;
  1976. label_Sensor_Gas.Text = "OK";
  1977. OK_cnt++;
  1978. break;
  1979. }
  1980. }
  1981. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试结束" + "\r\n";
  1982. Delay_ms(1000);
  1983. button_Walk.Text = "停止";
  1984. Walk_Process(0x00);
  1985. //判断测试结果
  1986. if (OK_cnt < 1)
  1987. {
  1988. label_Step.Text = "指拨模式测试异常,测试结束!";
  1989. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "指拨模式测试失败,测试结束" + "\r\n";
  1990. //关机
  1991. Code[0] = 0xF0;
  1992. SendCmd(0x7FF, 0x16, 0x2201, Code);
  1993. 开ToolStripMenuItem.Checked = false;
  1994. 关ToolStripMenuItem.Checked = true;
  1995. button_Start.Enabled = true;
  1996. //关闭串口
  1997. portsettings_form.closePort();
  1998. //存储测试数据
  1999. ErrorSaveLog();
  2000. return;
  2001. }
  2002. }
  2003. #endregion
  2004. #region Step6:版本查询和确认
  2005. TestStep = TestStep_Enum.Step_CheckVer;
  2006. label_Step.Text = "版本查询和确认";
  2007. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n";
  2008. richTextBox_Ver.Text = "";
  2009. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  2010. Delay_ms(200);
  2011. SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  2012. Delay_ms(200);
  2013. string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3];
  2014. string FW_Version_Result = "";
  2015. string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5];
  2016. string TE_FW_Ver_Result = "";
  2017. try
  2018. {
  2019. FW_Version_Result = richTextBox_Ver.Lines[3].Split(':')[1];
  2020. FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_Ver.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1];
  2021. TE_FW_Ver_Result = richTextBox_Ver.Lines[5].Split(':')[1];
  2022. }
  2023. catch (System.Exception)
  2024. {
  2025. label_Step.Text = "软件版本信息不完整!";
  2026. }
  2027. if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束
  2028. {
  2029. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息错误" + "\r\n";
  2030. label_Step.Text = "软件版本错误,测试结束!";
  2031. //关机
  2032. Code[0] = 0xF0;
  2033. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2034. 开ToolStripMenuItem.Checked = false;
  2035. 关ToolStripMenuItem.Checked = true;
  2036. button_Start.Enabled = true;
  2037. //关闭串口
  2038. portsettings_form.closePort();
  2039. //存储测试数据
  2040. ErrorSaveLog();
  2041. return;
  2042. }
  2043. else
  2044. {
  2045. if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示
  2046. {
  2047. if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No)
  2048. {
  2049. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE软件版本错误" + "\r\n";
  2050. label_Step.Text = "TE软件版本错误,测试结束!";
  2051. //关机
  2052. Code[0] = 0xF0;
  2053. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2054. 开ToolStripMenuItem.Checked = false;
  2055. 关ToolStripMenuItem.Checked = true;
  2056. button_Start.Enabled = true;
  2057. //关闭串口
  2058. portsettings_form.closePort();
  2059. //存储测试数据
  2060. ErrorSaveLog();
  2061. return;
  2062. }
  2063. }
  2064. label_FW_Version.BackColor = Color.Green;
  2065. label_FW_Version.Text = "OK";
  2066. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n";
  2067. }
  2068. Delay_ms(1000);
  2069. #endregion
  2070. #region 按照产品信息设置测试文件路径
  2071. ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_");
  2072. if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false)
  2073. System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName);
  2074. #endregion
  2075. #region Step7:参数查询和保存
  2076. TestStep = TestStep_Enum.Step_Save;
  2077. label_Step.Text = "参数导出";
  2078. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始导出电机内部参数" + "\r\n";
  2079. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\ParamsLog" + ".ttlog";
  2080. //开始发送指令
  2081. byte[] SendData = new byte[32];
  2082. //查询校验密钥
  2083. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
  2084. Delay_ms(100);
  2085. //查询版本信息
  2086. //SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
  2087. //Delay_ms(100);
  2088. //SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
  2089. //Delay_ms(100);
  2090. //查询用户字符串1
  2091. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
  2092. Delay_ms(100);
  2093. //查询用户字符串2
  2094. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
  2095. Delay_ms(100);
  2096. //查询用户字符串3
  2097. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
  2098. Delay_ms(100);
  2099. //查询用户参数1
  2100. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
  2101. Delay_ms(100);
  2102. //查询马达参数
  2103. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
  2104. Delay_ms(100);
  2105. //查询历史信息
  2106. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
  2107. Delay_ms(100);
  2108. //查询生产信息
  2109. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
  2110. Delay_ms(100);
  2111. //查询力矩传感器零点 Volans
  2112. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);
  2113. Delay_ms(100);
  2114. //查询运行信息
  2115. SendData[0] = 0x00;
  2116. SendData[1] = 0xF0;
  2117. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2118. Delay_ms(100);
  2119. SendData[0] = 0x01;
  2120. SendData[1] = 0xF0;
  2121. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2122. Delay_ms(100);
  2123. SendData[0] = 0x02;
  2124. SendData[1] = 0xF0;
  2125. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2126. Delay_ms(100);
  2127. SendData[0] = 0x03;
  2128. SendData[1] = 0xF0;
  2129. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2130. Delay_ms(100);
  2131. SendData[0] = 0x04;
  2132. SendData[1] = 0xF0;
  2133. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2134. Delay_ms(100);
  2135. SendData[0] = 0x33;
  2136. SendData[1] = 0xF0;
  2137. SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
  2138. Delay_ms(100);
  2139. //查询力矩传感器校正信息 Volans
  2140. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
  2141. Delay_ms(100);
  2142. //导出故障日志
  2143. UInt32 Address_Begin;
  2144. UInt32 Addres_End;
  2145. Address_Begin = 0x0801F800;
  2146. Addres_End = 0x0801FBFF;
  2147. UInt32 ReadCount = 0;
  2148. ReadCount = (Addres_End - Address_Begin + 1) / 128;
  2149. for (ushort i = 0; i < ReadCount; i++)
  2150. {
  2151. var SendByte = new byte[8];
  2152. UInt32 Address_Temp_Begin;
  2153. UInt32 Address_Temp_End;
  2154. Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
  2155. SendByte[0] = (byte)(Address_Temp_Begin >> 24);
  2156. SendByte[1] = (byte)(Address_Temp_Begin >> 16);
  2157. SendByte[2] = (byte)(Address_Temp_Begin >> 8);
  2158. SendByte[3] = (byte)(Address_Temp_Begin);
  2159. Address_Temp_End = Address_Temp_Begin + 127;
  2160. SendByte[4] = (byte)(Address_Temp_End >> 24);
  2161. SendByte[5] = (byte)(Address_Temp_End >> 16);
  2162. SendByte[6] = (byte)(Address_Temp_End >> 8);
  2163. SendByte[7] = (byte)(Address_Temp_End);
  2164. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
  2165. Delay_ms(100);
  2166. }
  2167. //查询骑行信息
  2168. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
  2169. Delay_ms(100);
  2170. //查询用户参数2
  2171. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
  2172. Delay_ms(100);
  2173. //查询力矩传感器零点值 Pegasi
  2174. SendData[0] = 1;
  2175. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2176. Delay_ms(100);
  2177. SendData[0] = 2;
  2178. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2179. Delay_ms(100);
  2180. SendData[0] = 3;
  2181. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
  2182. Delay_ms(100);
  2183. //查询校正系数 Pegasi
  2184. SendData[0] = 1;
  2185. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2186. Delay_ms(100);
  2187. SendData[0] = 2;
  2188. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2189. Delay_ms(100);
  2190. SendData[0] = 3;
  2191. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
  2192. Delay_ms(100);
  2193. //查询启动值 Pegasi
  2194. SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
  2195. Delay_ms(100);
  2196. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机参数导出完成" + "\r\n";
  2197. #endregion
  2198. //检查是否有故障
  2199. if (label_error_code.Text != "OK")
  2200. {
  2201. label_Step.Text = "电机存在故障!";
  2202. ErrorSaveLog();
  2203. return;
  2204. }
  2205. //显示测试结果
  2206. label_Result.BackColor = Color.Green;
  2207. label_Result.Text = "合格";
  2208. label_Step.Text = "合格,测试结束!";
  2209. label_Step.ForeColor = Color.Green;
  2210. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "合格,测试结束" + "\r\n";
  2211. //关机
  2212. Code[0] = 0xF0;
  2213. SendCmd(0x7FF, 0x16, 0x2201, Code);
  2214. 开ToolStripMenuItem.Checked = false;
  2215. 关ToolStripMenuItem.Checked = true;
  2216. button_Start.Enabled = true;
  2217. //关闭串口
  2218. portsettings_form.closePort();
  2219. //导出的参数上传服务器
  2220. if (StartForm.myFtp.IsNetConnected == true)
  2221. {
  2222. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2223. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2224. {
  2225. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2226. }
  2227. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2228. {
  2229. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2230. }
  2231. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2232. if (result == true)
  2233. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数上传服务器成功" + "\r\n";
  2234. }
  2235. //保存测试信息
  2236. //页面存储
  2237. Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
  2238. Graphics g = Graphics.FromImage(bit);
  2239. g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
  2240. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试页面" + ".png";
  2241. bit.Save(LogSaveFileName);
  2242. //测试页面上传服务器
  2243. if (StartForm.myFtp.IsNetConnected == true)
  2244. {
  2245. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2246. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2247. {
  2248. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2249. }
  2250. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2251. {
  2252. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2253. }
  2254. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2255. if (result == true)
  2256. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n";
  2257. }
  2258. //保存测试记录
  2259. LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".txt";
  2260. richTextBox_TestRecord.SaveFile(LogSaveFileName, RichTextBoxStreamType.PlainText);
  2261. //测试记录上传服务器
  2262. if (StartForm.myFtp.IsNetConnected == true)
  2263. {
  2264. string DateNow = DateTime.Now.ToString("yyyy-MM-dd");
  2265. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/", DateNow) == false)
  2266. {
  2267. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow);
  2268. }
  2269. if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow, ResultPathName) == false)
  2270. {
  2271. StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2272. }
  2273. bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/walkTest/log/" + DateNow + "/" + ResultPathName);
  2274. if (result == true)
  2275. richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试记录上传服务器成功" + "\r\n";
  2276. }
  2277. }
  2278. //开始测试
  2279. private void button_Start_Click(object sender, EventArgs e)
  2280. {
  2281. TestStart();
  2282. }
  2283. //测试子函数
  2284. private void TestStart()
  2285. {
  2286. //标签初始化
  2287. label_Init();
  2288. //检查生产地设置
  2289. if ((执行ToolStripMenuItem.Checked == false) && (不执行ToolStripMenuItem.Checked == false))
  2290. {
  2291. label_Step.Text = "请先选择是否执行\"系统清除\"!";
  2292. label_Step.ForeColor = Color.Red;
  2293. return;
  2294. }
  2295. #region 检查配置文件
  2296. if (comboBox_File.Items.Count == 0) //配置文件目录为空
  2297. {
  2298. label_Step.Text = "无可用配置文件!";
  2299. label_Step.ForeColor = Color.Red;
  2300. return;
  2301. }
  2302. if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
  2303. {
  2304. label_Step.Text = "请选择配置文件!";
  2305. return;
  2306. }
  2307. #endregion
  2308. #region 检查产品信息是否符合格式要求
  2309. try
  2310. {
  2311. string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0];
  2312. string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1];
  2313. 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]$");
  2314. if (regex.IsMatch(Sn) == false)
  2315. {
  2316. label_Step.Text = "SN格式错误!";
  2317. label_Step.ForeColor = Color.Red;
  2318. return;
  2319. }
  2320. }
  2321. catch (System.Exception ex)
  2322. {
  2323. label_Step.Text = "标签格式错误!";
  2324. label_Step.ForeColor = Color.Red;
  2325. return;
  2326. }
  2327. #endregion
  2328. //连接串口
  2329. if (portsettings_form.openPort() == false)
  2330. {
  2331. label_Step.Text = "串口连接失败!";
  2332. label_Step.ForeColor = Color.Red;
  2333. return;
  2334. }
  2335. //测试状态更新
  2336. TestStep = TestStep_Enum.Step_Init;
  2337. label_Step.Text = "准备开始测试";
  2338. label_Step.ForeColor = Color.Red;
  2339. Delay_ms(500);
  2340. //流水号累加和存储
  2341. label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString();
  2342. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2343. StreamReader objReader = new StreamReader(cfg_file);
  2344. string sLine = "";
  2345. ArrayList arrText = new ArrayList();//创建一个动态数组
  2346. while (sLine != null)
  2347. {
  2348. sLine = objReader.ReadLine();
  2349. arrText.Add(sLine);
  2350. }
  2351. arrText.RemoveAt(arrText.Count - 1);
  2352. objReader.Close();
  2353. arrText[1] = label_SerialNum.Text;
  2354. using (StreamWriter file = new StreamWriter(cfg_file))
  2355. {
  2356. foreach (var entry in arrText)
  2357. {
  2358. file.WriteLine(entry);
  2359. }
  2360. }
  2361. button_Start.Enabled = false;
  2362. //创建线程,执行测试任务
  2363. Thread th = new Thread(Thread_Task);
  2364. th.Start();
  2365. }
  2366. //1s计时
  2367. private void timer2_Tick(object sender, EventArgs e)
  2368. {
  2369. label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  2370. //MC故障信息超时清除
  2371. if (MC_ErrorCode_Refresh == false)
  2372. {
  2373. MC_ErrorCode_Refresh_Cnt++;
  2374. if (MC_ErrorCode_Refresh_Cnt > 5)
  2375. {
  2376. label_error_code.Text = "OK";
  2377. label_error_code.BackColor = Color.Green;
  2378. label_ErrorInfo.Text = "无故障";
  2379. label_ErrorInfo.ForeColor = Color.Green;
  2380. MC_ErrorCode_Refresh_Cnt = 0;
  2381. }
  2382. }
  2383. else
  2384. {
  2385. MC_ErrorCode_Refresh_Cnt = 0;
  2386. }
  2387. MC_ErrorCode_Refresh = false;
  2388. //MC_RunInfo超时清除
  2389. if (MC_RunInfo_Refresh == false)
  2390. {
  2391. MC_RunInfo_Refresh_Cnt++;
  2392. if (MC_RunInfo_Refresh_Cnt > 5)
  2393. {
  2394. MC_RunInfo_Clear();
  2395. MC_RunInfo_Refresh_Cnt = 0;
  2396. }
  2397. }
  2398. else
  2399. {
  2400. MC_RunInfo_Refresh_Cnt = 0;
  2401. }
  2402. MC_RunInfo_Refresh = false;
  2403. //等待反馈指令
  2404. if (Ack_Flag == false)
  2405. {
  2406. Ack_Cnt++;
  2407. if (Ack_Cnt > 2)
  2408. {
  2409. Ack_Flag = true;
  2410. StepTestFlag = StepTestResult_Enum.StepTestResult_Fail;
  2411. }
  2412. }
  2413. //推行测试计时
  2414. if ((TestStep == TestStep_Enum.Step_Walk) || (TestStep == TestStep_Enum.Step_GasMode))
  2415. {
  2416. WalkProcess_Cnt++;
  2417. }
  2418. }
  2419. //打开系统开关
  2420. private void 开ToolStripMenuItem_Click(object sender, EventArgs e)
  2421. {
  2422. byte[] Data = new byte[1];
  2423. if (portsettings_form.serialPort1.IsOpen)
  2424. {
  2425. //开机
  2426. Data[0] = 0xF1;
  2427. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2428. 开ToolStripMenuItem.Checked = true;
  2429. 关ToolStripMenuItem.Checked = false;
  2430. }
  2431. else
  2432. {
  2433. MessageBox.Show("串口未连接");
  2434. return;
  2435. }
  2436. }
  2437. //关闭系统开关
  2438. private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
  2439. {
  2440. byte[] Data = new byte[1];
  2441. if (portsettings_form.serialPort1.IsOpen)
  2442. {
  2443. //关机
  2444. Data[0] = 0xF0;
  2445. SendCmd(0x7FF, 0x16, 0x2201, Data);
  2446. 开ToolStripMenuItem.Checked = false;
  2447. 关ToolStripMenuItem.Checked = true;
  2448. }
  2449. else
  2450. {
  2451. MessageBox.Show("串口未连接");
  2452. return;
  2453. }
  2454. }
  2455. //清除流水号
  2456. private void label8_DoubleClick(object sender, EventArgs e)
  2457. {
  2458. //流水号清零
  2459. if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  2460. {
  2461. label_SerialNum.Text = "0";
  2462. //存储
  2463. string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\SerialNum";
  2464. StreamReader objReader = new StreamReader(cfg_file);
  2465. string sLine = "";
  2466. ArrayList arrText = new ArrayList();//创建一个动态数组
  2467. while (sLine != null)
  2468. {
  2469. sLine = objReader.ReadLine();
  2470. arrText.Add(sLine);
  2471. }
  2472. arrText.RemoveAt(arrText.Count - 1);
  2473. objReader.Close();
  2474. arrText[1] = label_SerialNum.Text;
  2475. using (StreamWriter file = new StreamWriter(cfg_file))
  2476. {
  2477. foreach (var entry in arrText)
  2478. {
  2479. file.WriteLine(entry);
  2480. }
  2481. }
  2482. }
  2483. }
  2484. private void richTextBox_TestRecord_TextChanged(object sender, EventArgs e)
  2485. {
  2486. richTextBox_TestRecord.SelectionStart = richTextBox_TestRecord.Text.Length;
  2487. richTextBox_TestRecord.ScrollToCaret();
  2488. }
  2489. private void textBox_PDinfo_Click(object sender, EventArgs e)
  2490. {
  2491. textBox_PDinfo.SelectAll();
  2492. }
  2493. private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e)
  2494. {
  2495. if (e.KeyCode == Keys.Enter)
  2496. {
  2497. TestStart();
  2498. }
  2499. }
  2500. private void 屏幕键盘ToolStripMenuItem_Click(object sender, EventArgs e)
  2501. {
  2502. System.Diagnostics.Process.Start("osk.exe");
  2503. }
  2504. private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
  2505. {
  2506. 执行ToolStripMenuItem.Checked = true;
  2507. 不执行ToolStripMenuItem.Checked = false;
  2508. }
  2509. private void 不执行ToolStripMenuItem_Click(object sender, EventArgs e)
  2510. {
  2511. 执行ToolStripMenuItem.Checked = false;
  2512. 不执行ToolStripMenuItem.Checked = true;
  2513. }
  2514. private void 端口选择ToolStripMenuItem_Click(object sender, EventArgs e)
  2515. {
  2516. portsettings_form.Show();
  2517. }
  2518. }
  2519. }