private void ResetCanButton_Click(object sender, EventArgs e) { if (CanZLG.VCI_ResetCAN(m_devtype, 0, can_ind1) != CanZLG.STATUS_OK) { MessageBox.Show("复位失败"); } StartCanButton.Enabled = true; ResetCanButton.Enabled = false; }
private void CarTest_FormClosing(object sender, FormClosingEventArgs e) { try { RecvThread.Abort(); if (canOpen) { CanZLG.VCI_CloseDevice(m_devtype, 0); } timer1.Stop(); SimulateTimer.Stop(); sw_Record.Close(); } catch (Exception) { } }
private void StartCanButton_Click(object sender, EventArgs e) { if (canOpen) { canOpen = false; CanZLG.VCI_CloseDevice(m_devtype, 0); } CanZLG.VCI_INIT_CONFIG init_config = new CanZLG.VCI_INIT_CONFIG(); init_config.Mode = 0; init_config.Filter = 1; init_config.AccCode = 0; init_config.AccMask = 0xffffffff; init_config.AccCode = 0x451 << 20; init_config.AccMask = 0x000FFFFF; // init_config.Timing0 = 0; // init_config.Timing1 = 0x1c; if (CanZLG.VCI_OpenDevice(m_devtype, 0, 0) != CanZLG.STATUS_OK) { MessageBox.Show("Open device fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte[] baud; baud = BitConverter.GetBytes(0x60007); //500k 0x60007 //UInt32 baud; //baud = 0x1000c8; if (CanZLG.VCI_SetReference(m_devtype, 0, can_ind1, 0, baud) != CanZLG.STATUS_OK) { MessageBox.Show("设置波特率错误,打开设备失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); CanZLG.VCI_CloseDevice(m_devtype, 0); return; } if (CanZLG.VCI_InitCAN(m_devtype, 0, can_ind1, ref init_config) != CanZLG.STATUS_OK) { MessageBox.Show("Init can fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); CanZLG.VCI_CloseDevice(m_devtype, 0); return; } if (CanZLG.VCI_StartCAN(m_devtype, 0, can_ind1) != CanZLG.STATUS_OK) { MessageBox.Show("Start Failed"); } /* * baud = BitConverter.GetBytes(0x1000c8);//250k * init_config.Timing0 = 1; * init_config.Timing1 = 0x1c; * if (CanZLG.VCI_SetReference(m_devtype, 0, can_ind0, 0, baud) != CanZLG.STATUS_OK) * { * * MessageBox.Show("设置波特率错误,打开设备失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); * CanZLG.VCI_CloseDevice(m_devtype, 0); * return; * } * * if (CanZLG.VCI_InitCAN(m_devtype, 0, can_ind0, ref init_config) != CanZLG.STATUS_OK) * { * * //MessageBox.Show("Init can fault!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); * * //CanZLG.VCI_CloseDevice(m_devtype, 0); * //return; * } * if (CanZLG.VCI_StartCAN(m_devtype, 0, can_ind0) != CanZLG.STATUS_OK) * { * MessageBox.Show("Start Failed"); * } */ canOpen = true; StartCanButton.Enabled = false; ModeButton.Enabled = true; ResetCanButton.Enabled = true; //RecTimer.Start(); }
//List<uint> ids = new List<uint>(); //StreamWriter sw = new StreamWriter(@"D:\Documents\EQTest\0723\a.txt"); private void ReceiveCanData() { uint ReceiveNum = 0; uint stsResult = 0; while (true) { if (this.canOpen) { ReceiveNum = CanZLG.VCI_GetReceiveNum(m_devtype, 0, can_ind1); if (ReceiveNum > 0) { ReceiveNum = Math.Min(ReceiveNum, 50); for (int index = 0; index < ReceiveNum; index++) { stsResult = CanZLG.VCI_Receive(m_devtype, 0, can_ind1, ref receiveFrameinfo, 1, 1); if (stsResult != 1) { break; } else { //ids.Add(receiveFrameinfo.ID); //if (ids.Count > 20) //{ // sw.WriteLine(string.Join("\t", ids)); // sw.Flush(); // ids.Clear(); //} //if (receiveFrameinfo.ID == 0x21) //{ // SpeedInfo = receiveFrameinfo.Data[0]*256 + receiveFrameinfo.Data[1]; // //GasInfo = frameinfo.Data[2]; // BrakeInfo = receiveFrameinfo.Data[3]; // GearInfo = receiveFrameinfo.Data[4]; // AccelerateInfo = receiveFrameinfo.Data[6]*100; // Delay = receiveFrameinfo.Data[7]*10; // Record = new StringBuilder((DateTime.Now.Hour > 9 ? "" : "0") + DateTime.Now.Hour // + (DateTime.Now.Minute > 9 ? ":" : ":0") + // DateTime.Now.Minute // + (DateTime.Now.Second > 9 ? ":" : ":0") + // DateTime.Now.Second // + (DateTime.Now.Millisecond > 9 ? ":" : ":0") + // DateTime.Now.Millisecond + "\t"); // Record.Append("SpeedInfo:" + SpeedInfo + " Accelerate:" + AccelerateInfo + " Delay:" + // Delay); // lock (recordList) // { // recordList.Add(Record.ToString()); // } //} if (receiveFrameinfo.ID == 0x451) { receive451Data = string.Empty; GasInfo = receiveFrameinfo.Data[6]; for (int i = 0; i < 8; i++) { receive451Data += receiveFrameinfo.Data[i].ToString("X2") + " "; } } if (receiveFrameinfo.ID == 0x403) { can403count++; receive403Data = string.Empty; GasInfo = receiveFrameinfo.Data[6]; for (int i = 0; i < 8; i++) { receive403Data += receiveFrameinfo.Data[i].ToString("X2") + " "; } //Receive_Speed = (((double)(receiveFrameinfo.Data[1] & 0x07) * 1024) + ((double)receiveFrameinfo.Data[2] * 4) + (double)((receiveFrameinfo.Data[3] & 0xC0) >> 6)) / 16; } if (receiveFrameinfo.ID == 0x394) { Receive_Steer = (double)((receiveFrameinfo.Data[1] << 7) + (receiveFrameinfo.Data[2] >> 1)) * 0.04375; } //解析角度反馈 //if (StartRecord && receiveFrameinfo.ID == 0x401) //{ // RecordCount++; // int ActualAngel = receiveFrameinfo.Data[4]*256 + receiveFrameinfo.Data[5] - 1024; // if (Delay == 0 && ActualAngel != LastReceiveAngle) // Delay = RecordCount; // if (Complete == 0 && Math.Abs(Angle - ActualAngel) < 2) // Complete = RecordCount; // Log log = new Log(); // log.Data = new List<int>(); // log.Data.Add(RecordCount); // log.Data.Add(Angle); // log.Data.Add(ActualAngel); // log.Data.Add(Delay); // log.Data.Add(Complete); // LogData.Add(log); // LastReceiveAngle = ActualAngel; //} /* * if ( frameinfo.ID == 0x2e9) * { * SpeedInfo = 16*(frameinfo.Data[6]&0x1f)+ frameinfo.Data[5]/16; * AccelerateInfo = (SpeedInfo - SpeedInfoEx)/0.18; * SpeedInfoEx = SpeedInfo; * Record = new StringBuilder((DateTime.Now.Hour > 9 ? "" : "0") + DateTime.Now.Hour + (DateTime.Now.Minute > 9 ? ":" : ":0") + DateTime.Now.Minute + (DateTime.Now.Second > 9 ? ":" : ":0") + DateTime.Now.Second + (DateTime.Now.Millisecond > 9 ? ":" : ":0") + DateTime.Now.Millisecond + "\t"); + + Record.Append("SpeedInfo:" + SpeedInfo + " Accelerate:" + AccelerateInfo ); + + if (ModeButton.Text == "自动驾驶") + { + lock (recordList) + { + recordList.Add(Record.ToString()); + } + } + + } + if (stsResult > 0 && frameinfo.ID == 0x403) + { + GearInfo = frameinfo.Data[0] & 0x0f; + } */ } } if (stsResult == 0xFFFFFFFF) { CanZLG.VCI_ReadErrInfo(m_devtype, 0, can_ind0, ref Err_Info); CanZLG.VCI_ReadCANStatus(m_devtype, 0, can_ind0, ref Can_Status); CanZLG.VCI_ClearBuffer(m_devtype, 0, can_ind0); } else { CanZLG.VCI_ClearBuffer(m_devtype, 0, can_ind1); } } } Thread.Sleep(1); } }
private bool Send(int para, SendType type, int para2 = 0) { if (!canOpen) { return(false); } frameinfo.ID = 0x20; switch (type) { case SendType.Auto: frameinfo.Data[0] = (byte)para2; if (para2 == 10) { frameinfo.Data[1] = 0x09; } if (para2 == 00) { frameinfo.Data[1] = 0x08; } frameinfo.Data[2] = 0x00; frameinfo.Data[3] = 0x00; frameinfo.Data[4] = 0x00; frameinfo.Data[5] = (byte)(para >> 8); frameinfo.Data[6] = (byte)(para & 0xff); frameinfo.Data[7] = 0x56; break; case SendType.Angle: //frameinfo.Data[1] = 0x08; frameinfo.Data[5] = (byte)(para & 0xff); frameinfo.Data[4] = (byte)(para >> 8); frameinfo.Data[6] = 0xF5; break; case SendType.Brake: frameinfo.Data[0] = (byte)para; frameinfo.Data[1] = 0x09; frameinfo.Data[5] = 0x04; frameinfo.Data[7] = 0x56; break; case SendType.BrakeReset: frameinfo.Data[0] = (byte)para; frameinfo.Data[1] = 0x08; frameinfo.Data[5] = 0x04; frameinfo.Data[7] = 0x56; break; case SendType.Light: frameinfo.Data[1] = 0x09; frameinfo.Data[1] += ConvertData1(para); frameinfo.Data[2] = ConvertData2(para); break; case SendType.Mode: frameinfo.Data[1] = (byte)para; break; case SendType.AnlgeCan: for (int i = 0; i < 8; ++i) { frameinfo.Data[i] = 0; } if (Angle != LastSendAngle) { ResetRecordAngel(); } frameinfo.ID = 0x500; frameinfo.Data[0] = 0x20; //模式 1 2 5 reserve frameinfo.Data[3] = (byte)((Angle + 1024) >> 8); frameinfo.Data[4] = (byte)((Angle + 1024) & 0xff); frameinfo.Data[6] = (byte)Torque; //角速度+力矩 0x80 for (int i = 0; i < 7; i++) { frameinfo.Data[7] ^= frameinfo.Data[i]; } LastSendAngle = Angle; break; default: break; } frameinfo.RemoteFlag = 0; frameinfo.ExternFlag = 0; frameinfo.DataLen = 8; // 打印输出测试用 //string str = string.Empty; //for (int i = 0; i < 8; i++) //{ // if(frameinfo.Data[i] > 0) // str += "Data" + i + ":" + frameinfo.Data[i].ToString("x2"); //} //MessageBox.Show(str); if (CanZLG.VCI_Transmit(m_devtype, 0, can_ind0, ref frameinfo, 1) != CanZLG.STATUS_OK) { MessageBox.Show(".v"); return(false); } return(true); }