⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xldaiodemodlg.cpp

📁 汽车领can总线通讯程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    MessageBox ("Configuration was updated, please check the setting of \"DAIOdemo\" application channel.\n Press OK to proceed."); 
  }

  // now we should get correct application config
  if (xlGetApplConfig ("xlDAIOdemo",          // name of application's channel
                       0,                   // zero based application channel
                       &hwType,             // received type of hardware
                       &hwIndex,            // received hardware index
                       &hwChannel,          // received hardware channel
                       XL_BUS_TYPE_DAIO) 
                       != XL_SUCCESS) {
    AddMessage ("Error: xlGetApplConfig failed.");
    return;
  }

  // retrieve mask for selected channel
  gChannelMask = xlGetChannelMask ((int)hwType, (int)hwIndex, (int)hwChannel);
  if (!gChannelMask){
    AddMessage ("Error: xlGetChannelMask failed.");
    return;
  }

  initAccess = gChannelMask;

  // now we are going to open port for the application
  if (xlOpenPort ( &gPort,                // pointer to port handle
                   "xlDAIOdemo",            // application name
                   gChannelMask,          // selected channel mask
                   &initAccess,           // init access mask request / response
                   1024,                  // receive queue size
                   XL_INTERFACE_VERSION,  // desired xl interface version
                   XL_BUS_TYPE_DAIO)      // we are opening DAIO bus
                   != XL_SUCCESS) {
    AddMessage ("Error: xlOpenPort failed.");
    return;
  }

  // if we have not init access, we should fail here, because than we can only read DAIOdata
  if (gChannelMask != initAccess) {
    AddMessage ("Error: Application cannot get init access.");
    goto lerror;  // from now we should fail safely, because we could have opened port and driver
  }
  if (!gRxHandle) gRxHandle = CreateEvent(NULL,false,false,NULL);

  if (!gRxHandle) {
    AddMessage ("Error: CreateEvent failed, cannot set notification handle.");
    goto lerror;
  }
  
  if (xlSetNotification (gPort, &gRxHandle, 1 ) != XL_SUCCESS) {
    AddMessage ("Error: xlSetNotification failed.");
    goto lerror;
  }

  // we can call configuration here to make sure that after activate channel we will have all settings done
  OnSetConfiguration();

  // following step is going to activate channel and stay prepared for configuration
  if (xlActivateChannel (gPort,                 // application port
                         initAccess,           // desired access mask
                         XL_BUS_TYPE_DAIO,      // we are activating DAIO bus
                         XL_ACTIVATE_RESET_CLOCK)  // timestamp should start from zero
                         != XL_SUCCESS) {
    AddMessage ("Error: xlActivateChannel failed.");
    goto lerror;
  }

  // if we have not init access, we should fail here, because than we can only read DAIOdata
  if (gChannelMask != initAccess) {
    AddMessage ("Error: Application cannot get init access for activation.");
    goto lerror;
  }

  // now we are ready with driver configuration, we have opened channel with DAIO configuration
  // and when bus configuration is updated, we can start measurement

  if (xlGetDriverConfig(&cfg) != XL_SUCCESS) {
    AddMessage ("Error: xlGetDriverConfig failed.");
    goto lerror;
  }

  // now we should display name of the channel and serial number of device we selected
  for (pos = 0; pos < cfg.channelCount; pos++) {
    if (cfg.channel[pos].channelMask == gChannelMask){
      m_SelectedChannelName.Format("%s (s.n. %d)", cfg.channel[pos].name, cfg.channel[pos].serialNumber);
    }
  }
  
  // first reset all values to starting state
  m_DigitalStateList.ResetContent();
  m_DigitalOut1.SetCheck(0);
  m_DigitalOut2.SetCheck(0);
  m_DigitalOut3.SetCheck(0);
  m_DigitalOut4.SetCheck(0);
  m_Analog1 = 0;
  m_Analog2 = 0;
  m_Analog3 = 0;
  m_Analog4 = 0;
  gLastDigital = 0;
  memset(gLastAnalog, 0, sizeof(gLastAnalog));

  m_StartCtrl.EnableWindow (FALSE);
  m_StopCtrl.EnableWindow (TRUE);

  UpdateData(FALSE);
  gChannelActivated = TRUE;
  return;
lerror:
  xlClosePort(gPort);
  xlCloseDriver();

}

