📄 pcm3780dlg.cpp
字号:
// PCM3780Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "PCM3780.h"
#include "PCM3780Dlg.h"
#include "..\..\..\..\include\driver.h"
enum TIMER {IntTimer = 1, ReadTimer};
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPCM3780Dlg dialog
CPCM3780Dlg::CPCM3780Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CPCM3780Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPCM3780Dlg)
m_Scantime = 0;
m_DioData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPCM3780Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPCM3780Dlg)
DDX_Control(pDX, IDC_BUTTONSTOP, m_btnStop);
DDX_Control(pDX, IDC_BUTTONSTART, m_btnStart);
DDX_Control(pDX, IDC_LISTINTSET, m_ListInterrupt);
DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
DDX_Control(pDX, IDC_COMBOPORT, m_ComboPort);
DDX_Text(pDX, IDC_EDITSCANTIME, m_Scantime);
DDX_Text(pDX, IDC_STATICDATA, m_DioData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPCM3780Dlg, CDialog)
//{{AFX_MSG_MAP(CPCM3780Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTSELDEV, OnBtseldev)
ON_BN_CLICKED(IDC_BUTTONEXIT, OnButtonexit)
ON_BN_CLICKED(IDC_BUTTONSTART, OnButtonstart)
ON_WM_TIMER()
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTINTSET, OnItemchangedListintset)
ON_BN_CLICKED(IDC_BUTTONDISABLE, OnButtondisable)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTONSTOP, OnButtonstop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPCM3780Dlg message handlers
BOOL CPCM3780Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_ListInterrupt.SetExtendedStyle( m_ListInterrupt.GetExtendedStyle()
| LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );
m_ListInterrupt.InsertColumn(0, "Interrupt Channel", LVCFMT_CENTER, 90);
m_ListInterrupt.InsertColumn(1, "Event count/Sec", LVCFMT_CENTER, 80);
pThreadObject =NULL;
m_lDeviceHandle = NULL;
dwEventFlag = 0;
OnBtseldev();
return TRUE; // return TRUE unless you set the focus to a control
}
void CPCM3780Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPCM3780Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPCM3780Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPCM3780Dlg::OnBtseldev()
{
// TODO: Add your control notification handler code here
ULONG ulDeviceNum;
UCHAR DeviceName[150];
PT_DeviceGetFeatures ptGetFeatures;
DEVFEATURES devFeatures;
CString sText;
OnButtonstop();
if (pThreadObject != NULL){
OnButtondisable();
}
if (m_lDeviceHandle != NULL){
DRV_DeviceClose(&m_lDeviceHandle);
m_lDeviceHandle = NULL;
}
//1. select device
m_dwErrCde = DRV_SelectDevice(m_hWnd, FALSE, &ulDeviceNum, DeviceName);
if (m_dwErrCde != SUCCESS) {
DRV_GetErrorMessage(m_dwErrCde,(LPSTR)m_szErrMsg);
MessageBox((LPCSTR)m_szErrMsg, "Driver Message", MB_OK);
return ;
}
//2. open device
m_dwErrCde = DRV_DeviceOpen(ulDeviceNum, &m_lDeviceHandle);
if (m_dwErrCde != SUCCESS) {
DRV_GetErrorMessage(m_dwErrCde,(LPSTR)m_szErrMsg);
MessageBox((LPCSTR)m_szErrMsg, "Driver Message", MB_OK);
return ;
}
//3. retrieve device feature
ptGetFeatures.buffer = &devFeatures;
ptGetFeatures.size = sizeof(DEVFEATURES);
m_dwErrCde = DRV_DeviceGetFeatures( m_lDeviceHandle, &ptGetFeatures );
if (m_dwErrCde != SUCCESS) {
DRV_GetErrorMessage(m_dwErrCde,(LPSTR)m_szErrMsg);
MessageBox((LPCSTR)m_szErrMsg, "Driver Message", MB_OK);
return ;
}
//4. store the device features
m_usMaxDIChan = devFeatures.usMaxDIChl;
m_dwBordID = devFeatures.dwBoardID;
m_usDIOProg = devFeatures.usDIOPort;
//5. update the UI
m_DeviceName.SetWindowText((LPCTSTR)DeviceName);
m_ListInterrupt.DeleteAllItems();
if (m_usMaxDIChan > 0){
for (int nRow = 0; nRow < m_usMaxDIChan; nRow++)
{
sText.Format( "P%X_IDI%02d", nRow / 8, nRow % 8 );
m_ListInterrupt.InsertItem(nRow, sText);
}
m_ComboPort.Clear();
for (nRow = 0; nRow < m_usMaxDIChan/8; nRow++)
{
sText.Format("Port %X", nRow );
m_ComboPort.AddString(sText);
}
m_ComboPort.SetCurSel(0);
}
for (int i=0; i<24; i++){
dwEventCount[i] = 0;
}
dwEventFlag = 0;
UpdateData(FALSE);
}
void CPCM3780Dlg::OnButtonexit()
{
// TODO: Add your control notification handler code here
if (m_lDeviceHandle) {
DRV_DeviceClose(&m_lDeviceHandle);
m_lDeviceHandle = NULL;
}
CDialog::OnOK();
}
void CPCM3780Dlg::OnButtonstart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
PT_DioSetPortMode ptDioSetPortMode;
m_btnStart.EnableWindow(FALSE);
m_btnStop.EnableWindow(TRUE);
if (m_usDIOProg) {
for (USHORT i=0; i<m_usMaxDIChan/8; i++){
ptDioSetPortMode.port = i;
ptDioSetPortMode.dir = 0;
DRV_DioSetPortMode(m_lDeviceHandle, &ptDioSetPortMode);
}
}
SetTimer(ReadTimer,m_Scantime,NULL);
}
void CPCM3780Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if( nIDEvent == ReadTimer )
{
USHORT DIValue;
CString str;
PT_DioReadPortByte ptDioReadPortByte;
ptDioReadPortByte.port = m_ComboPort.GetCurSel();
ptDioReadPortByte.value = &DIValue;
DRV_DioReadPortByte( m_lDeviceHandle, &ptDioReadPortByte );
str.Format("%02X (H)", DIValue);
m_DioData = str;
}
if( nIDEvent == IntTimer )
{
float ratio;
char szBuf[20];
dwCurrentTime = GetTickCount();
if ((dwTime = dwCurrentTime - dwStartTime) > 1000)
{
dwStartTime = dwCurrentTime;
for(int i = 0; i < 24; i++)
{
ratio = (float)(((float)dwEventCount[i] / (float)dwTime) * 1000.0);
sprintf( szBuf, "%0.1f", ratio );
m_ListInterrupt.SetItemText( i, 1, szBuf);
dwEventCount[i]=0;
}
}
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
//DEL void CPCM3780Dlg::OnCancelMode()
//DEL {
//DEL CDialog::OnCancelMode();
//DEL
//DEL // TODO: Add your message handler code here
//DEL
//DEL }
void CPCM3780Dlg::OnItemchangedListintset(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
BOOL bCheck;
DWORD dwLabel;
PT_EnableEvent EventSetting;
bCheck = m_ListInterrupt.GetCheck( pNMListView->iItem );
if (bCheck){
dwLabel = 1;
}else{
dwLabel = 0;
}
if (!m_lDeviceHandle) {
*pResult = 0;
return;
}
//1. prevent the undesirable access at the initialization of the app
if ((dwEventFlag & (0x01<<pNMListView->iItem)) == (dwLabel<<pNMListView->iItem)) {
*pResult = 0;
return;
}
//2. enable/disable event
if( pNMListView->iItem >= 0 )
{
EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + pNMListView->iItem;
if( bCheck )
{
EventSetting.Enabled = TRUE;
EventSetting.Count = 1;
dwEventFlag = dwEventFlag | (0x01<<pNMListView->iItem);
dwEventCount[pNMListView->iItem] = 0;
}
else
{
EventSetting.Enabled = FALSE;
EventSetting.Count = 0;
dwEventFlag = dwEventFlag & ~(0x01<<pNMListView->iItem);
dwEventCount[pNMListView->iItem] = 0;
}
m_dwErrCde = DRV_EnableEvent( m_lDeviceHandle, &EventSetting );
if ( m_dwErrCde != 0)
{
DRV_GetErrorMessage( m_dwErrCde, (LPSTR)m_szErrMsg );
MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
return;
}
//3. create the thread to check the event
if( dwEventFlag && (pThreadObject == NULL) )
{
pThreadObject = AfxBeginThread( CPCM3780Dlg::EventThread, this,
THREAD_PRIORITY_HIGHEST, 0, 0, NULL );
dwStartTime = GetTickCount();
SetTimer(IntTimer,1001,NULL);
}
//4. terminate the thread when all the events are disabled
DWORD dwExitCode;
if(dwEventFlag == 0 && pThreadObject)
{
GetExitCodeThread(pThreadObject->m_hThread, &dwExitCode );
if( dwExitCode == STILL_ACTIVE )
TerminateThread( pThreadObject->m_hThread, dwExitCode );
pThreadObject=NULL;
// Kill the timer
KillTimer( IntTimer );
}
}
*pResult = 0;
}
UINT CPCM3780Dlg::EventThread(LPVOID ThreadArg)
{
PT_CheckEvent ptCheckEvent;
USHORT usEventType;
ptCheckEvent.EventType = &usEventType;
ptCheckEvent.Milliseconds = INFINITE;
CPCM3780Dlg* pArg= (CPCM3780Dlg*) ThreadArg;
while ( true )
{
if (DRV_CheckEvent( pArg->m_lDeviceHandle, &ptCheckEvent ) == SUCCESS)
{
usEventType -= ADS_EVT_INTERRUPT_DI0;
if( usEventType >= 0 && usEventType < 24 )
{
pArg->dwEventCount[usEventType]++;
}
}
}
return 0;
}
void CPCM3780Dlg::OnButtondisable()
{
// TODO: Add your control notification handler code here
int i;
PT_EnableEvent ptEnableEvent;
DWORD dwExitCode;
// 1. Clear the UI
for( i=0; i< 24; i++)
{
if( dwEventFlag & (0x01 << i) )
{
m_ListInterrupt.SetCheck( i, FALSE );
}
dwEventCount[i] = 0;
}
dwEventFlag = 0;
// 2. Stop the Check event thread
if(pThreadObject)
{
GetExitCodeThread(pThreadObject->m_hThread, &dwExitCode );
if( dwExitCode == STILL_ACTIVE )
TerminateThread( pThreadObject->m_hThread, dwExitCode );
pThreadObject=NULL;
}
// 3. disable all the event
ptEnableEvent.Count = 0;
ptEnableEvent.Enabled = 0; //disable ALL events
for( i=0; i<24; i++){
ptEnableEvent.EventType = ADS_EVT_INTERRUPT_DI0 + i;
m_dwErrCde = DRV_EnableEvent(m_lDeviceHandle, &ptEnableEvent);
if ( m_dwErrCde != SUCCESS)
{
DRV_GetErrorMessage( m_dwErrCde, (LPSTR)m_szErrMsg );
MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
return;
}
}
// 4. Kill the timer
KillTimer( IntTimer );
}
void CPCM3780Dlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (m_lDeviceHandle) {
DRV_DeviceClose(&m_lDeviceHandle);
}
}
void CPCM3780Dlg::OnButtonstop()
{
// TODO: Add your control notification handler code here
m_btnStart.EnableWindow(TRUE);
m_btnStop.EnableWindow(FALSE);
KillTimer( ReadTimer );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -