// Demo1Dlg.cpp : implementation file // #include "stdafx.h" #include "Demo1.h" #include "Demo1Dlg.h" #include "PISOCANDNMAPI.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BYTE HexToByte(CString str); ///////////////////////////////////////////////////////////////////////////// // CDemo1Dlg dialog CDemo1Dlg::CDemo1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDemo1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemo1Dlg) m_MasterID = 0; m_BaudRate = 0; m_NewDeviceID = 0; m_NewInputLen = 0; m_NewOutputLen = 0; m_NewEPR = 0; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDemo1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemo1Dlg) DDX_Control(pDX, IDC_COMBO_SLAVEID, m_AllSlaveID); DDX_Control(pDX, IDC_EDIT5, m_SetData); DDX_Control(pDX, IDC_EDIT4, m_GetDataASCII); DDX_Control(pDX, IDC_EDIT3, m_GetData); DDX_Control(pDX, IDC_COMBO6, m_AttributeID); DDX_Control(pDX, IDC_COMBO5, m_InstanceID); DDX_Control(pDX, IDC_COMBO4, m_ClassID); DDX_Text(pDX, IDC_EDIT_MASTERID, m_MasterID); DDV_MinMaxByte(pDX, m_MasterID, 0, 63); DDX_Text(pDX, IDC_EDIT_BAUDRATE, m_BaudRate); DDV_MinMaxByte(pDX, m_BaudRate, 0, 2); DDX_Text(pDX, IDC_EDIT_NEWID, m_NewDeviceID); DDX_Text(pDX, IDC_EDIT_NEWINPUTLEN, m_NewInputLen); DDX_Text(pDX, IDC_EDIT_NEWOUTPUTLEN, m_NewOutputLen); DDX_Text(pDX, IDC_EDIT_NEWEPR, m_NewEPR); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog) //{{AFX_MSG_MAP(CDemo1Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) ON_BN_CLICKED(IDC_BUTTON6, OnButton6) ON_BN_CLICKED(IDC_BUTTON7, OnButton7) ON_BN_CLICKED(IDC_BUTTON8, OnButton8) ON_BN_CLICKED(IDC_BUTTON9, OnButton9) ON_BN_CLICKED(IDC_BUTTON10, OnButton10) ON_BN_CLICKED(IDC_BUTTON11, OnButton11) ON_BN_CLICKED(IDC_BUTTON12, OnButton12) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemo1Dlg message handlers BOOL CDemo1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 CheckRadioButton(IDC_RADIO_POLL,IDC_RADIO_CYCLIC,IDC_RADIO_POLL); SetDlgItemText(IDC_EDIT5,"C4,09;"); return TRUE; // return TRUE unless you set the focus to a control } // 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 CDemo1Dlg::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 CDemo1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDemo1Dlg::OnButton1() { //ActiveBoard WORD Ret; Ret = CANDNM_Active(0,0); //Ret = DNM_ActiveBoard(0); if(Ret == 0) { SetDlgItemText(IDC_STATIC_ACTIVEBOARD,"OK."); GetDlgItem(IDC_STATIC_ACTIVEBOARD)->ShowWindow(true); } else GetDlgItem(IDC_STATIC_ACTIVEBOARD)->ShowWindow(false); } void CDemo1Dlg::OnOK() { // TODO: Add extra validation here //DNM_CloseBoard(0); CANDNM_Close(0,0); CDialog::OnOK(); } void CDemo1Dlg::OnButton2() { //Get MasterMACID DWORD MACID; CString Str; MACID = CANDNM_GetMasterMACID(0,0); if(MACID >= 64) {AfxMessageBox("Get Master MACID Fail"); return;} Str.Format("%02d",MACID); SetDlgItemText(IDC_EDIT_MASTERID,Str); } void CDemo1Dlg::OnButton3() { //AddDevice WORD Ret,NewEPR,InputLen,OutputLen; BYTE NewMACID,ConType; CString Str; short iRadioButton; UpdateData(TRUE); NewMACID = m_NewDeviceID; NewEPR = m_NewEPR; InputLen = m_NewInputLen; OutputLen = m_NewOutputLen; iRadioButton = GetCheckedRadioButton(IDC_RADIO_POLL,IDC_RADIO_CYCLIC); switch(iRadioButton) { case IDC_RADIO_POLL: ConType = ConType_Poll; break; case IDC_RADIO_STROBE: ConType = ConType_BitStrobe; break; case IDC_RADIO_COS: ConType = ConType_COS; break; case IDC_RADIO_CYCLIC: ConType = ConType_Cyclic; break; } //AddDevice Ret = CANDNM_AddDevice(0,0,NewMACID,1000); if(Ret != 0) { Str.Format("AddDevice Error(%d)",Ret); AfxMessageBox(Str); return; } //AddIOConnection Ret = CANDNM_AddIOConnection(0,0,NewMACID,ConType,InputLen,OutputLen,NewEPR); if(Ret != 0) { Str.Format("AddIOConnection Error(%d)",Ret); AfxMessageBox(Str); return; } AfxMessageBox("Add Device OK"); } void CDemo1Dlg::OnButton5() { // Start Device DWORD Ret; CString Str; static char *StopStr = new char[1]; BYTE DesMACID; m_AllSlaveID.GetLBText(m_AllSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; DesMACID = strtod((char*)LPCTSTR(Str),&StopStr); Ret = CANDNM_StartDevice(0,0,DesMACID); if(Ret == 0) Str.Format("Slave %d Started",DesMACID); else Str.Format("StartDevice Error(%d)",Ret); AfxMessageBox(Str); } void CDemo1Dlg::OnButton6() { // Stop Device DWORD Ret; CString Str; static char *StopStr = new char[1]; BYTE DesMACID; m_AllSlaveID.GetLBText(m_AllSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; DesMACID = strtod((char*)LPCTSTR(Str),&StopStr); Ret = CANDNM_StopDevice(0,0,DesMACID); if(Ret == 0) Str.Format("Slave %d Stopped",DesMACID); else Str.Format("StopDevice Error(%d)",Ret); AfxMessageBox(Str); } void CDemo1Dlg::OnButton7() { //Get Attribute CString Str; CString str1; CString str2; CString temp1; CString temp2; BYTE DesMACID; int ClassID,InstanceID,AttributeID; BYTE *data = new BYTE[64]; WORD len,Ret,i; static char *StopStr = new char[1]; ClassID = m_ClassID.GetCurSel(); InstanceID = m_InstanceID.GetCurSel(); AttributeID = m_AttributeID.GetCurSel(); if(ClassID == -1){MessageBox("Please select ClassID!"); return;} if(InstanceID == -1){MessageBox("Please select InstanceID!"); return;} if(AttributeID == -1){MessageBox("Please select AttributeID!"); return;} m_AllSlaveID.GetLBText(m_AllSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; DesMACID = strtod((char*)LPCTSTR(Str),&StopStr); Ret = CANDNM_GetAttribute(0,0,DesMACID,ClassID,InstanceID,AttributeID); Sleep(1000); Ret = CANDNM_GetAttributeValue(0,0,DesMACID,data,&len); if(Ret) { str1.Format("Error! Code is %d",Ret); m_GetData.SetWindowText(str1); return; } str1 = ""; str2 = ""; for(i=0;i= 64) return; Ret = CANDNM_SetMasterMACID(0,0,m_MasterID); if(Ret != DNMXS_NoError) {AfxMessageBox("Set Master MACID Fail"); return;} AfxMessageBox("Set Master ID OK"); } void CDemo1Dlg::OnButton10() { //Get Baud Rate DWORD BaudRate; CString Str; BaudRate = CANDNM_GetBaudRate(0,0); if(BaudRate >= 3) {AfxMessageBox("Get BaudRate Fail"); return;} Str.Format("%02d",BaudRate); SetDlgItemText(IDC_EDIT_BAUDRATE,Str); } void CDemo1Dlg::OnButton11() { //Set Baud Rate DWORD Ret; UpdateData(TRUE); if(m_BaudRate >= 3) return; Ret = CANDNM_SetBaudRate(0,0,m_BaudRate); if(Ret != DNMXS_NoError) {AfxMessageBox("Set Baud Rate Fail"); return;} AfxMessageBox("Set Baud Rate OK"); } void CDemo1Dlg::OnButton12() { //Get ScanList DWORD Ret; CString Str; WORD i,TotalDevices,InputDataLenList[128],OutputDataLenList[128],EPR_List[128]; BYTE DesMACIDList[128],ConnectionTypeList[128]; Ret = CANDNM_GetScanList(0,0,&TotalDevices,DesMACIDList,ConnectionTypeList,InputDataLenList,OutputDataLenList,EPR_List); m_AllSlaveID.ResetContent(); if(Ret) Str.Format("CANDNM_GetScanList Error(%d)",Ret); else Str.Format("CANDNM_GetScanList OK"); AfxMessageBox(Str); if(Ret) return; for(i = 0;i < TotalDevices;i++) { if(ConnectionTypeList[i] == ConType_Explicit) { Str.Format("%02d",DesMACIDList[i]); m_AllSlaveID.AddString(Str); } } if(TotalDevices >= 1) m_AllSlaveID.SetCurSel(0); }