using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; namespace MOTINOVA_Motor_Factory_Set { public partial class CheckForm : Form { #region 文件路径 public string LocalCfgFilePath = ""; public string LogSavePath = ""; public string LogSaveFileName = ""; #endregion #region Boot版本正确标志 bool IsBootVerOK = false; #endregion #region 端口设置界面 public static PortSettings portsettings_form = new PortSettings(); #endregion #region 力矩传感器判断标准 TorqueCheckParam TorqueCheck = new TorqueCheckParam(); #endregion #region 三个传感器校正系数 List K_array = new List(); #endregion #region 硬件版本检测判断值 string HW_Version_Check = ""; #endregion #region 串口缓存 public static List buffer = new List(4096); private byte[] binary_data_1 = new byte[1024]; #endregion #region MC故障码超时计数 bool MC_ErrorCode_Refresh = false; ushort MC_ErrorCode_Refresh_Cnt = 0; #endregion #region 配置文件信息 ArrayList array_CfgInfo = new ArrayList(); #endregion #region CRC32校验 static UInt32[] crc32_table = new UInt32[256] { 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD, 0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75, 0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A, 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039, 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58, 0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D, 0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95, 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0, 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072, 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA, 0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA, 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC, 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A, 0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2, 0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34, 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB, 0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53, 0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C, 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF, 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E, 0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B, 0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3, 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71, 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3, 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C, 0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E, 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC, 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A, 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C, 0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4, 0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662, 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4 }; static UInt32 crc32_cal(byte[] pData, UInt16 Length) { UInt32 nReg; UInt32 nTemp = 0; UInt16 i, n; nReg = 0xFFFFFFFF; for (n = 0; n < Length; n++) { nReg ^= (UInt32)pData[n]; for (i = 0; i < 4; i++) { nTemp = crc32_table[((nReg >> 24) & 0xFF)]; nReg <<= 8; nReg ^= nTemp; } } return nReg; } #endregion #region 命令发送 private void SendCmd(ushort ID, byte Mode, ushort Cmd, byte[] Data) { if (!portsettings_form.serialPort1.IsOpen) { MessageBox.Show("串口未连接"); return; } ushort DataLength = (ushort)(Cmd & 0xFF); var SendCmdTmp = new byte[DataLength + 13]; //帧头 SendCmdTmp[0] = 0x55; SendCmdTmp[1] = 0xAA; //ID SendCmdTmp[2] = (byte)(ID >> 8); SendCmdTmp[3] = (byte)(ID & 0xFF); //MODE SendCmdTmp[4] = Mode; //长度 SendCmdTmp[5] = (byte)((Cmd & 0xFF) + 2); //命令字 SendCmdTmp[6] = (byte)(Cmd >> 8); SendCmdTmp[7] = (byte)(Cmd & 0xFF); //数据段 for (UInt16 i = 0; i < (Cmd & 0xFF); i++) { SendCmdTmp[8 + i] = Data[i]; } //校验 UInt32 CRC_Result = crc32_cal(SendCmdTmp, (ushort)(DataLength + 8)); SendCmdTmp[DataLength + 8] = Convert.ToByte(CRC_Result >> 24); SendCmdTmp[DataLength + 9] = Convert.ToByte((CRC_Result >> 16) % 256); SendCmdTmp[DataLength + 10] = Convert.ToByte((CRC_Result >> 8) % 256); SendCmdTmp[DataLength + 11] = Convert.ToByte(CRC_Result % 256); //帧尾 SendCmdTmp[DataLength + 12] = 0xF0; //发送数据 portsettings_form.serialPort1.Write(SendCmdTmp, 0, SendCmdTmp.Length); } #endregion #region 串口数据解析 private void Serial_DataReceived_Process() { //串口解析 byte DataNum = 0; //记录每条命令数据段的长度 while (buffer.Count >= 13)//当buffer中的数据的个数大于等于4个时,即每一帧的数据长度所在位(第4位)出现时 { if (buffer[0] == 0x55 && buffer[1] == 0xAA)//判断帧头 { ushort PackageID; PackageID = (ushort)(buffer[2] * 256 + buffer[3]); if ((PackageID == 0x0710) || (PackageID == 0x0715) || (PackageID == 0x0610) || (PackageID == 0x0615) || (PackageID == 0x07FF)) { int CmdLen = buffer[5]; if (buffer.Count < CmdLen + 11) break;//如果接收到的数据没有达到一帧数据的指定长度, 则执行下次循环 buffer.RemoveRange(0, 6);//数据达到要求长度后删去帧头,ID,模式,和命令长度 DataNum = (byte)(buffer[1]);//命令字的第二个字节的表示数据位的长度 if (DataNum == (CmdLen - 2)) { for (int i = 0; i < DataNum + 2; i++) { binary_data_1[i] = buffer[i]; } DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理 } buffer.RemoveRange(0, CmdLen + 5);//解析完成 } else { buffer.RemoveAt(0);//ID不对 } } else { buffer.RemoveAt(0);//帧头不对,删除帧头高字节 } } } #endregion #region 故障转换和显示 List ErrorInfo = new List{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障", "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","", "",""}; private string ErrorInfoDisplay(UInt32 Code) { string Result = ""; if (Code == 0) { Result = "无故障"; } else { for (int i = 0; i < 32; i++) { if ((Code & 0x01) == 0x01) { Result += ErrorInfo[i] + " "; } Code >>= 1; } } return Result; } #endregion #region 数据解析处理 private void DataCmdProcess(ushort ID) { ushort CmdTemp = (ushort)(binary_data_1[0] * 256 + binary_data_1[1]); ushort DataTemp; #region 主控信息 if ((ID == 0x715) || (ID == 0x710)) { switch (CmdTemp) { case 0xC109://Boot 版本 { unchecked { this.Invoke((EventHandler)(delegate { if (IsBootVerOK == false)//开机时一般会发送4条,只显示1条 { richTextBox_TestRecord.AppendText("Bootloader版本:"); string boot_Version = ""; for (ushort i = 0; i < 9; i++) { boot_Version += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_TestRecord.AppendText(boot_Version+" "); richTextBox_TestRecord.AppendText(boot_Version.Contains("V1") ? "125K" : "250K"); richTextBox_TestRecord.AppendText("\r\n"); } IsBootVerOK = true; })); } break; } case 0x1104://故障码 { unchecked { this.Invoke((EventHandler)(delegate { MC_ErrorCode_Refresh = true; label_error_code.Text = Convert.ToString(binary_data_1[5], 16).PadLeft(2, '0').ToUpper() + Convert.ToString(binary_data_1[4], 16).PadLeft(2, '0').ToUpper() + Convert.ToString(binary_data_1[3], 16).PadLeft(2, '0').ToUpper() + Convert.ToString(binary_data_1[2], 16).PadLeft(2, '0').ToUpper() + "H"; label_error_code.BackColor = Color.Red; })); } break; } case 0x1240://电机版本信息 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_VerInfo.Text = ""; //电机型号 richTextBox_VerInfo.Text += "型号:"; for (ushort i = 0; i < 16; i++) { if (binary_data_1[2 + i] == 0x2E) { break; } richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_VerInfo.Text += "\r\n"; //电机SN richTextBox_VerInfo.Text += "序列号:"; for (ushort i = 0; i < 16; i++) { if (binary_data_1[18 + i] == 0x2E) { break; } richTextBox_VerInfo.Text += ((char)binary_data_1[18 + i]).ToString(); } richTextBox_VerInfo.Text += "\r\n"; //电机HW richTextBox_VerInfo.Text += "硬件版本:"; for (ushort i = 0; i < 10; i++)//前10位是PCB版号,以'.'结束 { if (binary_data_1[34 + i] == 0x2E) { break; } if (binary_data_1[34 + i] == 'r') { binary_data_1[34 + i] = (byte)'.'; } richTextBox_VerInfo.Text += ((char)binary_data_1[34 + i]).ToString(); } switch (binary_data_1[34 + 10])//第10位代表MCU品牌 { case (byte)'1': richTextBox_VerInfo.Text += " " + "ST" + " "; break; case (byte)'2': richTextBox_VerInfo.Text += " " + "APM" + " "; break; case (byte)'3': richTextBox_VerInfo.Text += " " + "HK" + " "; break; case (byte)'4': richTextBox_VerInfo.Text += " " + "GD" + " "; break; default: richTextBox_VerInfo.Text += " " + "NULL" + " "; break; } for (ushort i = 0; i < 4; i++)//后4位是MCU的ID校验码,用于识别板子的ID { richTextBox_VerInfo.Text += Convert.ToString(binary_data_1[46 + i], 16).PadLeft(2, '0').ToUpper();//MCU ID 的CRC32值 } richTextBox_VerInfo.Text += "\r\n"; //电机FW richTextBox_VerInfo.Text += "软件版本:"; for (ushort i = 0; i < 16; i++) { if (binary_data_1[50 + i] == 0x2E) { break; } if (binary_data_1[50 + i] == 'r') { binary_data_1[50 + i] = (byte)'.'; } richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString(); } richTextBox_VerInfo.Text += "\r\n"; })); } break; } case 0x1720://电机程序特性信息 { unchecked { this.Invoke((EventHandler)(delegate { //程序特性 richTextBox_VerInfo.Text += "软件属性:"; for (ushort i = 0; i < 32; i++) { if (binary_data_1[2 + i] == 0x2E) { break; } if (binary_data_1[2 + i] == 'r') { binary_data_1[2 + i] = (byte)'.'; } richTextBox_VerInfo.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_VerInfo.Text += "\r\n"; })); } break; } case 0xA020://用户参数1 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "[用户参数1]" + "\r\n"; richTextBox_ReadCfg.Text += "启动模式," + binary_data_1[3].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "停机时间," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "限速值," + binary_data_1[6].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "下降速度," + binary_data_1[7].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "前飞," + binary_data_1[8].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "后飞," + binary_data_1[9].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "限流," + binary_data_1[10].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "温度预警," + (binary_data_1[11] - 40).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "温度保护," + (binary_data_1[12] - 40).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "码表," + binary_data_1[13].ToString()+","; richTextBox_ReadCfg.Text += ((binary_data_1[13] == 0x55) ? "有" : "无") + "\r\n"; richTextBox_ReadCfg.Text += "轮胎周长," + binary_data_1[14].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "系列号," + binary_data_1[15].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "ECO增益," + binary_data_1[16].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "ECO加速," + binary_data_1[17].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "NORM增益," + binary_data_1[18].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "NORM加速," + binary_data_1[19].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "SPORT增益," + binary_data_1[20].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "SPORT加速," + binary_data_1[21].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "TURBO增益," + binary_data_1[22].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "TURBO加速," + binary_data_1[23].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "SMART增益," + binary_data_1[24].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "SMART加速," + binary_data_1[25].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "车速级数," + binary_data_1[26].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "踏频启动," + binary_data_1[27].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "指拨模式," + binary_data_1[2].ToString() + ","; richTextBox_ReadCfg.Text += ((binary_data_1[2] == 0x55) ? "不支持" : "支持") + "\r\n"; richTextBox_ReadCfg.Text += "速度信号来源," + binary_data_1[28].ToString() + ","; richTextBox_ReadCfg.Text += ((binary_data_1[28] == 0x55) ? "传感器" : (binary_data_1[28] == 0xAA) ? "踏频" : (binary_data_1[28] == 0xEE) ? "通信" : "") + "\r\n"; richTextBox_ReadCfg.Text += "周长微调," + ((int)binary_data_1[29]).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "低压保护," + ((ushort)(binary_data_1[31] * 256 + binary_data_1[30])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "推行限速," + binary_data_1[32].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "推行转速," + binary_data_1[33].ToString() + "\r\n"; })); } break; } case 0xA110://马达参数 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "\r\n"; richTextBox_ReadCfg.Text += "[马达参数]" + "\r\n"; richTextBox_ReadCfg.Text += "额定功率," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "额定转速," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "定子电阻," + ((ushort)(binary_data_1[7] * 256 + binary_data_1[6])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "定子Lq," + ((ushort)(binary_data_1[9] * 256 + binary_data_1[8])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "定子Ld," + ((ushort)(binary_data_1[11] * 256 + binary_data_1[10])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "反电动势," + ((ushort)(binary_data_1[13] * 256 + binary_data_1[12])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "额定电压," + binary_data_1[14].ToString() + "\r\n"; })); } break; } case 0xA318://单传感器零点值 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_TQ.Text += "零点:"; for (int i = 0; i < 12; i++) { richTextBox_TQ.Text += ((ushort)((binary_data_1[3 + 2 * i] << 8) + binary_data_1[2 + 2 * i])).ToString() + ", "; } richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n"; })); } break; } case 0xA408://校验秘钥 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "校验密钥,"; for (int i = 0; i < 8; i++) { richTextBox_ReadCfg.Text += ((char)binary_data_1[2+i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; })); } break; } case 0xA520://生产信息 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "\r\n"; richTextBox_ReadCfg.Text += "[其它信息]" + "\r\n"; richTextBox_ReadCfg.Text += "生产商,"; for (int i = 0; i < 8; i++) { if (binary_data_1[2 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; richTextBox_ReadCfg.Text += "生产地,"; for (int i = 0; i < 8; i++) { if (binary_data_1[10 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[10 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; richTextBox_ReadCfg.Text += "生产日期,"; for (int i = 0; i < 8; i++) { if (binary_data_1[18 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[18 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; })); } break; } case 0xA610://自定义1 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "信息1,"; for (int i = 0; i < 16; i++) { if (binary_data_1[2 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; })); } break; } case 0xA710://自定义2 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "信息2,"; for (int i = 0; i < 16; i++) { if (binary_data_1[2 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; })); } break; } case 0xA810://自定义3 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "信息3,"; for (int i = 0; i < 16; i++) { if (binary_data_1[2 + i] == '.') break; richTextBox_ReadCfg.Text += ((char)binary_data_1[2 + i]).ToString(); } richTextBox_ReadCfg.Text += "\r\n"; })); } break; } case 0xA903://写入反馈 { break; } case 0xAA04://单传感器校正值 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_TQ.Text += "校正系数:"; richTextBox_TQ.Text += ((float)binary_data_1[4] / 100f).ToString("#0.00") + "\r\n"; richTextBox_TQ.Text += "启动值:"; richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n"; })); } break; } case 0xAD20://用户参数2 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_ReadCfg.Text += "\r\n"; richTextBox_ReadCfg.Text += "[用户参数2]" + "\r\n"; richTextBox_ReadCfg.Text+="姿态传感器," + binary_data_1[6].ToString() + ","; richTextBox_ReadCfg.Text += ((binary_data_1[6] == 0x55) ? "不支持" : "支持") + "\r\n"; richTextBox_ReadCfg.Text += "俯仰角零偏," + ((ushort)(binary_data_1[3] * 256 + binary_data_1[2])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "横滚角零偏," + ((ushort)(binary_data_1[5] * 256 + binary_data_1[4])).ToString() + "\r\n"; richTextBox_ReadCfg.Text += "尾灯模式," + binary_data_1[7].ToString() + "\r\n"; richTextBox_ReadCfg.Text += "前灯电压," + binary_data_1[8].ToString() + ","; richTextBox_ReadCfg.Text += ((binary_data_1[8] == 0xFF) ? "随电池" : (binary_data_1[8] == 0) ? "硬件配置" : (binary_data_1[8].ToString() + "V")) + "\r\n"; richTextBox_ReadCfg.Text += "尾灯电压," + binary_data_1[9].ToString() + ","; richTextBox_ReadCfg.Text += ((binary_data_1[9] == 0xFF) ? "随电池" : (binary_data_1[9] == 0) ? "硬件配置" : (binary_data_1[9].ToString() + "V")) + "\r\n"; })); } break; } case 0xAE1A://三传感器零点 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "零点:"; for (int i = 0; i < 12; i++) { richTextBox_TQ.Text += ((ushort)((binary_data_1[4 + 2 * i] << 8) + binary_data_1[3 + 2 * i])).ToString() + ", "; } richTextBox_TQ.Text = richTextBox_TQ.Text.Substring(0, richTextBox_TQ.Text.Length - 2) + "\r\n"; })); } break; } case 0xAF02://三传感器校正系数 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_TQ.Text += "传感器" + binary_data_1[2].ToString() + "校正系数:"; richTextBox_TQ.Text += ((float)binary_data_1[3] / 100f).ToString("#0.00") + "\r\n"; K_array.Add((float)binary_data_1[3] / 100f); if (K_array.Count >= 3) { double avg = (K_array[0] + K_array[1] + K_array[2]) / 3.0f; double sum = Math.Pow(K_array[0] - avg, 2) + Math.Pow(K_array[1] - avg, 2) + Math.Pow(K_array[2] - avg, 2); double std = Math.Sqrt(sum / 3.0f); richTextBox_TQ.Text += "校正系数标准差:" + std.ToString("#0.0000") + "\r\n"; K_array.Clear(); } })); } break; } case 0xB002://三传感器启动值 { unchecked { this.Invoke((EventHandler)(delegate { richTextBox_TQ.Text += "启动值:"; richTextBox_TQ.Text += ((ushort)((binary_data_1[3] << 8) + binary_data_1[2])).ToString() + "\r\n"; })); } break; } default: break; } } #endregion #region TE信息 else if ((ID == 0x615) || (ID == 0x610)) { switch (CmdTemp) { case 0x1240://TE APP版本信息 { unchecked { this.Invoke((EventHandler)(delegate { //TE 软件版本 richTextBox_VerInfo.Text += "TE软件版本:"; for (ushort i = 0; i < 16; i++) { if (binary_data_1[50 + i] == 0x2E) { break; } if (binary_data_1[50 + i] == 'r') { binary_data_1[50 + i] = (byte)'.'; } richTextBox_VerInfo.Text += ((char)binary_data_1[50 + i]).ToString(); } })); } break; } default: break; } #endregion } } #endregion #region 20ms定时器 private void timer1_Tick(object sender, EventArgs e) { //串口数据解析 Serial_DataReceived_Process(); } #endregion #region 非独占性延时函数 public static void Delay_ms(int milliSecond) { int start = Environment.TickCount; while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒 { Application.DoEvents(); } } #endregion #region 获取配置文件日期 private void GetDateList(bool DisAll) { string[] DateList = { "" }; if (checkBox_LocalFile.Checked == true)//从本地更新日期列表 { DateList = System.IO.Directory.GetDirectories(LocalCfgFilePath); } else//从服务器更新日期列表 { DateList = StartForm.myFtp.GetDirectoryList(StartForm.myServerCfg.RootPath + "/cfg"); } comboBox_cfgFileDate.Items.Clear(); //日期列表加载 if (DisAll == true) { comboBox_cfgFileDate.Items.Add("显示近期"); comboBox_cfgFileDate.Items.Add("---"); foreach (var file in DateList) comboBox_cfgFileDate.Items.Add(file.Substring(file.LastIndexOf("\\") + 1)); } else { if (DateList.Length > 10) { comboBox_cfgFileDate.Items.Add("显示全部"); comboBox_cfgFileDate.Items.Add("---"); } for (int i = (DateList.Length > 10) ? (DateList.Length - 10) : 0; i < DateList.Length; i++)//显示最近10个日期 comboBox_cfgFileDate.Items.Add(DateList[i].Substring(DateList[i].LastIndexOf("\\") + 1)); } } #endregion #region 获取配置文件列表 private void GetFileListFrom(string date) { string[] FileList = { "" }; if (checkBox_LocalFile.Checked == true)//从本地更新文件列表 { FileList = System.IO.Directory.GetFiles(LocalCfgFilePath + date); } else//从服务器更新文件列表 { FileList = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/cfg/" + date); } comboBox_File.Items.Clear(); foreach (var file in FileList) { if (file.Contains(".ttcfg")) { comboBox_File.Items.Add(file.Substring(file.LastIndexOf("\\") + 1)); } } } #endregion #region 选择文件日期后更新文件列表 private void comboBox_cfgFileDate_SelectionChangeCommitted(object sender, EventArgs e) { if (comboBox_cfgFileDate.SelectedItem.ToString() == "显示全部") { GetDateList(true); } else if (comboBox_cfgFileDate.SelectedItem.ToString() == "显示近期") { GetDateList(false); } else { comboBox_File.Text = ""; GetFileListFrom(comboBox_cfgFileDate.SelectedItem.ToString()); } } #endregion #region 选择文件后加载文件 private void comboBox_File_SelectionChangeCommitted(object sender, EventArgs e) { //清空原来数据 array_CfgInfo.Clear(); string SelectFile = ""; if (checkBox_LocalFile.Checked == false)//从服务器下载文件 { SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString(); if (File.Exists(LocalCfgFilePath + SelectFile))//本地存在选定文件,先删除后再下载 { File.Delete(LocalCfgFilePath + SelectFile); } SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "/" + comboBox_File.SelectedItem.ToString(); StartForm.myFtp.DownloadFile(StartForm.myServerCfg.RootPath + "/cfg/" + SelectFile, LocalCfgFilePath + comboBox_cfgFileDate.SelectedItem.ToString()); } SelectFile = comboBox_cfgFileDate.SelectedItem.ToString() + "\\" + comboBox_File.SelectedItem.ToString(); //打开选中文件 StreamReader objReader = new StreamReader(LocalCfgFilePath + SelectFile); string sLine = ""; while (sLine != null) { sLine = objReader.ReadLine(); array_CfgInfo.Add(sLine); } objReader.Close(); //解析显示配置文件 richTextBox_CfgFile.Text = ""; try { foreach (string sOutput in array_CfgInfo) { if (sOutput.Contains("[测试阈值]")) { //删除最后一行,不显示后面内容 richTextBox_CfgFile.Text = richTextBox_CfgFile.Text.Remove(richTextBox_CfgFile.Text.Length - 1); break; } richTextBox_CfgFile.AppendText(sOutput + "\r\n"); } } catch (System.Exception) { return; } //加载力矩传感器判断标准 TorqueCheck.CheckParam_Load(LocalCfgFilePath + SelectFile); textBox_TQ_Zero.Text = TorqueCheck.Zero_Min.ToString() + " - " + TorqueCheck.Zero_Max.ToString(); textBox_TQ_K.Text = TorqueCheck.Cal_K_Min.ToString("#0.00") + " - " + TorqueCheck.Cal_K_Max.ToString("#0.00"); textBox_TQ_Start.Text = TorqueCheck.Start_Min.ToString() + " - " + TorqueCheck.Start_Max.ToString(); //加载PCBA检测值 try { for (int i = 0; i < array_CfgInfo.Count; i++) { if (array_CfgInfo[i].ToString().Contains("[其它测试备注信息]")) { HW_Version_Check = array_CfgInfo[i + 1].ToString().Split(',')[1]; break; } } } catch (System.Exception) { return; } } #endregion #region 修改获取文件方式,本地或服务器 private void checkBox_LocalFile_CheckedChanged(object sender, EventArgs e) { comboBox_cfgFileDate.Text = ""; comboBox_File.Text = ""; GetDateList(false); } #endregion public CheckForm() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } #region 端口设置 private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e) { portsettings_form.Show(); } #endregion #region 打开电源 private void 开ToolStripMenuItem_Click(object sender, EventArgs e) { byte[] Data = new byte[1]; if (portsettings_form.serialPort1.IsOpen) { //开机 Data[0] = 0xF1; SendCmd(0x7FF, 0x16, 0x2201, Data); 开ToolStripMenuItem.Checked = true; 关ToolStripMenuItem.Checked = false; } else { MessageBox.Show("串口未连接"); return; } } #endregion #region 关闭电源 private void 关ToolStripMenuItem_Click(object sender, EventArgs e) { byte[] Data = new byte[1]; if (portsettings_form.serialPort1.IsOpen) { //关机 Data[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Data); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; } else { MessageBox.Show("串口未连接"); return; } } #endregion #region 1s定时器更新时间 private void timer2_Tick(object sender, EventArgs e) { label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //MC故障信息超时清除 if (MC_ErrorCode_Refresh == false) { MC_ErrorCode_Refresh_Cnt++; if (MC_ErrorCode_Refresh_Cnt > 5) { label_error_code.Text = "OK"; label_error_code.BackColor = Color.Green; MC_ErrorCode_Refresh_Cnt = 0; } } else { MC_ErrorCode_Refresh_Cnt = 0; } MC_ErrorCode_Refresh = false; } #endregion #region 故障码标签鼠标滑过显示故障内容 private void label_error_code_MouseHover(object sender, EventArgs e) { // 创建the ToolTip ToolTip toolTip1 = new ToolTip(); // 设置显示样式 toolTip1.AutoPopDelay = 30000;//提示信息的可见时间 toolTip1.InitialDelay = 50;//事件触发多久后出现提示 toolTip1.ReshowDelay = 50;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 toolTip1.ShowAlways = true;//是否显示提示框 // 设置伴随的对象. string ErrorCode = this.label_error_code.Text; UInt32 Code = ErrorCode.Contains("H") ? Convert.ToUInt32(ErrorCode.Split('H')[0], 16) : 0; toolTip1.SetToolTip(this.label_error_code, ErrorInfoDisplay(Code));//设置故障内容显示 } #endregion //创建线程执行一次测试任务 /* 执行流程如下: 1、读取BOOT版本信息; 2、读取电机版本信息 -> 确认SN是否存在为0、确认版本和cfg文件是否一致; 3、读取力矩传感器标定值 -> 依次判断零点、校正系数、启动值是否在允许范围内,判断零点值差异是否过大; 4、依次读取配置参数,严格按照cfg文件的顺序读取 -> 对比两个richbox内容是否一致; 5、显示测试结果; 6、存储测试结果,上传ftp服务器。 */ private void Thread_Task() { //开始测试 richTextBox_TestRecord.Text += "电机信息:" + textBox_PDinfo.Text + "\r\n"; richTextBox_TestRecord.Text += "配置文件:" + comboBox_File.Text + "\r\n"; richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "开始测试" + "\r\n"; label_Result.Text = "不合格"; label_Result.BackColor = Color.Red; //公有变量 var Code = new byte[32];//发送的指令数据 //检查是否有故障 if (label_error_code.Text != "OK") { label_Step.Text = "电机存在故障!"; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); //存储测试数据 ErrorSaveLog(); return; } //开机 Code[0] = 0xF1; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = true; 关ToolStripMenuItem.Checked = false; Delay_ms(2000); #region 判断BootLoader版本 if (IsBootVerOK == false) { //发送系统复位指令 Code[0] = (byte)'R'; Code[1] = (byte)'E'; Code[2] = (byte)'S'; Code[3] = (byte)'E'; Code[4] = (byte)'T'; SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2505, Code); Delay_ms(2000); if (IsBootVerOK == false) { label_Step.Text = "Bootloader波特率错误!"; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); //存储测试数据 ErrorSaveLog(); return; } } #endregion #region 版本查询和确认 label_Step.Text = "检查版本信息"; Delay_ms(2000); richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询版本信息" + "\r\n"; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null); Delay_ms(500); SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null); Delay_ms(500); string FW_Version_Check = comboBox_File.Text.Split('_')[2] + "_" + comboBox_File.Text.Split('_')[3]; string FW_Version_Result = ""; string TE_FW_Ver_Check = comboBox_File.Text.Split('_')[4] + "_" + comboBox_File.Text.Split('_')[5]; string TE_FW_Ver_Result = ""; string HW_Version_Result = ""; try { HW_Version_Result = richTextBox_VerInfo.Lines[2].Split(':')[1]; FW_Version_Result = richTextBox_VerInfo.Lines[3].Split(':')[1]; FW_Version_Result = FW_Version_Result.Split('_')[0] + richTextBox_VerInfo.Lines[4].Split('-')[2].Replace('V', '.') + "_" + FW_Version_Result.Split('_')[1]; TE_FW_Ver_Result = richTextBox_VerInfo.Lines[5].Split(':')[1]; } catch (System.Exception) { //label_Step.Text = "软件版本信息不完整!"; } //PCBA版本确认 if (HW_Version_Result.Contains(HW_Version_Check) == false)//PCBA版号错误,直接结束 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "PCBA版本错误" + "\r\n"; label_Step.Text = "PCBA版本错误,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } //主控软件版本检查 if (string.Equals(FW_Version_Check, FW_Version_Result) == false) //主控版本错误,直接结束 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "主控版本错误" + "\r\n"; label_Step.Text = "主控软件版本错误,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } else { //TE软件版本检查 if (TE_FW_Ver_Check != "N_A")//单MCU主控板不检测TE版本 { if (string.Equals(TE_FW_Ver_Check, TE_FW_Ver_Result) == false) //TE版本错误,给出提示 { if (MessageBox.Show("TE版本错误,请确认主控板是否为HK?", "TE版本错误", MessageBoxButtons.YesNo) == DialogResult.No) { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "TE版本错误" + "\r\n"; label_Step.Text = "TE软件版本错误,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } } } //型号和SN确认 try { string mode1 = "", mode2 = ""; mode1 = richTextBox_VerInfo.Lines[0].Split(':')[1].Trim(); mode2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf('%')) : textBox_PDinfo.Text.Substring(0, textBox_PDinfo.Text.LastIndexOf(' ')).Trim(); if (string.Equals(mode1, mode2) == false)//型号不一致 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "型号错误" + "\r\n"; label_Step.Text = "型号不一致,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } string SN1 = "", SN2 = ""; SN1 = richTextBox_VerInfo.Lines[1].Split(':')[1].Trim(); SN2 = textBox_PDinfo.Text.Contains("%#") ? textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf('#') + 1) : textBox_PDinfo.Text.Remove(0, textBox_PDinfo.Text.LastIndexOf(' ') + 1); if (string.Equals(SN1, SN2) == false)//SN不一致 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "SN错误" + "\r\n"; label_Step.Text = "SN不一致,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "版本信息正确" + "\r\n"; } catch (Exception) { label_Step.Text = "电机信息错误,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } } #endregion #region 判断力矩传感器的标定值是否在允许范围内 label_Step.Text = "判断力矩传感器"; richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询力矩传感器" + "\r\n"; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);//单传感器零点 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);//单传感器校正信息 Delay_ms(200); Code[0] = 1; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器1零点 Delay_ms(200); Code[0] = 2; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器2零点 Delay_ms(200); Code[0] = 3; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, Code);//传感器3零点 Delay_ms(200); Code[0] = 1; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器1校正系数 Delay_ms(200); Code[0] = 2; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器2校正系数 Delay_ms(200); Code[0] = 3; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, Code);//传感器3校正系数 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);//三传感器启动值 Delay_ms(200); richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断力矩传感器" + "\r\n"; for (int i = 0; i < richTextBox_TQ.Lines.Length; i++) { switch (richTextBox_TQ.Lines[i].Split(':')[0]) { case "零点": case "传感器1零点": case "传感器2零点": case "传感器3零点": { string[] zero = richTextBox_TQ.Lines[i].Split(':')[1].Split(new string[] { ", " }, StringSplitOptions.None); for (int j = 0; j < zero.Length; j++) { if ((Convert.ToUInt16(zero[j]) > TorqueCheck.Zero_Max) || (Convert.ToUInt16(zero[j]) < TorqueCheck.Zero_Min))//零点超差 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器零点超范围" + "\r\n"; label_Step.Text = "传感器零点超范围,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n"; break; } case "校正系数": case "传感器1校正系数": case "传感器2校正系数": case "传感器3校正系数": { ushort k = (ushort)(Convert.ToSingle(richTextBox_TQ.Lines[i].Split(':')[1]) * 100); if ((k > (ushort)(TorqueCheck.Cal_K_Max * 100f)) || (k < (ushort)(TorqueCheck.Cal_K_Min * 100f)))//校正系数超差 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数超范围" + "\r\n"; label_Step.Text = "校正系数超范围,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } if (k == 100) { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "传感器未标定" + "\r\n"; label_Step.Text = "传感器未标定,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n"; break; } case "校正系数标准差": { double std = Convert.ToDouble(richTextBox_TQ.Lines[i].Split(':')[1]); if (std > 0.3f) { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "校正系数差异大" + "\r\n"; label_Step.Text = "校正系数标准差超范围,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n"; break; } case "启动值": { string start = richTextBox_TQ.Lines[i].Split(':')[1]; if ((Convert.ToUInt16(start) > TorqueCheck.Start_Max) || (Convert.ToUInt16(start) < TorqueCheck.Start_Min))//启动值超差 { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "启动值超范围" + "\r\n"; label_Step.Text = "启动值超范围,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_TQ.Lines[i].Split(':')[0] + "正常" + "\r\n"; break; } default:break; } } #endregion #region 依次读取配置参数 label_Step.Text = "判断配置参数"; richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "发送指令查询电机配置参数" + "\r\n"; SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);//用户参数1 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);//用户参数2 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);//马达参数 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);//生产信息 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);//校验秘钥 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);//自定义1 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);//自定义2 Delay_ms(200); SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);//自定义3 Delay_ms(200); richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "判断配置参数" + "\r\n"; for (int line = 0; line < richTextBox_CfgFile.Lines.Length; line++) { try { if (richTextBox_ReadCfg.Lines[line].Equals(richTextBox_CfgFile.Lines[line]) == false) { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + richTextBox_CfgFile.Lines[line].Split(',')[0] + "错误" + "\r\n"; label_Step.Text = "检查配置参数失败,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } } catch (System.Exception) { richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "读取电机配置参数失败" + "\r\n"; label_Step.Text = "检查配置参数失败,测试结束!"; label_Step.ForeColor = Color.Red; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; ErrorSaveLog(); //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); return; } } richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机配置参数正确" + "\r\n"; #endregion //检查是否有故障 if (label_error_code.Text != "OK") { label_Step.Text = "电机存在故障!"; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); //存储测试数据 ErrorSaveLog(); return; } #region 测试结束 richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试结束" + "\r\n"; label_Step.Text = "测试结束!"; label_Step.ForeColor = Color.Green; label_Result.Text = "合格"; label_Result.BackColor = Color.Green; button_Start.Enabled = true; textBox_PDinfo.Enabled = true; //关机 Code[0] = 0xF0; SendCmd(0x7FF, 0x16, 0x2201, Code); 开ToolStripMenuItem.Checked = false; 关ToolStripMenuItem.Checked = true; Delay_ms(500); //关闭串口 portsettings_form.closePort(); //合格计数累加和存储 label_OK_Count.Text = (Convert.ToInt32(label_OK_Count.Text) + 1).ToString(); string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum"; StreamReader objReader = new StreamReader(cfg_file); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } arrText.RemoveAt(arrText.Count - 1); objReader.Close(); arrText[3] = label_OK_Count.Text; using (StreamWriter file = new StreamWriter(cfg_file)) { foreach (var entry in arrText) { file.WriteLine(entry); } } #region 按照产品信息设置测试文件路径 string ResultPathName = textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_"); if (System.IO.Directory.Exists(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName) == false) System.IO.Directory.CreateDirectory(LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName); #endregion //页面存储 Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片 LogSaveFileName = LogSavePath + "log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\测试记录" + ".png"; bit.Save(LogSaveFileName); //测试页面上传服务器 if (StartForm.myFtp.IsNetConnected == true) { string DateNow = DateTime.Now.ToString("yyyy-MM-dd"); if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false) { StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow); } if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow + "/" + ResultPathName) == false) { StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName); } bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName); if (result == true) richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试页面上传服务器成功" + "\r\n"; } //电机数据存储 LogSaveFileName = LogSavePath + "\\log\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\" + ResultPathName + "\\电机参数" + ".txt"; string fileInfo = ""; fileInfo += "检验记录:\r\n" + richTextBox_TestRecord.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "电机生产参数:\r\n"; fileInfo += richTextBox_CfgFile.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "电机版本信息查询:\r\n"; fileInfo += richTextBox_VerInfo.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "力矩传感器标定结果:\r\n"; fileInfo += richTextBox_TQ.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "电机参数查询:\r\n"; fileInfo += richTextBox_ReadCfg.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "电机故障码:\r\n"; fileInfo += label_error_code.Text.Replace("\n", "\r\n") + "\r\n\r\n"; fileInfo += "测试结果:\r\n"; fileInfo += label_Result.Text.Replace("\n", "\r\n") + "\r\n\r\n"; System.IO.File.WriteAllText(LogSaveFileName, fileInfo); //电机数据上传服务器 if (StartForm.myFtp.IsNetConnected == true) { string DateNow = DateTime.Now.ToString("yyyy-MM-dd"); if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/", DateNow) == false) { StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow); } if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow, ResultPathName) == false) { StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName); } bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/log/" + DateNow + "/" + ResultPathName); if (result == true) richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "电机数据上传服务器成功" + "\r\n"; } #endregion //测试正常,清空扫码信息 Delay_ms(1000); textBox_PDinfo.Text = ""; } private void CheckForm_Load(object sender, EventArgs e) { timer1.Enabled = true; timer2.Enabled = true; //显示测试员信息 label_TestUser.Text = "测试员:" + StartForm.EnterForm1.comboBox_User.Text; //窗口加载时,从FTP服务器或本地获取文件日期列表 GetDateList(false); //装载序列号文件 string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum"; StreamReader objReader = new StreamReader(cfg_file); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } objReader.Close(); label_SerialNum.Text = arrText[1].ToString();//流水号 label_OK_Count.Text = arrText[3].ToString();//合格计数 } //测试失败存储测试界面 private void ErrorSaveLog() { //页面存储 Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片 string LogSaveFileName = LogSavePath + "errorlog\\" + DateTime.Now.ToString("yyyy-MM-dd").Replace("/", "-") + "\\"; if (System.IO.Directory.Exists(LogSaveFileName) == false) System.IO.Directory.CreateDirectory(LogSaveFileName); LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png"; bit.Save(LogSaveFileName); //测试页面上传服务器 if (StartForm.myFtp.IsNetConnected == true) { string DateNow = DateTime.Now.ToString("yyyy-MM-dd"); if (StartForm.myFtp.DirectoryExist(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/", DateNow) == false) { StartForm.myFtp.MakeDir(StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow); } bool result = StartForm.myFtp.UploadFile(LogSaveFileName, StartForm.myServerCfg.RootPath + "/qcTest/errorlog/" + DateNow); if (result == true) richTextBox_TestRecord.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "->" + "测试异常页面上传服务器成功" + "\r\n"; } } //开始测试按钮点击 private void button_Start_Click(object sender, EventArgs e) { TestStart(); } //开始一次测试 private void TestStart() { //显示数据清空 richTextBox_ReadCfg.Text = ""; richTextBox_TestRecord.Text = ""; richTextBox_TQ.Text = ""; richTextBox_VerInfo.Text = ""; //Boot版本标志复位 IsBootVerOK = false; #region 检查配置文件 if (comboBox_File.Items.Count == 0) //配置文件目录为空 { label_Step.Text = "无可用配置文件!"; label_Step.ForeColor = Color.Red; return; } if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件 { label_Step.Text = "请选择配置文件!"; label_Step.ForeColor = Color.Red; return; } #endregion #region 检查产品信息是否符合格式要求 try { 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][A-Z]?$"); if (textBox_PDinfo.Text.Contains(" ")) { string Mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0]; string Sn = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[1]; if (regex.IsMatch(Sn) == false) { label_Step.Text = "SN格式错误!"; label_Step.ForeColor = Color.Red; return; } } else if (textBox_PDinfo.Text.Contains("%#")) { string Mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0]; string Sn = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[1]; if (regex.IsMatch(Sn) == false) { label_Step.Text = "SN格式错误!"; label_Step.ForeColor = Color.Red; return; } } else { label_Step.Text = "标签格式错误!"; label_Step.ForeColor = Color.Red; return; } } catch (System.Exception) { label_Step.Text = "标签格式错误!"; label_Step.ForeColor = Color.Red; return; } #endregion #region 检查产品型号和配置文件是否一致 do { string cfg_mode = "", pd_mode = ""; cfg_mode = comboBox_File.Text.Split('_')[0]; if (textBox_PDinfo.Text.Contains(" ")) { pd_mode = textBox_PDinfo.Text.Split(new string[] { " " }, StringSplitOptions.None)[0]; } else if (textBox_PDinfo.Text.Contains("%#")) { pd_mode = textBox_PDinfo.Text.Split(new string[] { "%#" }, StringSplitOptions.None)[0]; } if (pd_mode != cfg_mode) { label_Step.Text = "打标信息与配置文件不一致!"; label_Step.ForeColor = Color.Red; return; } } while (false); #endregion //连接串口 if (portsettings_form.openPort() == false) { label_Step.Text = "串口连接失败!"; label_Step.ForeColor = Color.Red; return; } //测试状态更新 label_Step.Text = "准备开始测试"; label_Step.ForeColor = Color.Red; //流水号累加和存储 label_SerialNum.Text = (Convert.ToInt32(label_SerialNum.Text) + 1).ToString(); string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum"; StreamReader objReader = new StreamReader(cfg_file); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } arrText.RemoveAt(arrText.Count - 1); objReader.Close(); arrText[1] = label_SerialNum.Text; using (StreamWriter file = new StreamWriter(cfg_file)) { foreach (var entry in arrText) { file.WriteLine(entry); } } button_Start.Enabled = false; textBox_PDinfo.Enabled = false; //创建线程,执行测试任务 Thread th = new Thread(Thread_Task); th.Start(); } //双击清零流水号 private void label32_DoubleClick(object sender, EventArgs e) { StartForm.EnterForm1.ShowDialog(); try { if (StartForm.EnterForm1.textBox_Passwd.Text == StartForm.EnterForm1.UserAccount[StartForm.EnterForm1.comboBox_User.Text]) { if (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer") { //流水号清零 if (MessageBox.Show("流水号清零", "确认清零?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { label_SerialNum.Text = "0"; label_OK_Count.Text = "0"; //存储 string cfg_file = System.IO.Directory.GetCurrentDirectory() + "\\CheckSerialNum"; StreamReader objReader = new StreamReader(cfg_file); string sLine = ""; ArrayList arrText = new ArrayList();//创建一个动态数组 while (sLine != null) { sLine = objReader.ReadLine(); arrText.Add(sLine); } arrText.RemoveAt(arrText.Count - 1); objReader.Close(); arrText[1] = label_SerialNum.Text; arrText[3] = label_OK_Count.Text; using (StreamWriter file = new StreamWriter(cfg_file)) { foreach (var entry in arrText) { file.WriteLine(entry); } } } } else { MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void textBox_PDinfo_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) TestStart(); } private void textBox_PDinfo_Click(object sender, EventArgs e) { textBox_PDinfo.SelectAll(); } private void 屏幕键盘ToolStripMenuItem_Click_1(object sender, EventArgs e) { System.Diagnostics.Process.Start("osk.exe"); } private void 端口设置ToolStripMenuItem_Click(object sender, EventArgs e) { portsettings_form.Show(); } private void 合格ToolStripMenuItem_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "log\\"); } private void 不合格ToolStripMenuItem_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("explorer.exe", LogSavePath + "errorlog\\"); } private void CheckForm_FormClosing(object sender, FormClosingEventArgs e) { portsettings_form.serialPort1.Close(); portsettings_form.comboBox_ComIndex.Enabled = true; portsettings_form.button_Enter.Text = "确认"; portsettings_form.g_blnIsOpen = false; #if false //针对工程和越南账户,直接退出,不执行删除本地配置 if ((StartForm.EnterForm1.comboBox_User.Text == "越南Vietnam") || (StartForm.EnterForm1.comboBox_User.Text == "工程Engineer")) { return; } //关闭前,删除本地所有配置文件 string path = StartForm.UserPath + "\\cfg\\"; try { DirectoryInfo dir = new DirectoryInfo(path); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } //MessageBox.Show("文件清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception) { MessageBox.Show("文件清理失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } #endif } } }