Esempio n. 1
0
        private void connectM_Click(object sender, EventArgs e)
        {
            if (mIsOpen == 1)
            {
                timerSend.Enabled = false;
                DllAdapte.VCI_CloseDevice(mDevType, mDevInd);
                mIsOpen = 0;
            }
            else
            {
                uint ret = DllAdapte.VCI_OpenDevice(mDevType, mDevInd, 0);

                if (ret <= 0)
                {
                    MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {
                    AddListBoxItem("设备连接成功。");
                }

                mIsOpen = 1;
                InitCanAll();
            }

            connectM.Text     = mIsOpen == 1 ? "断开" : "连接";
            connectBtn.Text   = connectM.Text;
            timer_rec.Enabled = mIsOpen == 1 ? true : false;
        }
Esempio n. 2
0
        //发送按钮点击事件处理函数
        unsafe private void button_Send_Click(object sender, EventArgs e)
        {
            if (mIsOpen == 0)
            {
                return;
            }

            VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();

            //sendobj.Init();
            sendobj.SendType   = (byte)comboBox_SendType.SelectedIndex;
            sendobj.RemoteFlag = (byte)comboBox_FrameFormat.SelectedIndex;
            sendobj.ExternFlag = (byte)comboBox_FrameType.SelectedIndex;
            sendobj.ID         = System.Convert.ToUInt32("0x" + textBox_ID.Text, 16);
            int len = (textBox_Data.Text.Length + 1) / 3;

            sendobj.DataLen = System.Convert.ToByte(len);
            String strdata = textBox_Data.Text;

            for (int i = 0; i < len; i++)
            {
                if (i >= 8)
                {
                    break;
                }

                sendobj.Data[i] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16);
            }

            if (DllAdapte.VCI_Transmit(mDevType, mDevInd, mCanInd, ref sendobj, 1) == 0)
            {
                MessageBox.Show("发送失败", "错误",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Esempio n. 3
0
 //主窗口关闭处理函数
 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (mIsOpen == 1)
     {
         DllAdapte.VCI_CloseDevice(mDevType, mDevInd);
     }
 }
Esempio n. 4
0
        //初始化所有can通道
        private void InitCanAll()
        {
            VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();

            config.AccCode = System.Convert.ToUInt32("0x" + textBox_AccCode.Text, 16);
            config.AccMask = System.Convert.ToUInt32("0x" + textBox_AccMask.Text, 16);
            config.Filter  = mFilter; //(Byte)comboBox_Filter.SelectedIndex;
            config.Mode    = mMode;   //(Byte)comboBox_Mode.SelectedIndex;
            config.Timing0 = System.Convert.ToByte(can0T0TextBox.Text, 16);
            config.Timing1 = System.Convert.ToByte(can0T1TextBox.Text, 16);
            DllAdapte.VCI_InitCAN(mDevType, mDevInd, 0 /*(UInt32)CanIndex.Can0*/, ref config);
            config.Timing0 = System.Convert.ToByte(can1T0TextBox.Text, 16);
            config.Timing1 = System.Convert.ToByte(can1T1TextBox.Text, 16);
            DllAdapte.VCI_InitCAN(mDevType, mDevInd, 1 /*(UInt32)CanIndex.Can1*/, ref config);
        }
Esempio n. 5
0
        //can启动逻辑
        private void CanStart(UInt32 canId)
        {
            string state = string.Empty;
            //启动Can0通道
            uint ret = DllAdapte.VCI_StartCAN(mDevType, mDevInd, canId);

            if (ret > 0)
            {
                state = "成功";
            }
            else
            {
                state = "失败";
            }

            string disStr = string.Format("CAN{0}通道启动{1}。", canId /*.ToString()*/, state);

            RevListBox.Items.Add(disStr);
        }
Esempio n. 6
0
        //根据指定的can通道进行初始化
        private void InitCanByIndex(UInt32 canInd)
        {
            VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();

            if (canInd == 0)
            {
                config.Timing0 = System.Convert.ToByte(can0T0TextBox.Text, 16);
                config.Timing1 = System.Convert.ToByte(can0T1TextBox.Text, 16);
            }
            else if (canInd == 1)
            {
                config.Timing0 = System.Convert.ToByte(can1T0TextBox.Text, 16);
                config.Timing1 = System.Convert.ToByte(can1T1TextBox.Text, 16);
            }

            config.AccCode = System.Convert.ToUInt32("0x" + textBox_AccCode.Text, 16);
            config.AccMask = System.Convert.ToUInt32("0x" + textBox_AccMask.Text, 16);
            config.Filter  = mFilter; //(Byte)comboBox_Filter.SelectedIndex;
            config.Mode    = mMode;   //(Byte)comboBox_Mode.SelectedIndex;
            DllAdapte.VCI_InitCAN(mDevType, mDevInd, canInd, ref config);
        }
Esempio n. 7
0
        private void bmsUp_Click(object sender, EventArgs e)
        {
            string fileType = string.Empty;

            DllAdapte.VCI_ClearBuffer(mDevType, mDevInd, 0);

            //检查升级文件路径是否为空
            if (string.IsNullOrEmpty(mFilePath))
            {
                MessageBox.Show("请先选择升级文件!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //判断升级文件是否是bin文件
            fileType = Path.GetExtension(mFilePath);

            if (!fileType.Equals(".bin")) //if (fileType.Contains("bin"))
            {
                MessageBox.Show("升级文件类型不是xxx.bin,请选择bin文件!", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }


            mUpgradeFlag      = 1;                           //升级标志置1
            timerSend.Enabled = mIsOpen == 1 ? true : false; //使能发送定时器
            mUpCmd            = UpgradeCmd.CmdJump;
            mBinStream        = new FileStream(mFilePath, FileMode.Open, FileAccess.Read);
            mBinReader        = new BinaryReader(mBinStream);//二进制读写器
            mBinReader.BaseStream.Seek(0, SeekOrigin.Begin);

            FileInfo fileInfo = new FileInfo(mFilePath);

            mFileSize = (UInt32)fileInfo.Length;

            mSizeStr = (mFileSize & 0xff).ToString("X2") + " " + ((mFileSize >> 8) & 0xff).ToString("X2") + " " + ((mFileSize >> 16) & 0xff).ToString("X2") + " " + ((mFileSize >> 24) & 0xff).ToString("X2");

            upgradeBtn.Enabled = false;
        }
Esempio n. 8
0
        //can复位逻辑
        private void CanReset(UInt32 canId)
        {
            timerSend.Enabled = false;
            string state = string.Empty;

            mRevFrameNum  = 0;
            mSendFrameNum = 0;  //复位时清零
            //复位Can0通道
            uint ret = DllAdapte.VCI_ResetCAN(mDevType, mDevInd, canId);

            if (ret > 0)
            {
                state = "成功";
            }
            else
            {
                state = "失败";
            }

            string disStr = string.Format("CAN{0}通道复位{1}。", canId /*.ToString()*/, state);

            RevListBox.Items.Add(disStr);
        }
Esempio n. 9
0
        //发送升级数据
        unsafe private void SendUpData(string id, string data)
        {
            uint        canId   = 0;
            VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();

            //sendobj.Init();
            sendobj.SendType   = (byte)comboBox_SendType.SelectedIndex;
            sendobj.RemoteFlag = (byte)comboBox_FrameFormat.SelectedIndex;
            sendobj.ExternFlag = (byte)comboBox_FrameType.SelectedIndex;
            sendobj.ID         = System.Convert.ToUInt32("0x" + id, 16);
            int len = (data.Length + 1) / 3;

            sendobj.DataLen = System.Convert.ToByte(len);
            String strdata = data;

            Byte dat = (Byte)0;

            for (int i = 0; i < len; i++)
            {
                if (i >= 8)
                {
                    break;
                }

                dat             = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16);
                sendobj.Data[i] = dat;
            }

            sendListBox.Items.Add("报文ID: 0x" + id + "  发送数据: " + data + "    发送帧数: " + mSendFrameNum.ToString("X4"));
            sendListBox.SelectedIndex = sendListBox.Items.Count - 1;

            if (DllAdapte.VCI_Transmit(mDevType, mDevInd, canId, ref sendobj, 1) != 1)
            {
                sendListBox.Items.Add("发送失败");
            }
        }
Esempio n. 10
0
        unsafe private void ReceiveUpData()
        {
            String idStr   = "";
            string dataStr = string.Empty;
            UInt32 res     = new UInt32();
            UInt32 canId   = 0;

            res = DllAdapte.VCI_GetReceiveNum(mDevType, mDevInd, canId);

            if (res == 0)
            {
                return;
            }

            //res = DllAdapte.VCI_Receive(mDevType, mDevInd, mCanInd, ref mRecObj[0],50, 100);
            UInt32 con_maxlen = 50;
            IntPtr pt         = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * (Int32)con_maxlen);

            res = DllAdapte.VCI_Receive(mDevType, mDevInd, canId, pt, con_maxlen, 100);

            for (UInt32 i = 0; i < res; i++)
            {
                VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(VCI_CAN_OBJ))), typeof(VCI_CAN_OBJ));
                idStr = System.Convert.ToString((Int32)obj.ID, 16);

                if (idStr.Contains("1441abaa"))    // && mPauseFlag == 0
                {
                    if (obj.Data[1] == (byte)0xcc)
                    {
                        mUpCmd = UpgradeCmd.CmdUpgrade;
                        sendListBox.Items.Add("同主机Ping成功!");
                    }
                    else
                    {
                        mUpCmd = UpgradeCmd.CmdPing;
                    }
                }

                if (idStr.Contains("1442abaa"))     // && mPauseFlag == 0
                {
                    if (obj.Data[1] == (byte)0xcc)
                    {
                        mFrameSendFlag = 0;
                        mUpgradeFlag   = 0;
                        mUpCmd         = UpgradeCmd.CmdNone;
                        //timerSend.Enabled = false;  //重启后要先关闭发送定时器
                        sendListBox.Items.Add("主机重启成功!");
                    }
                }

                if (idStr.Contains("1443abaa"))      // && mPauseFlag == 0
                {
                    if (obj.Data[1] == (byte)0xcc)
                    {
                        mUpCmd = UpgradeCmd.CmdSendData;
                        sendListBox.Items.Add("发送升级地址成功!");
                    }
                    else if (obj.Data[1] == (byte)0x33)
                    {
                        mUpgradeFlag = 0;
                        mUpCmd       = UpgradeCmd.CmdNone;
                        sendListBox.Items.Add("升级下载地址不符合FLASH空间大小!");
                    }
                    else
                    {
                        mUpCmd = UpgradeCmd.CmdUpgrade;
                    }
                }

                if (idStr.Contains("1444abaa"))     // && mPauseFlag == 0
                {
                    if (obj.Data[1] == (byte)0x33)
                    {
                        mRevFrameNum        = 0;
                        mUpgradeFlag        = 0;
                        progressBar.Visible = false;
                        mUpCmd = UpgradeCmd.CmdRun;
                        sendListBox.Items.Add("升级成功!");
                        mBinReader.Close();
                        mBinStream.Close();
                    }
                    else if (obj.Data[1] == (byte)0xcc)
                    {
                        mUpCmd = UpgradeCmd.CmdSendData;
                    }
                    else
                    {
                        if (mSendDone == 1)
                        {
                            progressBar.Visible = false;
                            mSendDone           = 0; //标志复位
                            mUpgradeFlag        = 0;
                            //mUpCmd = UpgradeCmd.CmdRun;
                            sendListBox.Items.Add("数据发送完成但未收到主机应答信号!");
                        }
                        else
                        {
                            mUpgradeFlag = 0;
                            mUpCmd       = UpgradeCmd.CmdNone;
                            sendListBox.Items.Add("提示:主机在本帧中有丢失数据!");
                        }
                    }
                }

                if (obj.RemoteFlag == 0)
                {
                    byte len = (byte)(obj.DataLen % 9);

                    for (byte j = 0; j < len; j++)
                    {
                        if (j == 0)
                        {
                            dataStr += obj.Data[j].ToString("X2");
                        }
                        else
                        {
                            dataStr += " " + obj.Data[j].ToString("X2"); //System.Convert.ToString(obj.Data[j], 16);
                        }
                    }
                }

                if (mPauseFlag == 0)
                {
                    if (mFrameSendFlag == 1)
                    {
                        mRevFrameNum++;
                    }

                    RevListBox.Items.Add("报文ID: 0x" + idStr + "  接收数据: " + dataStr + "    接收帧数: " + mRevFrameNum.ToString("X4"));
                    RevListBox.SelectedIndex = RevListBox.Items.Count - 1;
                }

                dataStr = string.Empty;
            }

            Marshal.FreeHGlobal(pt);
        }
