Пример #1
0
 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (m_bOpen == 1)
     {
         VciNativeMethods.VCI_CloseDevice(m_devtype, m_devind);
     }
 }
Пример #2
0
 private void button_ResetCAN_Click(object sender, EventArgs e)
 {
     if (m_bOpen == 0)
     {
         return;
     }
     VciNativeMethods.VCI_ResetCAN(m_devtype, m_devind, m_canind);
     //button_StartCAN.Enabled = true;
 }
Пример #3
0
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (m_bOpen == 1)
            {
                VciNativeMethods.VCI_CloseDevice(m_devtype, m_devind);
                m_bOpen = 0;
            }
            else
            {
                m_devtype = (uint)m_arrdevtype[comboBox_devtype.SelectedIndex];

                m_devind = (UInt32)comboBox_DevIndex.SelectedIndex;
                m_canind = (UInt32)comboBox_CANIndex.SelectedIndex;
                var rc = VciNativeMethods.VCI_OpenDevice(m_devtype, m_devind, 0);
                if (rc == 0)
                {
                    MessageBox.Show("打开设备失败,请检查设备类型和设备索引号是否正确", "错误",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                //rc = VciNativeMethods.VCI_OpenDevice(m_devtype, m_devind, 0);
                //var error = new VCI_ERR_INFO();
                //VciNativeMethods.VCI_ReadErrInfo(m_devtype, m_devind, 0, ref error);

                rc = VciNativeMethods.VCI_ClearBuffer(m_devtype, m_devind, m_canind);

                m_bOpen = 1;
                var config = new VCI_INIT_CONFIG();
                config.AccCode = Convert.ToUInt32("0x" + textBox_AccCode.Text, 16);
                config.AccMask = Convert.ToUInt32("0x" + textBox_AccMask.Text, 16);
                config.Timing0 = Convert.ToByte("0x" + textBox_Time0.Text, 16);
                config.Timing1 = Convert.ToByte("0x" + textBox_Time1.Text, 16);
                config.Filter  = (FilterType)comboBox_Filter.SelectedIndex;
                config.Mode    = (WorkMode)comboBox_Mode.SelectedIndex;
                VciNativeMethods.VCI_InitCAN(m_devtype, m_devind, m_canind, ref config);

                // 读取板卡信息
                var boardInf = new VCI_BOARD_INFO();
                VciNativeMethods.VCI_ReadBoardInfo(m_devtype, m_devind, ref boardInf);
                //var serialNo = Encoding.ASCII.GetString(boardInf.str_Serial_Num, 0, boardInf.str_Serial_Num.Length);
                //Console.WriteLine(serialNo);
            }

            buttonConnect.Text = m_bOpen == 1 ? "断开" : "连接";
            timer_rec.Enabled  = m_bOpen == 1;
            //button_StartCAN.Enabled = m_bOpen == 1;
            //button_StopCAN.Enabled = m_bOpen == 1;
        }
Пример #4
0
 private void button_StartCAN_Click(object sender, EventArgs e)
 {
     try
     {
         if (m_bOpen == 0)
         {
             return;
         }
         VciNativeMethods.VCI_StartCAN(m_devtype, m_devind, m_canind);
         //button_StartCAN.Enabled = false;
     }
     catch (System.Exception ex)
     {
         //button_StartCAN.Enabled = true;
         MessageBox.Show(ex.Message);
     }
 }
Пример #5
0
        /// <summary>
        /// 接收线程。
        /// </summary>
        private void timer_rec_Tick(object sender, EventArgs e)
        {
            var count = VciNativeMethods.VCI_GetReceiveNum(m_devtype, m_devind, m_canind);

            if (count == 0)
            {
                return;
            }

            var sw = new Stopwatch();

            sw.Start();

            /////////////////////////////////////

            for (int i = 0; i < count; i++)
            {
                var canObj = new VCI_CAN_OBJ();
                //var ptr = GCHandle.ToIntPtr(GCHandle.Alloc(canObj[0]));
                var len = VciNativeMethods.VCI_Receive(m_devtype, m_devind, m_canind, ref canObj, 1, 100);
                if (len <= 0)
                {
                    //注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
                    //千万不能省略这一步(即使你可能不想知道错误码是什么)
                    //VCI_ReadErrInfo(m_devtype, this.m_devind, this.m_canind, ref errInfo);
                }

                ////////////////////////////////////////////////////////
                var sb = new StringBuilder(128);
                sb.AppendFormat("接收到数据:  帧ID:0x{0:X2}  帧格式:", canObj.ID);
                sb.AppendFormat("", (canObj.RemoteFlag == 0) ? "数据帧 " : "远程帧 ", (canObj.ExternFlag == 0) ? "标准帧 " : "扩展帧 ");

                if (canObj.RemoteFlag == 0)
                {
                    sb.AppendFormat("数据: {0}", string.Join(" ", canObj.Data.Take(canObj.DataLen).Select(p => string.Format("{0:X2}", p))));

                    listBox_Info.Items.Add(sb.ToString());
                    listBox_Info.SelectedIndex = listBox_Info.Items.Count - 1;
                }
            }


            sw.Stop();
            Console.WriteLine("接收耗时 = {0} 秒", sw.Elapsed.TotalSeconds);
        }
Пример #6
0
        private void button_Send_Click(object sender, EventArgs e)
        {
            if (m_bOpen == 0)
            {
                return;
            }

            var id         = System.Convert.ToUInt32("0x" + textBox_ID.Text, 16);
            var sendType   = (SendMode)comboBox_SendType.SelectedIndex;
            var remoteFlag = comboBox_FrameFormat.SelectedIndex; // 是否为远程帧?
            var externFlag = comboBox_FrameType.SelectedIndex;   // 是否为扩展帧?
            var sendTimers = uint.Parse(textBox1.Text);
            var data       = HelperTools.SplitHexText(textBox_Data.Text);

            var sendobj = new VCI_CAN_OBJ[sendTimers];

            for (int j = 0; j < sendTimers; j++)
            {
                sendobj[j].SendType   = sendType;
                sendobj[j].RemoteFlag = (byte)remoteFlag;
                sendobj[j].ExternFlag = (byte)externFlag;
                sendobj[j].ID         = id;
                sendobj[j].DataLen    = System.Convert.ToByte(data.Length % 9);
                sendobj[j].Data       = data;
            }

            var sw = new Stopwatch();

            sw.Start();
            var res = VciNativeMethods.VCI_Transmit(m_devtype, m_devind, m_canind, sendobj, sendTimers);

            if (res == 0)
            {
                MessageBox.Show("发送失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            sw.Stop();
            Console.WriteLine("发送耗时 = {0} 秒", sw.Elapsed.TotalSeconds);
        }