unsafe private CAN_FRAME[] GetCANFrames() { CAN_SEND_MODE mSendMode = (CAN_SEND_MODE)CAN.CAN_SEND_MODE_LIST[this.cbxSendMode.SelectedItem.ToString()]; CAN_FRAME_TYPE mFrameType = (CAN_FRAME_TYPE)CAN.CAN_FRAME_TYPE_LIST[this.cbxFrameType.SelectedItem]; CAN_FRAME_FORMAT mFrameFormat = (CAN_FRAME_FORMAT)CAN.CAN_FRAME_FORMAT_LIST[this.cbxFrameFormat.SelectedItem]; uint id = Convert.ToUInt32(tbxFrameID.Text, 16); uint mSendNum = Convert.ToUInt32(this.tbxSendNum.Text, 10); double mSendInterval = Convert.ToDouble(this.tbxSendInterval.Text); string[] sDataArr = new Regex("[\\s]+").Replace(this.tbxFrameData.Text.Trim(), " ").Split(' '); byte mDataLen = (byte)sDataArr.Length; StringBuilder sbBuilder = new StringBuilder("0x"); for (int index = mDataLen - 1; index >= 0; index--) { sbBuilder.Append(sDataArr[index].PadLeft(2, '0')); } ulong data = Convert.ToUInt64(sbBuilder.ToString(), 16); bool incID = chbxIncID.Checked; bool incData = chbxIncData.Checked; CAN_FRAME[] pCANFrames = new CAN_FRAME[mSendNum]; for (uint index = 0; index < mSendNum; index++) { CAN_OBJ pCANObj = new CAN_OBJ(); if (!incID) { pCANObj.ID = id; } else { pCANObj.ID = id + index; if (mFrameFormat == CAN_FRAME_FORMAT.STANDARD_FRAME && pCANObj.ID > CAN.STANDARD_FRAME_ID_MAXIMUM) { pCANObj.ID = CAN.STANDARD_FRAME_ID_MAXIMUM; } else if (mFrameFormat == CAN_FRAME_FORMAT.EXTENDED_FRAME && pCANObj.ID > CAN.EXTENDED_FRAME_ID_MAXIMUM) { pCANObj.ID = CAN.EXTENDED_FRAME_ID_MAXIMUM; } } //pCANObj.ID = incID ? id + index : id; pCANObj.DataLen = mDataLen; ulong ulData = incData ? data + index : data; Marshal.Copy(BitConverter.GetBytes(ulData), 0, (IntPtr)pCANObj.Data, (int)CAN.FRAME_DATA_LENGTH_MAXIMUM); pCANObj.SendType = (byte)mSendMode; pCANObj.RemoteFlag = (byte)mFrameType; pCANObj.ExternFlag = (byte)mFrameFormat; pCANObj.TimeFlag = (byte)CAN_FRAME_TIME_FLAG.INVALID; pCANObj.TimeStamp = 0; pCANFrames[index] = new CAN_FRAME(pCANObj, DateTime.Now, CAN_FRAME_DIRECTION.SEND, CAN_FRAME_STATUS.UNKNOWN); } return(pCANFrames); }
public static string FindCANSendModeKey(CAN_SEND_MODE mCANSendMode) { return(FindKey(mCANSendMode, CAN_SEND_MODE_LIST)); }
private bool Check() { //get send mode & frame type & frame format CAN_SEND_MODE mSendMode = (CAN_SEND_MODE)CAN.CAN_SEND_MODE_LIST[this.cbxSendMode.SelectedItem.ToString()]; CAN_FRAME_TYPE mFrameType = (CAN_FRAME_TYPE)CAN.CAN_FRAME_TYPE_LIST[this.cbxFrameType.SelectedItem]; CAN_FRAME_FORMAT mFrameFormat = (CAN_FRAME_FORMAT)CAN.CAN_FRAME_FORMAT_LIST[this.cbxFrameFormat.SelectedItem]; //get & check id uint id; try { id = Convert.ToUInt32(tbxFrameID.Text, 16); if (mFrameFormat == CAN_FRAME_FORMAT.STANDARD_FRAME && id > CAN.STANDARD_FRAME_ID_MAXIMUM) { MessageBox.Show(string.Format("标准帧ID超过最大值[0x{0}].", CAN.STANDARD_FRAME_ID_MAXIMUM.ToString("x"))); return(false); } else if (mFrameFormat == CAN_FRAME_FORMAT.EXTENDED_FRAME && id > CAN.EXTENDED_FRAME_ID_MAXIMUM) { MessageBox.Show(string.Format("扩展帧ID超过最大值[0x{0}].", CAN.STANDARD_FRAME_ID_MAXIMUM.ToString("x"))); return(false); } } catch (Exception e) { MessageBox.Show("ID格式不正确."); Logger.Info(string.Format("channel[{0}] send frame id format error: [{1}].", channel.ChannelName, tbxFrameID.Text), e); return(false); } //get & check send num uint mSendNum; try { mSendNum = Convert.ToUInt32(this.tbxSendNum.Text); if (mSendNum < SEND_NUM_MINIMUM) { MessageBox.Show(string.Format("发送次数不能小于[{0}].", SEND_NUM_MINIMUM)); return(false); } else if (mSendNum > SEND_NUM_MAXIMUM) { MessageBox.Show(string.Format("发送次数不能超过[{0}].", SEND_NUM_MAXIMUM)); return(false); } } catch (Exception e) { MessageBox.Show("发送次数格式不正确."); Logger.Info(string.Format("channel[{0}] send num format error: [{1}].", channel.ChannelName, tbxSendNum.Text), e); return(false); } //get & check send interval double mSendInterval; try { mSendInterval = Convert.ToDouble(this.tbxSendInterval.Text); if (mSendInterval < SEND_INTERVAL_MINIMUM) { MessageBox.Show(string.Format("发送时间间隔不能小于[{0}]ms.", SEND_INTERVAL_MINIMUM)); return(false); } else if (mSendInterval > SEND_INTERVAL_MAXIMUM) { MessageBox.Show(string.Format("发送时间间隔不能超过[{0}]ms.", SEND_INTERVAL_MAXIMUM)); return(false); } } catch (Exception e) { MessageBox.Show("发送时间间隔格式不正确."); Logger.Info(string.Format("channel[{0}] send time interval error: [{1}].", channel.ChannelName, tbxSendInterval.Text), e); return(false); } //get & check data try { string[] dataArr = new Regex("[\\s]+").Replace(this.tbxFrameData.Text.Trim(), " ").Split(' '); if (dataArr.Length > CAN.FRAME_DATA_LENGTH_MAXIMUM) { MessageBox.Show(string.Format("发送数据长度不能超过[{0}].", CAN.FRAME_DATA_LENGTH_MAXIMUM)); return(false); } foreach (string dataStr in dataArr) { if (dataStr.Length > 2) { MessageBox.Show(string.Format("发送数据格式不正确:[{0}].", dataStr)); return(false); } } } catch (Exception e) { MessageBox.Show("发送数据格式不正确."); Logger.Info(string.Format("channel[{0}] send data format error: [{1}].", channel.ChannelName, tbxFrameData.Text), e); return(false); } return(true); }