static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);
/// <summary> /// 格式化发送数据 /// </summary> /// <param name="value">type ID</param> /// <param name="sendData">发送数据</param> /// <returns></returns> private unsafe VCI_CAN_OBJ FormatSendData(string value, string sendData) { VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ(); sendobj.SendType = m_sendType; sendobj.RemoteFlag = m_framFormat; sendobj.ExternFlag = m_framType; sendobj.ID = System.Convert.ToUInt32(value, 16); int len = (sendData.Length + 1) / 3; sendobj.DataLen = System.Convert.ToByte(len); String strdata = sendData; int i = -1; if (i++ < len - 1) sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); return sendobj; }
/// <summary> /// 发送数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private unsafe void btnSend_Click(object sender, EventArgs e) { if (m_bOpen == 0) { MessageBox.Show("请检查CAN设备是否已打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 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; int i = -1; if (i++ < len - 1) sendobj.Data[0] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[1] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[2] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[3] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[4] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[5] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[6] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (i++ < len - 1) sendobj.Data[7] = System.Convert.ToByte("0x" + strdata.Substring(i * 3, 2), 16); if (VCI_Transmit(m_devtype, m_devind, m_canind, ref sendobj, 1) == 0) { MessageBox.Show("发送失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }