1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Collections;
- using System.Threading;
- namespace MOTINOVA_Motor_Factory_Set
- {
- public partial class ImportForm : Form
- {
- #region 文件路径
- string LogSaveFileName = "";
- string CfgFilePath = "";
- #endregion
- #region 推行测试过程比较值
- ushort MotorRadeSpeed = 1200;
- ushort RateVoltage = 36000;
- ushort No_load_Current = 800;
- ushort RateCadence = 100;
- #endregion
- #region 端口设置界面
- public static PortSettings portsettings_form = new PortSettings();
- #endregion
- #region 串口缓存
- public static List<byte> buffer = new List<byte>(4096);
- private byte[] binary_data_1 = new byte[1024];
- #endregion
- #region MC运行信息超时计数
- bool MC_RunInfo_Refresh = false;
- ushort MC_RunInfo_Refresh_Cnt = 0;
- #endregion
- #region MC故障码超时计数
- bool MC_ErrorCode_Refresh = false;
- ushort MC_ErrorCode_Refresh_Cnt = 0;
- #endregion
- #region 执行步骤
- public enum TestStep_Enum
- {
- Step_Init = 0,
- Step_SysClear = 1,
- Step_Write = 2,
- Step_Light = 3,
- Step_Walk = 4,
- Step_CheckVer = 5,
- Step_Save = 6
- }
- TestStep_Enum TestStep;
- #endregion
- #region 反馈指令判断计时
- bool Ack_Flag = true;
- ushort Ack_Cnt = 0;
- #endregion
- #region 参数写入步骤
- ushort WriteAckCount = 0;
- ushort WriteCmdNum = 10;
- bool Write_Flag = false;
- ushort Write_DelayTimeCnt = 0;
- #endregion
- public ImportForm()
- {
- InitializeComponent();
- }
- private void ImportForm_Load(object sender, EventArgs e)
- {
- //装载配置文件
- CfgFilePath = System.IO.Directory.GetCurrentDirectory() + "\\cfg\\";
- var files = Directory.GetFiles(CfgFilePath, "*.ttcfg");
- comboBox_File.Items.Clear();
- foreach (var file in files)
- {
- comboBox_File.Items.Add(Path.GetFileName(file));
- }
- }
- private void toolStripMenuIte_PortSet_Click(object sender, EventArgs e)
- {
- portsettings_form.Show();
- if (!portsettings_form.serialPort1.IsOpen)
- {
- groupBox2.Enabled = true;
- button_Start.Enabled = true;
- TestStep = TestStep_Enum.Step_Init;
- }
- else
- {
- groupBox2.Enabled = false;
- button_Start.Enabled = false;
- }
- }
- #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];
- }
- if (TestStep == TestStep_Enum.Step_Save)
- {
- //数据存储
- var RecData = new byte[CmdLen];
- Array.Copy(binary_data_1, RecData, CmdLen);
- DataSave(RecData);
- }
- DataCmdProcess(PackageID);//对于不同的命令段,做不同的处理
- }
- buffer.RemoveRange(0, CmdLen + 5);//解析完成
- }
- else
- {
- buffer.RemoveAt(0);//ID不对
- }
- }
- else
- {
- buffer.RemoveAt(0);//帧头不对,删除帧头高字节
- }
- }
- }
- #endregion
- #region 数据存储
- private void DataSave(byte[] Data)
- {
- string SaveData = "";
- //数据存储
- for (int i = 0; i < Data.Length; i++)
- {
- SaveData += Convert.ToString(Data[i], 16).PadLeft(2, '0').ToUpper() + " ";
- }
- SaveData += "\r\n";
- System.IO.File.AppendAllText(LogSaveFileName, SaveData);
- }
- #endregion
- List<string> ErrorInfo = new List<string>{ "过流保护", "低压保护", "过压保护", "堵转保护","过热保护","速度传感器故障","力矩传感器故障","霍尔故障",
- "马达缺相","NTC故障","","","","MCU故障","踏频传感器故障","指拨故障","MOS短路","电压异常","电路故障","TE故障","TE电路故障","","","","","","","","","",
- "",""};
- private void ErrorInfoDisplay(UInt32 Code)
- {
- label_ErrorInfo.Text = "";
- if (Code == 0)
- {
- label_ErrorInfo.Text = "无故障";
- label_ErrorInfo.ForeColor = Color.Green;
- }
- else
- {
- label_ErrorInfo.ForeColor = Color.Red;
- for (int i = 0; i < 32; i++)
- {
- if ((Code & 0x01) == 0x01)
- {
- label_ErrorInfo.Text += ErrorInfo[i] + " ";
- }
- Code >>= 1;
- }
- }
- }
- #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 0x1020://电机运行信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- MC_RunInfo_Refresh = true;
- //车速
- DataTemp = (ushort)(binary_data_1[3] * 256 + binary_data_1[2]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if (DataTemp > 250)
- {
- label_sensor_speed.BackColor = Color.Green;
- label_sensor_speed.Text = "OK";
- }
- }
- textBox_BikeSpeed.Text = ((float)DataTemp / 10f).ToString("0.0");
- //输出转速
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- DataTemp = (ushort)(binary_data_1[5] * 256 + binary_data_1[4]);
- if ((DataTemp > (MotorRadeSpeed - 200)) && (DataTemp < (MotorRadeSpeed + 200)))
- {
- label_sensor_motor.BackColor = Color.Green;
- label_sensor_motor.Text = "OK";
- }
- }
- textBox_MotorSpeed.Text = Convert.ToString(DataTemp);
-
- //母线电压
- DataTemp = (ushort)(binary_data_1[9] * 256 + binary_data_1[8]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if ((DataTemp > (RateVoltage - 1000)) && (DataTemp < (RateVoltage + 1000)))
- {
- label_sensor_vol.BackColor = Color.Green;
- label_sensor_vol.Text = "OK";
- }
- }
- textBox_Vol.Text = Convert.ToString(DataTemp);
- //母线电流
- DataTemp = (ushort)(binary_data_1[11] * 256 + binary_data_1[10]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if (DataTemp > No_load_Current)
- {
- label_sensor_cur.BackColor = Color.Green;
- label_sensor_cur.Text = "OK";
- }
- }
- textBox_Cur.Text = Convert.ToString(DataTemp);
- //踏频
- DataTemp = (ushort)(binary_data_1[12]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if ((DataTemp > (RateCadence - 20)) && (DataTemp < (RateCadence + 20)))
- {
- label_sensor_cadence.BackColor = Color.Green;
- label_sensor_cadence.Text = "OK";
- }
- }
- textBox_Cadence.Text = Convert.ToString(DataTemp);
- //踩踏力矩
- DataTemp = (ushort)(binary_data_1[13]);
- textBox_Torque.Text = Convert.ToString(DataTemp);
- //踩踏方向
- DataTemp = (ushort)(binary_data_1[14]);
- if (DataTemp == 0)
- {
- textBox_Dir.Text = "正向";
- }
- else if (DataTemp == 1)
- {
- textBox_Dir.Text = "反向";
- }
- else if (DataTemp == 2)
- {
- textBox_Dir.Text = "停止";
- }
- //PCB温度
- DataTemp = (ushort)(binary_data_1[23]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if (DataTemp > 45)
- {
- label_sensor_PCB_temp.BackColor = Color.Green;
- label_sensor_PCB_temp.Text = "OK";
- }
- }
- textBox_PCB.Text = Convert.ToString((int)(DataTemp - 40));
- //绕组温度
- DataTemp = (ushort)(binary_data_1[24]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if (DataTemp > 45)
- {
- label_sensor_coil_temp.BackColor = Color.Green;
- label_sensor_coil_temp.Text = "OK";
- }
- }
- textBox_Coil.Text = Convert.ToString((int)(DataTemp - 40));
- //MCU温度
- DataTemp = (ushort)(binary_data_1[25]);
- if (TestStep == TestStep_Enum.Step_Walk)
- {
- if (DataTemp > 45)
- {
- label_sensor_MCU_temp.BackColor = Color.Green;
- label_sensor_MCU_temp.Text = "OK";
- }
- }
- textBox_MCU.Text = Convert.ToString((int)(DataTemp - 40));
- }));
- }
- 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;
- UInt32 ErrorCode = (UInt32)((binary_data_1[5] << 24) + (binary_data_1[4] << 16) + (binary_data_1[3] << 8) + binary_data_1[2]);
- ErrorInfoDisplay(ErrorCode);
-
- }));
- }
- break;
- }
- case 0x1240://电机版本信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- richTextBox_Ver.Text = "";
- //电机型号
- richTextBox_Ver.Text += "型号:";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[2 + i] == 0x2E)
- {
- break;
- }
- richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
- }
- richTextBox_Ver.Text += "\r\n";
- //电机SN
- richTextBox_Ver.Text += "序列号:";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[18 + i] == 0x2E)
- {
- break;
- }
- richTextBox_Ver.Text += ((char)binary_data_1[18 + i]).ToString();
- }
- richTextBox_Ver.Text += "\r\n";
- //电机HW
- richTextBox_Ver.Text += "硬件版本:";
- for (ushort i = 0; i < 16; i++)
- {
- if (binary_data_1[34 + i] == 0x2E)
- {
- break;
- }
- if (binary_data_1[34 + i] == 'r')
- {
- binary_data_1[34 + i] = (byte)'.';
- }
- richTextBox_Ver.Text += ((char)binary_data_1[34 + i]).ToString();
- }
- richTextBox_Ver.Text += "\r\n";
- //电机FW
- richTextBox_Ver.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_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
- }
- richTextBox_Ver.Text += "\r\n";
- }));
- }
- break;
- }
- case 0x1720://电机程序特性信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //程序特性
- for (ushort i = 0; i < 32; i++)
- {
- if (binary_data_1[2 + i] == 0x2E)
- {
- break;
- }
- richTextBox_Ver.Text += ((char)binary_data_1[2 + i]).ToString();
- }
- richTextBox_Ver.Text += "\r\n";
- }));
- }
- break;
- }
- case 0xA903://反馈指令
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- Ack_Flag = true;
- switch (TestStep)
- {
- case TestStep_Enum.Step_SysClear:
- timer1.Enabled = false;
- MessageBox.Show("清除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- timer1.Enabled = true;
- //结果显示
- label_Sta_Clear.Text = "OK";
- label_Sta_Clear.BackColor = Color.Green;
- //产品信息输入框使能
- textBox_PDinfo.Enabled = true;
- //使能参数写入按钮
- button_Write.Enabled = true;
- break;
- case TestStep_Enum.Step_Write:
- WriteAckCount++;
- if (WriteAckCount >= WriteCmdNum) //一共会发送WriteCmdNum条写入指令
- {
- timer1.Enabled = false;
- MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- timer1.Enabled = true;
- button_Write.Enabled = true;
- button_Write.Text = "电机参数写入";
- Write_Flag = false;
- //结果显示
- label_Sta_Write.Text = "OK";
- label_Sta_Write.BackColor = Color.Green;
- //使能开灯测试按钮
- button_LightCtl.Enabled = true;
- }
- break;
- case TestStep_Enum.Step_Light:
- break;
- case TestStep_Enum.Step_Walk:
- break;
- case TestStep_Enum.Step_CheckVer:
- break;
- case TestStep_Enum.Step_Save:
- break;
- }
- }));
- }
- break;
- }
- default: break;
- }
- }
- #endregion
- else if ((ID == 0x615) || (ID == 0x610))
- {
- switch (CmdTemp)
- {
- case 0x1240://TE APP版本信息
- {
- unchecked
- {
- this.Invoke((EventHandler)(delegate
- {
- //TE 软件版本
- richTextBox_Ver.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_Ver.Text += ((char)binary_data_1[50 + i]).ToString();
- }
- }));
- }
- break;
- }
- default: break;
- }
- }
- }
- #endregion
- private void timer1_Tick(object sender, EventArgs e)
- {
- //串口数据解析
- Serial_DataReceived_Process();
- //MC故障信息超时清除
- if (MC_ErrorCode_Refresh == false)
- {
- MC_ErrorCode_Refresh_Cnt++;
- if (MC_ErrorCode_Refresh_Cnt > 50)
- {
- label_error_code.Text = "OK";
- label_error_code.BackColor = Color.Green;
- label_ErrorInfo.Text = "无故障";
- label_ErrorInfo.ForeColor = Color.Green;
- MC_ErrorCode_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_ErrorCode_Refresh_Cnt = 0;
- }
- MC_ErrorCode_Refresh = false;
- //MC_RunInfo超时清除
- if (MC_RunInfo_Refresh == false)
- {
- MC_RunInfo_Refresh_Cnt++;
- if (MC_RunInfo_Refresh_Cnt > 50)
- {
- MC_RunInfo_Clear();
- MC_RunInfo_Refresh_Cnt = 0;
- }
- }
- else
- {
- MC_RunInfo_Refresh_Cnt = 0;
- }
- MC_RunInfo_Refresh = false;
- //系统清除等待
- if (Ack_Flag == false)
- {
- Ack_Cnt++;
- if (Ack_Cnt > 100)
- {
- Ack_Flag = true;
- MessageBox.Show("指令操作失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- switch (TestStep)
- {
- case TestStep_Enum.Step_SysClear:
- label_Sta_Clear.Text = "NG";
- label_Sta_Clear.BackColor = Color.Red;
- break;
- case TestStep_Enum.Step_Write:
- label_Sta_Write.Text = "NG";
- label_Sta_Write.BackColor = Color.Red;
- break;
- case TestStep_Enum.Step_Light:
- break;
- case TestStep_Enum.Step_Walk:
- break;
- case TestStep_Enum.Step_CheckVer:
- break;
- case TestStep_Enum.Step_Save:
- break;
- }
- }
- }
- //参数写入超时判断
- if (Write_Flag == true)
- {
- Write_DelayTimeCnt++;
- if (Write_DelayTimeCnt > 500)
- {
- button_Write.Enabled = true;
- button_Write.Text = "电机参数写入";
- Write_Flag = false;
- label_Sta_Write.BackColor = Color.Red;
- label_Sta_Write.Text = "NG";
- }
- }
- }
- //电机运行信息清除
- private void MC_RunInfo_Clear()
- {
- foreach (Control c in groupBox9.Controls)
- {
- if (c is TextBox)
- {
- c.Text = "---";
- }
- }
- }
- //系统清除按钮
- private void button_Clear_Click(object sender, EventArgs e)
- {
- var Code = new byte[5];
- Code[0] = (byte)'C';
- Code[1] = (byte)'L';
- Code[2] = (byte)'E';
- Code[3] = (byte)'A';
- Code[4] = (byte)'R';
- timer1.Enabled = false;
- if (MessageBox.Show("系统清除后将无法恢复", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- if (MessageBox.Show("请再次确认", "确认清除?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2605, Code);
- Ack_Flag = false;
- Ack_Cnt = 0;
- TestStep = TestStep_Enum.Step_SysClear;
- label_Sta_Clear.Text = "Ing";
- label_Sta_Clear.BackColor = Color.Yellow;
- }
- }
- timer1.Enabled = true;
- }
- //一键写入配置信息
- private void button_Write_Click(object sender, EventArgs e)
- {
- //导入配置文件
- if (comboBox_File.Items.Count == 0) //配置文件目录为空
- {
- MessageBox.Show("无可用配置文件!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
- {
- MessageBox.Show("请选择配置文件!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- //打开选中文件
- string SelectFile = comboBox_File.SelectedItem.ToString();
- StreamReader objReader = new StreamReader(CfgFilePath + "\\" + SelectFile);
- string sLine = "";
- ArrayList arrText = new ArrayList();//创建一个动态数组
- while (sLine != null)
- {
- sLine = objReader.ReadLine();
- arrText.Add(sLine);
- }
- objReader.Close();
- //读取电机型号和序列号
- if (textBox_PDinfo.Text.Contains("%#") == false)
- {
- MessageBox.Show("请输入正确的打标信息!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- button_Write.Enabled = false;
- button_Write.Text = "写入中";
- //反馈指令计数清零
- WriteAckCount = 0;
- Ack_Flag = false;
- Ack_Cnt = 0;
- Write_Flag = true;
- Write_DelayTimeCnt = 0;
- TestStep = TestStep_Enum.Step_Write;
- label_Sta_Write.Text = "Ing";
- label_Sta_Write.BackColor = Color.Yellow;
- //写入电机型号
- string Mode = textBox_PDinfo.Text.Split(new char[2] { '%', '#' })[0];
- var ModeArray = new byte[16];
- for (ushort i = 0; i < ((Mode.Length > 16) ? 16 : Mode.Length); i++)
- {
- ModeArray[i] = (byte)Mode[i];
- }
- if (Mode.Length < 16)
- {
- ModeArray[Mode.Length] = (byte)'.';
- for (ushort i = 0; i < 16 - Mode.Length - 1; i++)
- {
- ModeArray[Mode.Length + 1 + i] = 0x20;
- }
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2210, ModeArray);
- Thread.Sleep(200);
- //写入电机序列号
- string SN = textBox_PDinfo.Text.Split('#')[1];
- var SNArray = new byte[16];
- for (ushort i = 0; i < ((SN.Length > 16) ? 16 : SN.Length); i++)
- {
- SNArray[i] = (byte)SN[i];
- }
- if (SN.Length < 16)
- {
- SNArray[SN.Length] = (byte)'.';
- for (ushort i = 0; i < 16 - SN.Length - 1; i++)
- {
- SNArray[SN.Length + 1 + i] = 0x20;
- }
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2310, SNArray);
- Thread.Sleep(200);
- //写入用户参数1
- var ConfigParam = new byte[32];
- ushort DataTemp;
- //指拨模式
- ConfigParam[0] = Convert.ToByte(arrText[25].ToString().Split(',')[1]);
- //启动模式
- ConfigParam[1] = Convert.ToByte(arrText[1].ToString().Split(',')[1]);
- //停机时间
- DataTemp = Convert.ToUInt16(arrText[2].ToString().Split(',')[1]);
- ConfigParam[2] = (byte)(DataTemp & 0xFF);
- ConfigParam[3] = (byte)(DataTemp >> 8);
- //限速
- ConfigParam[4] = Convert.ToByte(arrText[3].ToString().Split(',')[1]);
- //下降速度
- ConfigParam[5] = Convert.ToByte(arrText[4].ToString().Split(',')[1]);
- //前飞
- ConfigParam[6] = Convert.ToByte(arrText[5].ToString().Split(',')[1]);
- //后飞
- ConfigParam[7] = Convert.ToByte(arrText[6].ToString().Split(',')[1]);
- //限流
- ConfigParam[8] = Convert.ToByte(arrText[7].ToString().Split(',')[1]);
- //温度预警
- ConfigParam[9] = (byte)(Convert.ToByte(arrText[8].ToString().Split(',')[1]) + 40);
- //温度保护
- ConfigParam[10] = (byte)(Convert.ToByte(arrText[9].ToString().Split(',')[1]) + 40);
- //无PBU
- ConfigParam[11] = Convert.ToByte(arrText[10].ToString().Split(',')[1]);
- //轮胎周长
- ConfigParam[12] = Convert.ToByte(arrText[11].ToString().Split(',')[1]);
- //电机系列号
- ConfigParam[13] = Convert.ToByte(arrText[12].ToString().Split(',')[1]);
- //ECO助力增益
- ConfigParam[14] = Convert.ToByte(arrText[13].ToString().Split(',')[1]);
- //ECO加速
- ConfigParam[15] = Convert.ToByte(arrText[14].ToString().Split(',')[1]);
- //NORM助力增益
- ConfigParam[16] = Convert.ToByte(arrText[15].ToString().Split(',')[1]);
- //NORM加速
- ConfigParam[17] = Convert.ToByte(arrText[16].ToString().Split(',')[1]);
- //SPORT助力增益
- ConfigParam[18] = Convert.ToByte(arrText[17].ToString().Split(',')[1]);
- //SPORT加速
- ConfigParam[19] = Convert.ToByte(arrText[18].ToString().Split(',')[1]);
- //TURBO助力增益
- ConfigParam[20] = Convert.ToByte(arrText[19].ToString().Split(',')[1]);
- //TURBO加速
- ConfigParam[21] = Convert.ToByte(arrText[20].ToString().Split(',')[1]);
- //SMART助力增益
- ConfigParam[22] = Convert.ToByte(arrText[21].ToString().Split(',')[1]);
- //SMART加速
- ConfigParam[23] = Convert.ToByte(arrText[22].ToString().Split(',')[1]);
- //速度传感器信号个数
- ConfigParam[24] = Convert.ToByte(arrText[23].ToString().Split(',')[1]);
- //踏频启动信号个数
- ConfigParam[25] = Convert.ToByte(arrText[24].ToString().Split(',')[1]);
- //速度传感器
- ConfigParam[26] = Convert.ToByte(arrText[26].ToString().Split(',')[1]);
- //周长微调
- ConfigParam[27] = (byte)Convert.ToInt16(arrText[27].ToString().Split(',')[1]);
- //低压保护阈值
- DataTemp = Convert.ToUInt16(arrText[28].ToString().Split(',')[1]);
- ConfigParam[28] = (byte)(DataTemp & 0xFF);
- ConfigParam[29] = (byte)(DataTemp >> 8);
- //推行限速
- DataTemp = Convert.ToByte(arrText[29].ToString().Split(',')[1]);
- ConfigParam[30] = (byte)Convert.ToByte(DataTemp);
- //推行马达转速
- ConfigParam[31] = (byte)Convert.ToByte(arrText[30].ToString().Split(',')[1]);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1B20, ConfigParam);
- Thread.Sleep(200);
- //写入用户参数2
- var ConfigParam_Ext = new byte[32];
- short DataTemp_Int16;
- //俯仰角零偏
- DataTemp_Int16 = (short)(decimal.Parse(arrText[34].ToString().Split(',')[1]) * 10);
- ConfigParam_Ext[0] = (byte)(DataTemp_Int16 & 0xFF);
- ConfigParam_Ext[1] = (byte)(DataTemp_Int16 >> 8);
- //横滚角零偏
- DataTemp_Int16 = (short)(decimal.Parse(arrText[35].ToString().Split(',')[1]) * 10);
- ConfigParam_Ext[2] = (byte)(DataTemp_Int16 & 0xFF);
- ConfigParam_Ext[3] = (byte)(DataTemp_Int16 >> 8);
- //支持姿态传感器
- ConfigParam_Ext[4] = Convert.ToByte(arrText[33].ToString().Split(',')[1]);
- //尾灯模式
- ConfigParam_Ext[5] = Convert.ToByte(arrText[36].ToString().Split(',')[1]);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x3120, ConfigParam_Ext);
- Thread.Sleep(200);
- //写入马达参数
- //额定功率
- DataTemp = Convert.ToUInt16(arrText[39].ToString().Split(',')[1]);
- ConfigParam[0] = (byte)(DataTemp & 0xFF);
- ConfigParam[1] = (byte)(DataTemp >> 8);
- //额定转速
- DataTemp = Convert.ToUInt16(arrText[40].ToString().Split(',')[1]);
- ConfigParam[2] = (byte)(DataTemp & 0xFF);
- ConfigParam[3] = (byte)(DataTemp >> 8);
- //定子电阻
- DataTemp = Convert.ToUInt16(arrText[41].ToString().Split(',')[1]);
- ConfigParam[4] = (byte)(DataTemp & 0xFF);
- ConfigParam[5] = (byte)(DataTemp >> 8);
- //Lq
- DataTemp = Convert.ToUInt16(arrText[42].ToString().Split(',')[1]);
- ConfigParam[6] = (byte)(DataTemp & 0xFF);
- ConfigParam[7] = (byte)(DataTemp >> 8);
- //Ld
- DataTemp = Convert.ToUInt16(arrText[43].ToString().Split(',')[1]);
- ConfigParam[8] = (byte)(DataTemp & 0xFF);
- ConfigParam[9] = (byte)(DataTemp >> 8);
- //反电动势
- DataTemp = Convert.ToUInt16(arrText[44].ToString().Split(',')[1]);
- ConfigParam[10] = (byte)(DataTemp & 0xFF);
- ConfigParam[11] = (byte)(DataTemp >> 8);
- //额定电压
- ConfigParam[12] = Convert.ToByte(arrText[45].ToString().Split(',')[1]);
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1D10, ConfigParam);
- Thread.Sleep(200);
- //更新推行模式测试判断标准
- MotorRadeSpeed = Convert.ToUInt16(arrText[40].ToString().Split(',')[1]);
- RateCadence = (ushort)(MotorRadeSpeed / 11U);
- RateVoltage = (ushort)(Convert.ToByte(arrText[45].ToString().Split(',')[1]) * 1000);
- //写入生产信息
- var MAC_InfoArray = new byte[32];
- string MAC_Info = arrText[48].ToString().Split(',')[1];
- for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
- {
- MAC_InfoArray[i] = (byte)MAC_Info[i];
- }
- MAC_Info = arrText[49].ToString().Split(',')[1];
- for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
- {
- MAC_InfoArray[8 + i] = (byte)MAC_Info[i];
- }
- MAC_Info = arrText[50].ToString().Split(',')[1];
- for (ushort i = 0; i < ((MAC_Info.Length > 8) ? 8 : MAC_Info.Length); i++)
- {
- MAC_InfoArray[16 + i] = (byte)MAC_Info[i];
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2420, MAC_InfoArray);
- Thread.Sleep(200);
- //写入校验密钥
- var KeyArray = new byte[8];
- if (arrText[51].ToString().EndsWith(",") == false)
- {
- string KeyString = arrText[51].ToString().Split(',')[1];
- for (ushort i = 0; i < ((KeyString.Length > 8) ? 8 : KeyString.Length); i++)
- {
- KeyArray[i] = (byte)KeyString[i];
- }
- }
- else
- {
- KeyArray.Initialize();
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1108, KeyArray);
- Thread.Sleep(200);
- //写入自定义信息1
- var UserInfo1Array = new byte[16];
- if (arrText[52].ToString().EndsWith(",") == false)
- {
- string UserInfo1String = arrText[52].ToString().Split(',')[1];
- for (ushort i = 0; i < (UserInfo1String.Length > 16 ? 16 : UserInfo1String.Length); i++)
- {
- UserInfo1Array[i] = (byte)UserInfo1String[i];
- }
- }
- else
- {
- UserInfo1Array.Initialize();
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1410, UserInfo1Array);
- Thread.Sleep(200);
- //写入自定义信息2
- var UserInfo2Array = new byte[16];
- if (arrText[53].ToString().EndsWith(",") == false)
- {
- string UserInfo2String = arrText[53].ToString().Split(',')[1];
- for (ushort i = 0; i < (UserInfo2String.Length > 16 ? 16 : UserInfo2String.Length); i++)
- {
- UserInfo2Array[i] = (byte)UserInfo2String[i];
- }
- }
- else
- {
- UserInfo2Array.Initialize();
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1610, UserInfo2Array);
- Thread.Sleep(200);
- //写入自定义信息3
- var UserInfo3Array = new byte[16];
- if (arrText[54].ToString().EndsWith(",") == false)
- {
- string UserInfo3String = arrText[54].ToString().Split(',')[1];
- for (ushort i = 0; i < (UserInfo3String.Length > 16 ? 16 : UserInfo3String.Length); i++)
- {
- UserInfo3Array[i] = (byte)UserInfo3String[i];
- }
- }
- else
- {
- UserInfo3Array.Initialize();
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1810, UserInfo3Array);
- Thread.Sleep(200);
- }
- private void ImportForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- portsettings_form.serialPort1.Close();
- portsettings_form.comboBox_ComIndex.Enabled = true;
- portsettings_form.button_ComOpen.Text = "连接";
- portsettings_form.g_blnIsOpen = false;
- }
- private void button_LightCtl_Click(object sender, EventArgs e)
- {
- var CtrlCode = new byte[2];
- CtrlCode[0] = 0x00;
- if (button_LightCtl.Text == "开灯")
- {
- button_LightCtl.Text = "关灯";
- CtrlCode[1] = 0xF1;
- label_sta_light.BackColor = Color.Yellow;
- label_sta_light.Text = "Ing";
- radioButton_Light_OK.Enabled = true;
- radioButton_Light_NG.Enabled = true;
- }
- else
- {
- button_LightCtl.Text = "开灯";
- CtrlCode[1] = 0xF0;
- radioButton_Light_OK.Enabled = false;
- radioButton_Light_NG.Enabled = false;
- }
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, CtrlCode);
- TestStep = TestStep_Enum.Step_Light;
- }
- private void radioButton_Light_OK_Click(object sender, EventArgs e)
- {
- label_sta_light.BackColor = Color.Green;
- label_sta_light.Text = "OK";
- //使能推行测试按钮
- button_Walk.Enabled = true;
- }
- private void radioButton_Light_NG_Click(object sender, EventArgs e)
- {
- label_sta_light.BackColor = Color.Red;
- label_sta_light.Text = "NG";
- //使能推行测试按钮
- button_Walk.Enabled = true;
- }
- private void button_Walk_Click(object sender, EventArgs e)
- {
- var Code = new byte[2];
- if (button_Walk.Text == "启动")
- {
- button_Walk.Text = "停止";
- //发送指令使电机进入配置模式
- Code[0] = 0x01;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
- Thread.Sleep(200);
- //发送指令进入推行模式
- Code[0] = 0x22;
- if (button_LightCtl.Text == "关灯")
- Code[1] = 0xF1;
- else
- Code[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
- label_sensor_vol.BackColor = Color.Red;
- label_sensor_vol.Text = "NG";
- label_sensor_cur.BackColor = Color.Red;
- label_sensor_cur.Text = "NG";
- label_sensor_speed.BackColor = Color.Red;
- label_sensor_speed.Text = "NG";
- label_sensor_cadence.BackColor = Color.Red;
- label_sensor_cadence.Text = "NG";
- label_sensor_motor.BackColor = Color.Red;
- label_sensor_motor.Text = "NG";
- label_sensor_PCB_temp.BackColor = Color.Red;
- label_sensor_PCB_temp.Text = "NG";
- label_sensor_coil_temp.BackColor = Color.Red;
- label_sensor_coil_temp.Text = "NG";
- label_sensor_MCU_temp.BackColor = Color.Red;
- label_sensor_MCU_temp.Text = "NG";
- TestStep = TestStep_Enum.Step_Walk;
- }
- else
- {
- button_Walk.Text = "启动";
- //发送指令退出推行模式
- Code[0] = 0x00;
- if (button_LightCtl.Text == "关灯")
- Code[1] = 0xF1;
- else
- Code[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, Code);
- Thread.Sleep(200);
- //发送指令使电机退出配置模式
- Code[0] = 0x00;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x1901, Code);
- button_Check_Ver.Enabled = true;
- TestStep = TestStep_Enum.Step_CheckVer;
- }
- }
- private void button_Check_Ver_Click(object sender, EventArgs e)
- {
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
- Thread.Sleep(200);
- SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
- richTextBox_Ver.Text = "";
- button_Fnish.Enabled = true;
- }
- private void button_Fnish_Click(object sender, EventArgs e)
- {
- button_Fnish.Text = "存储中";
- button_Fnish.Enabled = false;
- TestStep = TestStep_Enum.Step_Save;
- //页面存储
- 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 = System.IO.Directory.GetCurrentDirectory() + "\\log\\";
- LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_测试记录" + ".png";
- bit.Save(LogSaveFileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
- //一键导出数据
- //设置保存路径
- LogSaveFileName = System.IO.Directory.GetCurrentDirectory() + "\\log\\";
- LogSaveFileName += textBox_PDinfo.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(":", string.Empty).Replace(" ", "_") + "_ParamsLog" + ".ttlog";
- //开始发送指令
- byte[] SendData = new byte[32];
- //查询校验密钥
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1000, null);
- Thread.Sleep(200);
- //查询版本信息
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1200, null);
- Thread.Sleep(200);
- SendCmd((ushort)0x651, (byte)0x11, (ushort)0x1200, null);
- Thread.Sleep(200);
- //查询用户字符串1
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1300, null);
- Thread.Sleep(200);
- //查询用户字符串2
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1500, null);
- Thread.Sleep(200);
- //查询用户字符串3
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1700, null);
- Thread.Sleep(200);
- //查询用户参数1
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1A00, null);
- Thread.Sleep(200);
- //查询马达参数
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1C00, null);
- Thread.Sleep(200);
- //查询历史信息
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1E00, null);
- Thread.Sleep(200);
- //查询生产信息
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x1F00, null);
- Thread.Sleep(200);
- //查询力矩传感器零点 Volans
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2000, null);
- Thread.Sleep(200);
- //查询运行信息
- SendData[0] = 0x00;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- SendData[0] = 0x01;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- SendData[0] = 0x02;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- SendData[0] = 0x03;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- SendData[0] = 0x04;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- SendData[0] = 0x33;
- SendData[1] = 0xF0;
- SendCmd((ushort)0x751, (byte)0x16, (ushort)0x2802, SendData);
- Thread.Sleep(200);
- //查询力矩传感器校正信息 Volans
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2900, null);
- Thread.Sleep(200);
- //导出故障日志
- UInt32 Address_Begin;
- UInt32 Addres_End;
- Address_Begin = 0x0801F800;
- Addres_End = 0x0801FBFF;
- UInt32 ReadCount = 0;
- ReadCount = (Addres_End - Address_Begin + 1) / 128;
- for (ushort i = 0; i < ReadCount; i++)
- {
- var SendByte = new byte[8];
- UInt32 Address_Temp_Begin;
- UInt32 Address_Temp_End;
- Address_Temp_Begin = Address_Begin + 128 * (UInt32)i;
- SendByte[0] = (byte)(Address_Temp_Begin >> 24);
- SendByte[1] = (byte)(Address_Temp_Begin >> 16);
- SendByte[2] = (byte)(Address_Temp_Begin >> 8);
- SendByte[3] = (byte)(Address_Temp_Begin);
- Address_Temp_End = Address_Temp_Begin + 127;
- SendByte[4] = (byte)(Address_Temp_End >> 24);
- SendByte[5] = (byte)(Address_Temp_End >> 16);
- SendByte[6] = (byte)(Address_Temp_End >> 8);
- SendByte[7] = (byte)(Address_Temp_End);
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2D08, SendByte);
- Thread.Sleep(200);
- }
- //查询骑行信息
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x2E00, null);
- Thread.Sleep(200);
- //查询用户参数2
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3000, null);
- Thread.Sleep(200);
- //查询力矩传感器零点值 Pegasi
- SendData[0] = 1;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
- Thread.Sleep(200);
- SendData[0] = 2;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
- Thread.Sleep(200);
- SendData[0] = 3;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3201, SendData);
- Thread.Sleep(200);
- //查询校正系数 Pegasi
- SendData[0] = 1;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
- Thread.Sleep(200);
- SendData[0] = 2;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
- Thread.Sleep(200);
- SendData[0] = 3;
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3301, SendData);
- Thread.Sleep(200);
- //查询启动值 Pegasi
- SendCmd((ushort)0x751, (byte)0x11, (ushort)0x3500, null);
- Thread.Sleep(200);
- timer1.Enabled = false;
- MessageBox.Show("已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- timer1.Enabled = true;
- button_Fnish.Text = "保存";
- button_Clear.Enabled = false;
- textBox_PDinfo.Enabled = false;
- button_Write.Enabled = false;
- button_LightCtl.Enabled = false;
- button_Walk.Enabled = false;
- button_Check_Ver.Enabled = false;
- radioButton_Light_NG.Enabled = false;
- radioButton_Light_OK.Enabled = false;
- }
- private void button_Start_Click(object sender, EventArgs e)
- {
- button_Clear.Enabled = true;
- //所有按钮和标签初始化
- radioButton_Light_OK.Checked = false;
- radioButton_Light_NG.Checked = true;
- label_error_code.BackColor = Color.Green;
- label_error_code.Text = "OK";
- label_Sta_Clear.BackColor = Color.Red;
- label_Sta_Clear.Text = "NG";
- label_Sta_Write.BackColor = Color.Red;
- label_Sta_Write.Text = "NG";
- label_sta_light.BackColor = Color.Red;
- label_sta_light.Text = "NG";
- label_sensor_vol.BackColor = Color.Red;
- label_sensor_vol.Text = "NG";
- label_sensor_cur.BackColor = Color.Red;
- label_sensor_cur.Text = "NG";
- label_sensor_speed.BackColor = Color.Red;
- label_sensor_speed.Text = "NG";
- label_sensor_cadence.BackColor = Color.Red;
- label_sensor_cadence.Text = "NG";
- label_sensor_motor.BackColor = Color.Red;
- label_sensor_motor.Text = "NG";
- label_sensor_PCB_temp.BackColor = Color.Red;
- label_sensor_PCB_temp.Text = "NG";
- label_sensor_coil_temp.BackColor = Color.Red;
- label_sensor_coil_temp.Text = "NG";
- label_sensor_MCU_temp.BackColor = Color.Red;
- label_sensor_MCU_temp.Text = "NG";
- button_LightCtl.Text = "开灯";
- button_Walk.Text = "启动";
- richTextBox_Ver.Text = "";
- TestStep = TestStep_Enum.Step_Init;
- }
- private void timer2_Tick(object sender, EventArgs e)
- {
- label_SystemTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- }
- 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;
- }
- }
- 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;
- }
- }
- private void button_CheckCfg_Click(object sender, EventArgs e)
- {
- if (button_CheckCfg.Text == "查看")
- {
- if (comboBox_File.Items.Count == 0) //配置文件目录为空
- {
- MessageBox.Show("无可用配置文件!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (comboBox_File.Text.Trim().Length == 0)//未选择配置文件
- {
- MessageBox.Show("请选择配置文件!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- richTextBox_Cfg.Visible = true;
- richTextBox_Cfg.Text = "";
- button_CheckCfg.Text = "隐藏";
- this.Size = new Size(this.Size.Width + 280, this.Size.Height);
- //显示文件
- string SelectFile = comboBox_File.SelectedItem.ToString();
- StreamReader objReader = new StreamReader(CfgFilePath + "\\" + SelectFile);
- string sLine = "";
- ArrayList arrText = new ArrayList();//创建一个动态数组
- while (sLine != null)
- {
- sLine = objReader.ReadLine();
- arrText.Add(sLine);
- }
- objReader.Close();
- foreach (string sOutput in arrText)
- {
- richTextBox_Cfg.Text += sOutput + "\r\n";
- }
- }
- else
- {
- richTextBox_Cfg.Visible = false;
- richTextBox_Cfg.Text = "";
- button_CheckCfg.Text = "查看";
- this.Size = new Size(this.Size.Width - 280, this.Size.Height);
- }
- }
- }
- }
|