// Demo2Dlg.cpp : implementation file // #include "stdafx.h" #include "Demo2.h" #include "Demo2Dlg.h" #include "PISOCANDNMAPI.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BYTE HexToByte(CString str); WORD TotalDevices,InputDataLenList[128],OutputDataLenList[128],EPR_List[128]; BYTE DesMACIDList[128],ConnectionTypeList[128]; ///////////////////////////////////////////////////////////////////////////// // CDemo2Dlg dialog CDemo2Dlg::CDemo2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDemo2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemo2Dlg) m_MasterID = 0; m_BaudRate = 0; m_NewDeviceID = 0; m_NewInputLen = 0; m_NewOutputLen = 0; m_NewEPR = 0; m_IOType = _T(""); m_InputData = _T(""); m_OutputData = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDemo2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemo2Dlg) DDX_Control(pDX, IDC_COMBO_IOSLAVEID, m_IOSlaveID); DDX_Control(pDX, IDC_COMBO_SLAVEID, m_AllSlaveID); 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); DDX_Text(pDX, IDC_EDIT_IOTYPE, m_IOType); DDX_Text(pDX, IDC_EDIT_INPUTDATA, m_InputData); DDX_Text(pDX, IDC_EDIT_OUTPUTDATA, m_OutputData); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemo2Dlg, CDialog) //{{AFX_MSG_MAP(CDemo2Dlg) 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) ON_CBN_SELENDOK(IDC_COMBO_IOSLAVEID, OnSelendokComboIoslaveid) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemo2Dlg message handlers BOOL CDemo2Dlg::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); 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 CDemo2Dlg::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 CDemo2Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDemo2Dlg::OnButton1() { //ActiveBoard DWORD 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 CDemo2Dlg::OnOK() { // TODO: Add extra validation here //DNM_CloseBoard(0); CANDNM_Close(0,0); CDialog::OnOK(); } void CDemo2Dlg::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 CDemo2Dlg::OnButton3() { //AddDevice DWORD Ret; WORD 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 CDemo2Dlg::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 = (BYTE)strtol((char*)LPCTSTR(Str),&StopStr,10); 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 CDemo2Dlg::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 = (BYTE)strtol((char*)LPCTSTR(Str),&StopStr,10); 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 CDemo2Dlg::OnButton7() { //Read Input Data BYTE SlaveID,ConType = 0; DWORD Ret; WORD IOLen,i; BYTE IODATA[512]; CString Str,IOStr; static char *StopStr = new char[1]; m_IOSlaveID.GetLBText(m_IOSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; SlaveID = (BYTE)strtol((char*)LPCTSTR(Str),&StopStr,10); UpdateData(TRUE); if(m_IOType == "Poll") ConType = ConType_Poll; if(m_IOType == "Strobe") ConType = ConType_BitStrobe; if(m_IOType == "COS") ConType = ConType_COS; if(m_IOType == "Cyclic") ConType = ConType_Cyclic; Ret = CANDNM_ReadInputData(0,0,SlaveID,ConType,&IOLen,IODATA); if(Ret) { Str.Format("ReadInputData Error(%d)",Ret); AfxMessageBox(Str); return; } //Str.Format("IOLen:%d",IOLen); //AfxMessageBox(Str); IOStr = ""; for(i = 0;i < IOLen;i++) { Str.Format("%02X,",IODATA[i]); //AfxMessageBox(Str); IOStr += Str; } SetDlgItemText(IDC_EDIT_INPUTDATA,IOStr); } void CDemo2Dlg::OnButton8() { // Write Output Data BYTE Data[512]; WORD DataLen,StrIndex; DWORD Ret; CString Str,str1,str2; BYTE SlaveID,ConType; static char *StopStr = new char[1]; m_IOSlaveID.GetLBText(m_IOSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; SlaveID = (BYTE)strtol((char*)LPCTSTR(Str),&StopStr,10); UpdateData(TRUE); if(m_IOType == "Poll")ConType = ConType_Poll; if(m_IOType == "Strobe")ConType = ConType_BitStrobe; if(m_IOType == "COS")ConType = ConType_COS; if(m_IOType == "Cyclic")ConType = ConType_Cyclic; str1 = m_OutputData; if(str1.GetLength() < 1) return; DataLen = 0; StrIndex = 0; str2 = ""; while(1) { if(StrIndex > str1.GetLength()) break; else if(str1.GetAt(StrIndex) == ',') { Data[DataLen] = HexToByte(str2); DataLen++; str2 = ""; //Str.Format("%d. (%d)",DataLen,Data[DataLen-1]); //AfxMessageBox(Str); } else if(str1.GetAt(StrIndex) == ';') { Data[DataLen] = HexToByte(str2); DataLen++; //Str.Format("%d. (%d)",DataLen,Data[DataLen-1]); //AfxMessageBox(Str); break; } else str2 += str1.GetAt(StrIndex); StrIndex++; } Ret = CANDNM_WriteOutputData(0,0,SlaveID,ConType,DataLen,Data); if(Ret) Str.Format("Write Output Data Error(%d",Ret); else Str.Format("Write Output Data OK"); AfxMessageBox(Str); } BYTE HexToByte(CString str) { int first; int second; switch(str[0]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': first = str[0]-'0'; break; case 'A': case 'a': first = 10; break; case 'B': case 'b': first = 11; break; case 'C': case 'c': first = 12; break; case 'D': case 'd': first = 13; break; case 'E': case 'e': first = 14; break; case 'F': case 'f': first = 15; break; } if(str.GetLength() == 1) return first; switch(str[1]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': second = str[1]-'0'; break; case 'A': case 'a': second = 10; break; case 'B': case 'b': second = 11; break; case 'C': case 'c': second = 12; break; case 'D': case 'd': second = 13; break; case 'E': case 'e': second = 14; break; case 'F': case 'f': second = 15; break; } return first*16+second; } void CDemo2Dlg::OnButton9() { //Set Master ID DWORD Ret; UpdateData(TRUE); if(m_MasterID >= 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 CDemo2Dlg::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 CDemo2Dlg::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 CDemo2Dlg::OnButton12() { //Get ScanList DWORD Ret; CString Str; WORD i; Ret = CANDNM_GetScanList(0,0,&TotalDevices,DesMACIDList,ConnectionTypeList,InputDataLenList,OutputDataLenList,EPR_List); m_AllSlaveID.ResetContent(); m_IOSlaveID.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); m_IOSlaveID.AddString(Str); } } if(TotalDevices >= 1) m_AllSlaveID.SetCurSel(0); } void CDemo2Dlg::OnSelendokComboIoslaveid() { BYTE SlaveID,i,ConType; WORD OutputLen; CString Str,IOStr; static char *StopStr = new char[1]; m_IOSlaveID.GetLBText(m_IOSlaveID.GetCurSel(),(char*)LPCTSTR(Str)); StopStr[0] = '\n'; SlaveID = (BYTE)strtol((char*)LPCTSTR(Str),&StopStr,10); for(i = 0;i < 128;i++) { if((DesMACIDList[i] == SlaveID) && (ConnectionTypeList[i] != ConType_Explicit)) { ConType = ConnectionTypeList[i]; OutputLen = OutputDataLenList[i]; break; } } switch(ConType) { case ConType_Poll: m_IOType = "Poll"; break; case ConType_BitStrobe: m_IOType = "Strobe"; break; case ConType_COS: m_IOType = "COS"; break; case ConType_Cyclic: m_IOType = "Cyclic"; break; default: m_IOType = ConType; break; } if(OutputLen != 0) { IOStr = ""; for(i = 0;i < OutputLen;i++) { Str.Format("00,"); IOStr += Str; } IOStr.SetAt(IOStr.GetLength()-1,';'); } else IOStr = ""; m_OutputData = IOStr; UpdateData(FALSE); }