示例#1
0
        /// <summary>
        /// 将DataFrame数据转为LitViewItem
        /// </summary>
        private void DataFrameToListview(DataFrame dat)
        {
            string str1, str2;
            if (dat.IsExternFrame)
                str1 = "扩展帧";
            else
                str1 = "标准帧";
            if (dat.IsRemoteFrame)
                str2 = "遥控帧";
            else
                str2 = "数据帧";

            ListViewItem lt = new ListViewItem(new string[] {
                dat.ReceiveTime.ToString(),str2,str1,
               /*Convert.ToString(dat.ID,16)*/
                dat.ID.ToString("X8")
            /*string.Format("{0,8:X8}",dat.ID)*/,dat.DataLength.ToString(),
                BitConverter .ToString(dat.Date).Replace('-',' ')});
            listView1.Items.Add(lt);
        }
示例#2
0
        /// <summary>
        /// 发送一帧CAN数据
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            uint id = ConvertToID(textBox1.Text);
            if (id == 0xFFFFFFFF)
                return;

            byte[] data = null;
            byte len = 0;
            //只有数据帧才会用数据
            if (!(bool)comboBox5.SelectedValue)
            {
                data = ConvertToData(textBox2.Text);
                if (data == null)
                    return;
                else
                    len = (byte)data.Length;
            }
            else //摇控帧无数据只有,请求回应的数据长度
            {
                try
                {
                    byte i = Convert.ToByte(textBox2.Text);
                    if (i > 8)
                        throw new Exception("请求的数据长度不能大于8");
                }
                catch
                {
                    MessageBox.Show("数据长度应是0-8之间的数", "警告", MessageBoxButtons.OK,
                        MessageBoxIcon.Stop);
                    return;
                }
            }

            DataFrame send = new DataFrame(id, data, len, (bool)comboBox5.SelectedValue,
                (bool)comboBox6.SelectedValue, (CAN_SendType)comboBox4.SelectedValue);
            bool flag = CANChannel.Send(send);
            if (!flag)
            {
                //MessageBox.Show("发送该帧数失败", "提示:",
                //    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                ListViewItem lt = new ListViewItem(new string[]{
                    "发送->失败",comboBox5.Text,comboBox6.Text,
                    send.ID.ToString("X8"),send.DataLength.ToString(),BitConverter.ToString(send.Date)});
                lt.ForeColor = Color.Red;
                listView1.Items.Add(lt);

                ErrorCode err = CANChannel.GetErrorCode();
                if (err != null)
                    MessageBox.Show(UsbCanDevice.ErrorCodeDict[err.Code], "提示:",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ListViewItem lt = new ListViewItem(new string[]{
                    "发送->成功",comboBox5.Text,comboBox6.Text,
                    send.ID.ToString("X8"),send.DataLength.ToString(),BitConverter.ToString(send.Date)});
                lt.ForeColor = Color.Blue;

                listView1.Items.Add(lt);
            }
        }
示例#3
0
            /// <summary>
            /// 把结构CAN_DataFrame转为DataFrame对象
            /// </summary>
            private DataFrame Conver2ClassDataFrame(Like.CAN_DataFrame data)
            {
                bool remote, exter;
                if (data.bExternFlag == 1)
                    exter = true;
                else
                    exter = false;
                if (data.bRemoteFlag == 1)
                    remote = true;
                else
                    remote = false;

                uint timeflag = DateTime2Uinx(this.StartTime) + (data.uTimeFlag / 1000000);
                byte[] dat = new byte[data.nDataLen];
                Array.Copy(data.arryData, 0, dat, 0, data.nDataLen);

                DataFrame res = new DataFrame(data.uID, dat, data.nDataLen,
                    remote, exter, (CAN_SendType)data.nSendType, timeflag);

                return res;
            }
示例#4
0
            /// <summary>
            /// 把DataFrame对象转为结构本CAN_DataFrame
            /// </summary>
            private Like.CAN_DataFrame Conver2StructDataFrame(DataFrame data)
            {
                Like.CAN_DataFrame res = new Like.CAN_DataFrame();
                res.arryData = new byte[8];
                Array.Copy(data.Date, res.arryData, data.Date.Length);
                res.nDataLen = data.DataLength;
                res.nSendType = (byte)data.SendType;
                res.uID = data.ID;
                if (data.IsExternFrame)
                    res.bExternFlag = 1;
                else
                    res.bExternFlag = 0;
                if (data.IsRemoteFrame)
                    res.bRemoteFlag = 1;
                else
                    res.bRemoteFlag = 0;
                res.uTimeFlag = 0;

                return res;
            }
示例#5
0
 /// <summary>
 /// 同步发送数据
 /// </summary>
 public bool Send(DataFrame sendData)
 {
     //if (sendData.ID == 0xFFFFFFFF) return false;
     if (this.IsRuning)
     {
         Like.CAN_DataFrame dat = Conver2StructDataFrame(sendData);
         uint res = Like.CAN_ChannelSend(this._handle, this.Channel, ref dat);
         if (res == 1)
         {
             Debug.WriteLine("发送\t一帧数据\t成功", DateTime.Now.ToString());
             //触发数据发送事件
             if (EventSendHander != null)
             {
                 EventSendHander(this, sendData);
             }
             return true;
         }
         else
         {
             Debug.WriteLine("发送\t一帧数据\t失败");
             return false;
         }
     }
     else
     {
         Debug.WriteLine("对应的CAN通道未打开,无法发送数据");
         System.Windows.Forms.MessageBox.Show(string.Format("CAN{0}未开启,发送数据前请先开启CAN通道", this.Channel),
             "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
         return false;
     }
 }