Exemplo n.º 1
0
        private void buttonSetNewBaudRate_Click(object sender, EventArgs e)
        {
            UInt16 NodeAddr;

            if (configDevice())
            {
                //判断当前所选择的节点
                if (this.listViewNodeList.Items.Count <= 0)
                {
                    //当前节点列表中没有节点
                    MessageBox.Show(this, "节点列表中无节点,请先扫描节点!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    if (this.checkBoxAllNode.Checked)
                    {
                        NodeAddr = 0;
                    }
                    else
                    {
                        if (this.listViewNodeList.SelectedIndices[0] < 0)
                        {
                            //没有选择任何节点
                            MessageBox.Show(this, "请先选择节点!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                        else
                        {
                            NodeAddr = UInt16.Parse(this.listViewNodeList.Items[this.listViewNodeList.SelectedIndices[0]].SubItems[0].Text);
                        }
                    }
                }
                USB2CAN.CAN_INIT_CONFIG CAN_InitConfig = new USB2CAN.CAN_INIT_CONFIG();
                int BaudRate = int.Parse(this.comboBoxNewBaudRate.Text.Substring(0, this.comboBoxNewBaudRate.Text.Length - 4));
                if (DevType == DevTypeUSB2CANB)
                {
                    CAN_InitConfig.CAN_BRP = (UInt32)CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].PreScale;
                    CAN_InitConfig.CAN_SJW = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].SJW;
                    CAN_InitConfig.CAN_BS1 = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].BS1;
                    CAN_InitConfig.CAN_BS2 = CANBaudRateTab42M[GetBaudRateIndex(BaudRate)].BS2;
                }
                else
                {
                    CAN_InitConfig.CAN_BRP = (UInt32)CANBaudRateTab[GetBaudRateIndex(BaudRate)].PreScale;
                    CAN_InitConfig.CAN_SJW = CANBaudRateTab[GetBaudRateIndex(BaudRate)].SJW;
                    CAN_InitConfig.CAN_BS1 = CANBaudRateTab[GetBaudRateIndex(BaudRate)].BS1;
                    CAN_InitConfig.CAN_BS2 = CANBaudRateTab[GetBaudRateIndex(BaudRate)].BS2;
                }
                int ret = USB2CAN.CAN_BL_SetNewBaudRate(DevHandles[DeviceIndex], CANIndex, NodeAddr, ref CAN_InitConfig, (UInt32)BaudRate, 100);
                if (ret != USB2CAN.CAN_SUCCESS)
                {
                    MessageBox.Show(this, "设置新波特率失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    this.comboBoxBaudRate.SelectedIndex = this.comboBoxNewBaudRate.SelectedIndex;
                }
            }
        }