/////////////////////////////////////////////////////////////////////////////
// OnStart - stops the channel with DAIO bus
void CXlDAIOdemoDlg::OnStop() {
  if (!gChannelActivated) return;

  // close driver configuration and free resources to other applications
  if (gRxHandle) CloseHandle(gRxHandle);
  gRxHandle = NULL;
  xlDeactivateChannel(gPort, gChannelMask);

  xlClosePort(gPort);
  
  xlCloseDriver();    

  gChannelActivated = FALSE;
  m_SelectedChannelName = "No channel activated";
  m_StopCtrl.EnableWindow (FALSE);
  m_StartCtrl.EnableWindow (TRUE);
  UpdateData(FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// OnSetConfiguration - function sends current configuration to the device
void CXlDAIOdemoDlg::OnSetConfiguration() {
  unsigned int measure_delay = 0;
  UpdateData(TRUE);

  switch (m_MeasurementFrequency) {
    case 1: measure_delay = 1000;  //    1 Hz
      break;
    case 2: measure_delay =  100;  //   10 Hz
      break;
    case 3: measure_delay =   10;  //  100 Hz
      break;
    case 4: measure_delay =    1;  // 1000 Hz
      break;
  }

  if (xlDAIOSetMeasurementFrequency ( gPort, 
                                      gChannelMask,
                                      measure_delay)
                                      != XL_SUCCESS) {
    AddMessage ("Error: xlDAIOSetMeasurementFrequency failed.");
    return;
  }

  // now we will setup digital channels to output, values we can read anyway
  if (xlDAIOSetDigitalParameters (gPort, gChannelMask, 0x00, 0xff) != XL_SUCCESS) {
    AddMessage ("Error: xlDAIOSetDigitalParameters failed.");
    return;
  }

  // we will use two analog outputs (port 1 and 2) and two analog inputs
  // for all of them input range to 32V
  if (xlDAIOSetAnalogParameters (gPort, gChannelMask, 0x0c, 0x03, 0x0f) != XL_SUCCESS) {
    AddMessage ("Error: xlDAIOSetAnalogParameters failed.");
    return;
  }

  AddMessage ("Configuration was set successfully.");
}


/////////////////////////////////////////////////////////////////////////////
// OnSetAnalog - updates analog outputs
void CXlDAIOdemoDlg::OnSetAnalog() {
  UpdateData(TRUE);

  if (!gChannelActivated) {
    AddMessage ("Warning! OnSetAnalog: channel is not activated.");
    return;
  }

  if (xlDAIOSetAnalogOutput (gPort, gChannelMask, m_Analog1, m_Analog2, m_Analog3, m_Analog4) != XL_SUCCESS) {
    AddMessage ("Error: OnSetAnalog: xlDAIOSetAnalogOutput failed.");
  }

}

/////////////////////////////////////////////////////////////////////////////
// OnSetDigital - updates digital outputs
void CXlDAIOdemoDlg::OnSetDigital1() {
  unsigned int pattern = 0;
  UpdateData(TRUE);

  if (!gChannelActivated) {
    AddMessage ("Warning! OnSetDigital1: channel is not activated.");
    return;
  }

  if (m_DigitalOut1.GetCheck()) pattern = (1 << 0);

  if (xlDAIOSetDigitalOutput (gPort, gChannelMask, 0x1, pattern) != XL_SUCCESS) {
    AddMessage ("Error: OnSetDigital1: xlDAIOSetDigitalOutput failed.");
  }
}

void CXlDAIOdemoDlg::OnSetDigital2() {
  unsigned int pattern = 0;
  UpdateData(TRUE);

  if (!gChannelActivated) {
    AddMessage ("Warning! OnSetDigital2: channel is not activated.");
    return;
  }

  if (m_DigitalOut2.GetCheck()) pattern = (1 << 1);

  if (xlDAIOSetDigitalOutput (gPort, gChannelMask, 0x2, pattern) != XL_SUCCESS) {
    AddMessage ("Error: OnSetDigital2: xlDAIOSetDigitalOutput failed.");
  }
}

void CXlDAIOdemoDlg::OnSetDigital3() {
  unsigned int pattern = 0;
  UpdateData(TRUE);

  if (!gChannelActivated) {
    AddMessage ("Warning! OnSetDigital3: channel is not activated.");
    return;
  }

  if (m_DigitalOut3.GetCheck()) pattern = (1 << 2);

  if (xlDAIOSetDigitalOutput (gPort, gChannelMask, 0x4, pattern) != XL_SUCCESS) {
    AddMessage ("Error: OnSetDigital3: xlDAIOSetDigitalOutput failed.");
  }
}

void CXlDAIOdemoDlg::OnSetDigital4() {
  unsigned int pattern = 0;
  UpdateData(TRUE);

  if (!gChannelActivated) {
    AddMessage ("Warning! OnSetDigital4: channel is not activated.");
    return;
  }

  if (m_DigitalOut4.GetCheck()) pattern = (1 << 3);

  if (xlDAIOSetDigitalOutput (gPort, gChannelMask, 0x8, pattern) != XL_SUCCESS) {
    AddMessage ("Error: OnSetDigital4: xlDAIOSetDigitalOutput failed.");
  }
}

/////////////////////////////////////////////////////////////////////////////
// OnMeasureNow - forces manual measurement 
void CXlDAIOdemoDlg::OnMeasureNow() {
  UpdateData(TRUE);
  if (!gChannelActivated) {
    AddMessage ("Warning! OnMeasureNow: channel is not activated.");
    return;
  }

  if (xlDAIORequestMeasurement (gPort, gChannelMask) != XL_SUCCESS) {
    AddMessage ("Error: OnMeasureNow: xlDAIORequestMeasurement failed.");
  }
}


/////////////////////////////////////////////////////////////////////////////
// OnReleasedcaptureSlider1 - user updated pwm setting
void CXlDAIOdemoDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) {
  unsigned int frequency = 0;
	*pResult = 0;
  UpdateData(TRUE);
  if (!gChannelActivated) {
    AddMessage ("Warning! OnReleasedcaptureSlider1: channel is not activated.");
    return;
  }

  //if (m_PwmFrequency == 0) return; // pwm is disabled

  if (m_PwmFrequency == 1) frequency = 100;
  if (m_PwmFrequency == 2) frequency = 5000;
  if (m_PwmFrequency == 3) frequency = 20000;
  if (m_PwmFrequency == 4) frequency = 40000;

  if (xlDAIOSetPWMOutput (gPort, gChannelMask, frequency, m_PwmValue) != XL_SUCCESS) {
    AddMessage ("Error: OnMeasureNow: xlDAIORequestMeasurement failed.");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -