User Tools

Site Tools

blog:2021-09-13_zlg_周立功_can_usb_send_message_test



2021-09-13 ZLG (周立功) CAN USB Send Message Test

  • Here is the test code that can send message to ZLG CAN USB in C#.
  • The test code is in button1_click event as follows.

Code

  • private void button1_Click(object sender, EventArgs e)
    {
        // Test Send Command
    
        /*
        uint id = (uint)System.Convert.ToInt32(textBox_ID.Text, 16);
        string data = textBox_senddata.Text;
        int frame_type_index = comboBox_frametype.SelectedIndex;
        int protocol_index = comboBox_protocol.SelectedIndex;
        int send_type_index = comboBox_sendtype.SelectedIndex;
        int canfd_exp_index = comboBox_canfd_exp.SelectedIndex;
        uint result; //发送的帧数
    
        ZCAN_Transmit_Data can_data = new ZCAN_Transmit_Data();
        can_data.frame.can_id = MakeCanId(id, frame_type_index, 0, 0);
        can_data.frame.data = new byte[8];
        can_data.frame.can_dlc = (byte)SplitData(data, ref can_data.frame.data, CAN_MAX_DLEN);
        can_data.transmit_type = (uint)send_type_index;
        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(can_data));
        Marshal.StructureToPtr(can_data, ptr, true);
        result = Method.ZCAN_Transmit(channel_handle_, ptr, 1);
        Marshal.FreeHGlobal(ptr);
    
        if (result != 1)
        {
            MessageBox.Show("发送数据失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            AddErr();
        }
        */
    
        uint id = (uint)System.Convert.ToInt32("00000100", 16);
        string data = "A0 FF 1F 04 00 00 00 00";
        int frame_type_index = 0;
        int protocol_index = 0;
        int send_type_index = 0;
        int canfd_exp_index = 0;
        uint result = 1; //发送的帧数
    
        ZCAN_Transmit_Data can_data = new ZCAN_Transmit_Data();
        can_data.frame.can_id = MakeCanId(id, frame_type_index, 0, 0);
        can_data.frame.data = new byte[8];
        can_data.frame.can_dlc = (byte)SplitData(data, ref can_data.frame.data, CAN_MAX_DLEN);
        can_data.transmit_type = (uint)send_type_index;
        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(can_data));
        Marshal.StructureToPtr(can_data, ptr, true);
        result = Method.ZCAN_Transmit(channel_handle_, ptr, 1);
        Marshal.FreeHGlobal(ptr);
    
        if (result != 1)
        {
            MessageBox.Show("发送数据失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            AddErr();
        }
    }
    
    public uint MakeCanId(uint id, int eff, int rtr, int err)//1:extend frame 0:standard frame
    {
        uint ueff = (uint)(!!(Convert.ToBoolean(eff)) ? 1 : 0);
        uint urtr = (uint)(!!(Convert.ToBoolean(rtr)) ? 1 : 0);
        uint uerr = (uint)(!!(Convert.ToBoolean(err)) ? 1 : 0);
        return id | ueff << 31 | urtr << 30 | uerr << 29;
    }
    
            //拆分text到发送data数组
    private int SplitData(string data, ref byte[] transData, int maxLen)
    {
        string[] dataArray = data.Split(' ');
        for (int i = 0; (i < maxLen) && (i < dataArray.Length); i++)
        {
            transData[i] = Convert.ToByte(dataArray[i].Substring(0, 2), 16);
        }
        return dataArray.Length;
    }

TAGS

  • 371 person(s) visited this page until now.

Permalink blog/2021-09-13_zlg_周立功_can_usb_send_message_test.txt · Last modified: 2021/09/13 16:39 by jethro

oeffentlich