Esempio n. 11
0
        unsafe private void ReceiveDataHandle(UInt32 canId)
        {
            String str = "";
            UInt32 res = new UInt32();

            res = DllAdapte.VCI_GetReceiveNum(mDevType, mDevInd, canId);

            if (res == 0)
            {
                return;
            }

            //res = DllAdapte.VCI_Receive(mDevType, mDevInd, mCanInd, ref mRecObj[0],50, 100);
            UInt32 con_maxlen = 50;
            IntPtr pt         = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * (Int32)con_maxlen);

            res = DllAdapte.VCI_Receive(mDevType, mDevInd, canId, pt, con_maxlen, 100);

            for (UInt32 i = 0; i < res; i++)
            {
                VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(VCI_CAN_OBJ))), typeof(VCI_CAN_OBJ));
                //str = "接收到数据: ";
                str += "  帧ID:0x" + System.Convert.ToString((Int32)obj.ID, 16);
                str += "  帧格式:";

                if (obj.RemoteFlag == 0)
                {
                    str += "数据帧 ";
                }
                else
                {
                    str += "远程帧 ";
                }

                if (obj.ExternFlag == 0)
                {
                    str += "标准帧 ";
                }
                else
                {
                    str += "扩展帧 ";
                }

                if (obj.RemoteFlag == 0)
                {
                    str += "数据: ";
                    byte len = (byte)(obj.DataLen % 9);

                    for (byte j = 0; j < len; j++)
                    {
                        if (j == 0)
                        {
                            str += obj.Data[j].ToString("X2");
                        }
                        else
                        {
                            str += " " + obj.Data[j].ToString("X2"); //System.Convert.ToString(obj.Data[j], 16);
                        }
                    }
                }

                if (mPauseFlag == 0)
                {
                    RevListBox.Items.Add(str);
                    RevListBox.SelectedIndex = RevListBox.Items.Count - 1;
                }

                str = string.Empty;
            }

            Marshal.FreeHGlobal(pt);
        }