// SelectDP5.cpp : implementation file // #include "stdafx.h" #include "SelectDP5.h" #include "SelectDP5Res.h" #include ".\selectdp5.h" BEGIN_MESSAGE_MAP(CSelectDP5, CDialog) ON_MESSAGE(WM_DPPCOMM_RCV_CHAR, OnCommunication) ON_MESSAGE(WM_DPPSOCKET_RCV_BUFFER, OnReceive) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_FIND_DP5_IP, OnBnClickedButtonFindDp5Ip) ON_BN_CLICKED(IDC_BUTTON_CLOSE_PORT, OnBnClickedButtonClosePort) ON_BN_CLICKED(IDC_BUTTON_OPEN_PORT, OnBnClickedButtonOpenPort) ON_BN_CLICKED(IDC_BUTTON_SERIAL_REQ_DATA, OnBnClickedButtonSerialReqData) ON_BN_CLICKED(IDC_RADIO_COMMTYPE1, OnBnClickedRadioCommtype1) ON_BN_CLICKED(IDC_RADIO_COMMTYPE2, OnBnClickedRadioCommtype2) ON_BN_CLICKED(IDC_RADIO_COMMTYPE3, OnBnClickedRadioCommtype3) ON_BN_CLICKED(IDC_BUTTON_FIND_USB_DEVICE, OnBnClickedButtonFindUsbDevice) ON_BN_CLICKED(IDC_CONNECTOK, OnBnClickedConnectOK) ON_BN_CLICKED(IDOK, OnOK) ON_BN_CLICKED(IDC_BUTTON_IP_SELECT1, OnBnClickedButtonIpSelect1) ON_BN_CLICKED(IDC_BUTTON_IP_SELECT2, OnBnClickedButtonIpSelect2) ON_BN_CLICKED(IDC_BUTTON_IP_SELECT3, OnBnClickedButtonIpSelect3) ON_BN_CLICKED(IDC_BUTTON_IP_SELECT4, OnBnClickedButtonIpSelect4) ON_STN_CLICKED(IDC_STATIC_NETFINDER1, OnStnClickedStaticNetfinder1) ON_STN_CLICKED(IDC_STATIC_NETFINDER2, OnStnClickedStaticNetfinder2) ON_STN_CLICKED(IDC_STATIC_NETFINDER3, OnStnClickedStaticNetfinder3) ON_STN_CLICKED(IDC_STATIC_NETFINDER4, OnStnClickedStaticNetfinder4) ON_BN_CLICKED(IDC_BUTTON_COUNT_DEVICES, OnBnClickedButtonCountDevices) ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SELECT_DEVICE, OnDeltaposSpinSelectDevice) ON_BN_CLICKED(IDC_BUTTON_CLOSE_DEVICE, OnBnClickedButtonCloseDevice) ON_BN_CLICKED(IDC_CHECK_TYPE_IP_ADDRESS, OnBnClickedCheckTypeIpAddress) ON_BN_CLICKED(IDC_BUTTON_CLIPBOARD_COPY, OnBnClickedButtonClipboardCopy) ON_EN_CHANGE(IDC_SPECTRUM_REFRESH_TIME, OnEnChangeSpectrumRefreshTime) ON_BN_CLICKED(IDC_BUTTON_SPECTRUM_REFRESH_DEFAULT, OnBnClickedButtonSpectrumRefreshDefault) ON_WM_HELPINFO() END_MESSAGE_MAP() // CSelectDP5 dialog //IDD_DP5_CONNECT //IMPLEMENT_DYNAMIC(CSelectDP5, CDialog) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog(CSelectDP5::IDD, pParent) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog(_T("IDD_DP5_CONNECT"), pParent) //CSelectDP5::CSelectDP5(CWnd* pParent /*=NULL*/) // : CDialog("IDD_DP5_CONNECT") CSelectDP5::CSelectDP5() : CDialog(_T("IDD_DP5_CONNECT")) { bTypeInIPAddress = FALSE; } //CSelectDP5::~CSelectDP5() //{ //} void CSelectDP5::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSpectrumDlg) // netfinder display DDX_Control(pDX, IDC_STATIC_NETFINDER1, lblNetFinder[0]); DDX_Control(pDX, IDC_STATIC_NETFINDER2, lblNetFinder[1]); DDX_Control(pDX, IDC_STATIC_NETFINDER3, lblNetFinder[2]); DDX_Control(pDX, IDC_STATIC_NETFINDER4, lblNetFinder[3]); DDX_Control(pDX, IDC_STATIC_FOUND1, lblDppFound[0]); DDX_Control(pDX, IDC_STATIC_FOUND2, lblDppFound[1]); DDX_Control(pDX, IDC_STATIC_FOUND3, lblDppFound[2]); DDX_Control(pDX, IDC_COMBO_COMPORT, cboComPort); DDX_Control(pDX, IDC_SPIN_SELECT_DEVICE, m_SpinSelectDevice); DDX_Control(pDX, IDC_CHECK_TYPE_IP_ADDRESS, m_TypeInIPAddressCheck); DDX_Control(pDX, IDC_SPECTRUM_REFRESH_TIME, m_SpectrumRefreshTimeCtrl); //}}AFX_DATA_MAP } BOOL CSelectDP5::OnInitDialog() { CDialog::OnInitDialog(); m_bModified = FALSE; CString strDataRefresh; long lDataRefresh; ComPortEnum(); lDataRefresh = s.profile->data_refresh; if ((lDataRefresh < 500) || (lDataRefresh > 20000)) { // 500-20,000 milli seconds lDataRefresh = 1000; s.profile->data_refresh = 1000; } strDataRefresh.Format("%d",s.profile->data_refresh); m_SpectrumRefreshTimeCtrl.SetWindowText(strDataRefresh); m_TypeInIPAddressCheck.SetCheck(bTypeInIPAddress); s.DppWinUSB.NumDevices = 0; GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("0"); s.DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetRange(1,1); m_SpinSelectDevice.SetPos(1); GetDlgItem(IDC_STATIC_NUM_DEVICES2)->SetWindowText("1"); IpSelectBtn[0] = IDC_BUTTON_IP_SELECT1; IpSelectBtn[1] = IDC_BUTTON_IP_SELECT2; IpSelectBtn[2] = IDC_BUTTON_IP_SELECT3; IpSelectBtn[3] = IDC_BUTTON_IP_SELECT4; // TODO: Add extra initialization here GetDlgItem(IDC_EDIT_IP_ADDR1)->SetWindowText("192"); GetDlgItem(IDC_EDIT_IP_ADDR2)->SetWindowText("168"); GetDlgItem(IDC_EDIT_IP_ADDR3)->SetWindowText("0"); GetDlgItem(IDC_EDIT_IP_ADDR4)->SetWindowText("230"); GetDlgItem(IDC_EDIT_IP_PORT)->SetWindowText("10001"); //UpdateNetfinderIpAddr(s.SockAddr); UpdateNetfinderIpAddr(s.profile->SockAddr); s.NetfinderActive = FALSE; m_PrevOwner = s.m_DppSck.m_Owner; s.m_DppSck.m_Owner = sockownStartDlg; s.m_DppSck.CreateSocket(this); GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(FALSE); GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(TRUE); cboComPort.SetCurSel(0); if ((s.profile->ComPort > 0) && (s.profile->ComPort <= 17)) { SetComPortID(s.profile->ComPort); //cboComPort.SetCurSel(s.profile->ComPort - 1); } lblDppFound[RS232] .SetFontName("Arial") .SetFontSize(14) .SetTextColor(colorLightSlateGray) .SetBkColor(colorLightSteelBlue) .SetFontBold(TRUE); lblDppFound[RS232].ShowWindow(SW_HIDE); s.isDppFound[RS232] = FALSE; lblDppFound[USB] .SetFontName("Arial") .SetFontSize(14) .SetTextColor(colorLightSlateGray) .SetBkColor(colorLightSteelBlue) .SetFontBold(TRUE); if (s.DppWinUSB.devInfo.deviceConnected) { lblDppFound[USB].ShowWindow(SW_SHOW); } else { lblDppFound[USB].ShowWindow(SW_HIDE); } lblDppFound[ETHERNET] .SetFontName("Arial") .SetFontSize(14) .SetTextColor(colorLightSlateGray) .SetBkColor(colorLightSteelBlue) .SetFontBold(TRUE); if (s.CurrentInterface < RS232) { s.CurrentInterface = RS232; } else if (s.CurrentInterface > ETHERNET) { s.CurrentInterface = ETHERNET; } if (s.CurrentInterface == RS232) { CheckRadioButton(IDC_RADIO_COMMTYPE1, IDC_RADIO_COMMTYPE3, IDC_RADIO_COMMTYPE1); } else if (s.CurrentInterface == USB) { CheckRadioButton(IDC_RADIO_COMMTYPE1, IDC_RADIO_COMMTYPE3, IDC_RADIO_COMMTYPE2); } else if (s.CurrentInterface == ETHERNET) { CheckRadioButton(IDC_RADIO_COMMTYPE1, IDC_RADIO_COMMTYPE3, IDC_RADIO_COMMTYPE3); } EnableCommFunctions(s.CurrentInterface); // initialize interface selection if (s.CurrentInterface == USB) { OnBnClickedButtonCountDevices(); OnBnClickedButtonFindUsbDevice(); } CenterWindow(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } // CSelectDP5 message handlers void CSelectDP5::OnBnClickedButtonFindDp5Ip() { CString strSockAddr; unsigned long SockAddr; for(int idxIP=0;idxIP<4;idxIP++){ GetDlgItem(IpSelectBtn[idxIP])->ShowWindow(SW_HIDE); IpSelectAddr[idxIP] = 0; } lblDppFound[2].ShowWindow(SW_HIDE); GetDlgItem(IDC_STATIC_IP_SERIAL_NUM)->ShowWindow(SW_HIDE); FindDpp.InitAllEntries(); ClearNetFinderStatusAll(); if (bTypeInIPAddress) { strSockAddr = FindDpp.SockAddr_DlgToStr(this,IDC_EDIT_IP_ADDR1,IDC_EDIT_IP_ADDR2,IDC_EDIT_IP_ADDR3,IDC_EDIT_IP_ADDR4); SockAddr = FindDpp.SockAddr_StrToULong(strSockAddr); // Test IP Address ReqDataStatusSock(SockAddr); s.NetfinderActive = FALSE; } else { SendBroadcast(TRUE); s.NetfinderActive = TRUE; } } //void CSelectDP5::OnReceive(int nErrorCode) LONG CSelectDP5::OnReceive(WPARAM ch, LPARAM Bytes) { //if (s.NetfinderActive) { // ReceivePacket(); //} else { // ReceivePacket(); // // get data //} ReceivePacket(); return 0; } void CSelectDP5::ReceivePacket() { unsigned char buffer[4096] = {0}; int num_bytes; //--------------------------------------------------------------- // Receive Packet from Buffer //--------------------------------------------------------------- // IP and Port of sender. CString destIP; UINT destPort; BOOL IpAddrFound; CString strSockAddr; unsigned long SockAddr; unsigned long SockAddrNew; int EntryFound; //int TestEntry; //CString strPort; BOOL bHaveNetFinderPacket; BOOL bDiscardPacket; int iBufferSize; int idxBuff; unsigned long TypeInSockAddr; num_bytes = s.m_DppSck.ReceiveFrom(buffer, 4096, destIP, destPort); //strPort.Format(" Port: %d",destPort); //AfxMessageBox(destIP + strPort); // Check for errors if(num_bytes >= 10000){ // Handle Error AfxMessageBox("Error receiving data"); return; } //AfxMessageBox("Received Packet"); //--------------------------------------------------------------- // Verify NetFinder Packet //--------------------------------------------------------------- bHaveNetFinderPacket = FALSE; bDiscardPacket = FALSE; if (!s.NetfinderActive) { // check for Amptek protocol Netfinder packet if ((buffer[0] == 0xF5) && (buffer[1] == 0xFA) && (buffer[2] == 0x82) && (buffer[3] == 0x08)) { iBufferSize = (int)(buffer[4] << 8) + (int)buffer[5]; if (iBufferSize < num_bytes) { // fix the buffer for NetFinder class for(idxBuff=0;idxBuff= 32) && (buffer[0] == 0x01) && (buffer[2] == (FindDpp.m_rand >> 8)) && (buffer[3] == (FindDpp.m_rand & 0x00FF))) { bHaveNetFinderPacket = TRUE; // have Silicon Labs Netfinder packet } else if((buffer[0] == 0x00) && (buffer[2] == (FindDpp.m_rand >> 8)) && (buffer[3] == (FindDpp.m_rand & 0x00FF))) { bDiscardPacket = TRUE; } // Discard Packet // We have received an identity request from another host. // This is a very rare case in which the PC randomly chooses port 3040 // for the netfinder app and the same random number is chosen. } if (bHaveNetFinderPacket) { EntryFound = NO_ENTRIES; //TestEntry = NO_ENTRIES; if (FindDpp.LastEntry == NO_ENTRIES) { // Add entry FindDpp.LastEntry = 0; FindDpp.AddEntry(&FindDpp.DppEntries[FindDpp.LastEntry], buffer, destPort); //TestEntry = FindDpp.LastEntry; } else { SockAddr = FindDpp.SockAddr_ByteToULong(&buffer[20]); EntryFound = FindDpp.FindEntry(SockAddr); if (EntryFound > NO_ENTRIES) { // update an existing entry FindDpp.InitEntry(&FindDpp.DppEntries[EntryFound]); FindDpp.AddEntry(&FindDpp.DppEntries[EntryFound], buffer, destPort); //TestEntry = EntryFound; } else { // Add entry // 20140430 Limit number of devices that can be added to Ethernet device list // LastEntry must be less than MAX_ENTRIES // any number of devices can be scanned // only MAX_ENTRIES number of devices can be stored // only 4 devices can be displayed at a time if (FindDpp.LastEntry < (MAX_ENTRIES - 1)) { FindDpp.LastEntry++; FindDpp.AddEntry(&FindDpp.DppEntries[FindDpp.LastEntry], buffer, destPort); //TestEntry = FindDpp.LastEntry; } } } ClearNetFinderStatusAll(); CString strStatus; COLORREF rgbConnect; IpAddrFound = FALSE; lblDppFound[ETHERNET].ShowWindow(SW_HIDE); s.isDppFound[ETHERNET] = FALSE; strSockAddr = FindDpp.SockAddr_DlgToStr(this,IDC_EDIT_IP_ADDR1,IDC_EDIT_IP_ADDR2,IDC_EDIT_IP_ADDR3,IDC_EDIT_IP_ADDR4); SockAddr = FindDpp.SockAddr_StrToULong(strSockAddr); // Test IP Address if (FindDpp.LastEntry > NO_ENTRIES) { for (int idxEntry=0;idxEntry<=FindDpp.LastEntry;idxEntry++) { if (idxEntry < 4) { // only 4 displays strStatus = FindDpp.DppEntries[idxEntry].str_display; rgbConnect = FindDpp.DppEntries[idxEntry].ccConnectRGB; if (bTypeInIPAddress) { // a amptek protocol netfinder packet has been received // the netfinder packet will have the devices local address // the local address may not equal the inet ip address // this not an error, use type in address as test place holder // the socket data source address can be used // in place of this value SockAddrNew = TypeInSockAddr; } else { SockAddrNew = FindDpp.DppEntries[idxEntry].SockAddr; } GetDlgItem(IpSelectBtn[idxEntry])->ShowWindow(SW_SHOW); IpSelectAddr[idxEntry] = SockAddrNew; if (SockAddr == SockAddrNew) { IpAddrFound = TRUE; //AfxMessageBox("Found"); lblDppFound[2].ShowWindow(SW_SHOW); s.isDppFound[ETHERNET] = TRUE; //s.SockAddr = SockAddr; // 20110502 rem dups use profile only s.profile->SockAddr = SockAddr; //s.strSockAddr = FindDpp.SockAddr_ULongToStr(s.SockAddr); // 20110502 s.strSockAddr = FindDpp.SockAddr_ULongToStr(s.profile->SockAddr); // 20110502 GetDlgItem(IDC_STATIC_IP_SERIAL_NUM)->SetWindowText(s.strSockAddr); GetDlgItem(IDC_STATIC_IP_SERIAL_NUM)->ShowWindow(SW_SHOW); s.m_DppSck.DppPort = FindDpp.SockPort_DlgToUINT(this,IDC_EDIT_IP_PORT); // s.m_DppSck.DppAddr = s.SockAddr; s.profile->SockAdd s.m_DppSck.DppAddr = s.profile->SockAddr; } UpdateNetFinderStatus(strStatus, rgbConnect, idxEntry); } } } } ////else { //// AfxMessageBox("Invalid Search Packet Received or Random Number Mismatch"); ////} } void CSelectDP5::UpdateNetFinderStatus(CString strStatus, COLORREF rgbConnect, int idxEntry) { lblNetFinder[idxEntry].SetBkColor(rgbConnect); // set the color first UpdateData(FALSE); lblNetFinder[idxEntry].SetText(strStatus); UpdateData(FALSE); } void CSelectDP5::ClearNetFinderStatus(int idxEntry) { lblNetFinder[idxEntry].SetBkColor(colorWhite); UpdateData(FALSE); lblNetFinder[idxEntry].SetWindowText(""); UpdateData(FALSE); } void CSelectDP5::ClearNetFinderStatusAll() { int idxEntry; for (idxEntry=0;idxEntry<4;idxEntry++){ ClearNetFinderStatus(idxEntry); } } void CSelectDP5::ReqDataStatusSock(unsigned long DppAddr) { //----------------------------------------------------------------- // Send Broadcast Identity Request Packets //----------------------------------------------------------------- // Note: This section may be moved to a function to allow // multiple re-tries. // //unsigned char buff[8] = { 0xF5, 0xFA, 0x02, 0x03, 0x00, 0x00, 0xFE, 0x0C }; unsigned char buff[8] = { 0xF5, 0xFA, 0x03, 0x07, 0x00, 0x00, 0xFE, 0x07 }; int dwBytes; SOCKADDR_IN remoteAddr; remoteAddr.sin_family = AF_INET; remoteAddr.sin_port = htons(10001); //DppAddr = inet_addr("192.168.0.230"); remoteAddr.sin_addr.s_addr = DppAddr; dwBytes =s.m_DppSck.SendTo(buff, 8, (const SOCKADDR* )&remoteAddr, sizeof(remoteAddr)); if(SOCKET_ERROR == dwBytes){ CString str; str.Format("Error Code: %i.", GetLastError()); AfxMessageBox(str + " Unable To Send Packet. Please Check Network Connection."); } } // Send Dpp Style NetFinder Broadcast Identity Request Packets void CSelectDP5::SendBroadcast(BOOL NewRand) { int m_rand; unsigned char buff[6] = { 0,0,0,0,0xF4,0xFA }; if (NewRand) { do{ m_rand = (rand()); // rand returns a number between 0 and 0x7FFF } while(m_rand == 0); // make sure m_rand is not zero } buff[2] = (m_rand >> 8); buff[3] = (m_rand & 0x00FF); FindDpp.m_rand = m_rand; // save to netfinder for dpp response detection if(SOCKET_ERROR == s.m_DppSck.SendTo(buff, 6, 3040, NULL)){ CString str; str.Format("Error Code: %i.", GetLastError()); AfxMessageBox(str + " Unable To Send Packet. Please Check Network Connection."); } } BOOL CSelectDP5::DestroyWindow() { // TODO: Add your specialized code here and/or call the base class // inet and rs232 need setup for data redirection to main dialog if (s.m_DppPort.IsPortOpen()) { // close the port, if selected will be reopened s.m_DppPort.ClosePort(); } s.m_DppSck.m_Owner = m_PrevOwner; // reassign previous owner return CDialog::DestroyWindow(); } void CSelectDP5::ComPortEnum() { cboComPort.ResetContent(); for (int i = 1; i < 255; i++) { char szComPortName[32]; char szComboName[32]; sprintf (szComPortName, "\\\\.\\COM%d", i); sprintf (szComboName, "COM%d", i); HANDLE hPort = CreateFile( szComPortName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hPort != INVALID_HANDLE_VALUE){ CloseHandle(hPort); hPort = NULL; cboComPort.AddString(szComboName); } } } int CSelectDP5::GetComPortID() { int iComPort=0; CString strCom(""); cboComPort.GetWindowText(strCom); iComPort = atoi(strCom.Mid(3)); return iComPort; } void CSelectDP5::SetComPortID(int iComPort) { CString strComPort; int iComPortIndex=0; strComPort.Format("COM%d",iComPort); iComPortIndex = cboComPort.FindStringExact(0,strComPort); if (iComPortIndex == -1) { if (cboComPort.GetCount() > 0) { iComPortIndex = 0; } } cboComPort.SetCurSel(iComPortIndex); } void CSelectDP5::OnBnClickedButtonClosePort() { if (s.m_DppPort.IsPortOpen()) { GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(FALSE); s.m_DppPort.ClosePort(); GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(TRUE); lblDppFound[RS232].ShowWindow(SW_HIDE); s.isDppFound[RS232] = FALSE; } } void CSelectDP5::OnBnClickedButtonOpenPort() { UINT ComPort; if (!s.m_DppPort.IsPortOpen()) { // init the port ComPort = GetComPortID(); //ComPort = (UINT)(cboComPort.GetCurSel()+1); //if (m_DppPort.Settings(this, 1, 115200, 'N', 8, 1, EV_RXCHAR, 1024)) { //20120725 VARIABLE_BAUD_RATE s.profile->baud_rate if (s.m_DppPort.Settings(this, ComPort, s.profile->baud_rate, 'N', 8, 1, EV_RXCHAR, 1024)) { //if (s.m_DppPort.Settings(this, ComPort, 115200, 'N', 8, 1, EV_RXCHAR, 1024)) { GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(FALSE); s.m_DppPort.OpenPort(); GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(TRUE); s.profile->ComPort = ComPort; //s.ComPort = ComPort; } } } LONG CSelectDP5::OnCommunication(WPARAM ch, LPARAM Bytes) { DWORD BytesRead; BytesRead = (DWORD)Bytes; if (BytesRead == 0) return -1; DataIn[s.DP5Proto.RS232BytesInBuffer]=(unsigned char)ch; s.DP5Proto.RS232BytesInBuffer+=BytesRead; // this calls OnComm for RxChar Events if (s.DP5Proto.RS232_RThreshold == 0) { // always call if thresh == 0 OnComm(); } else if (s.DP5Proto.RS232BytesInBuffer >= s.DP5Proto.RS232_RThreshold) { OnComm(); } return 0; } void CSelectDP5::OnComm() { long pktLen; unsigned char buffer[4096] = {0}; CString strPort; if (s.DP5Proto.RS232HeaderReceived) { s.DP5Proto.RS232HeaderReceived = FALSE; FindDppRS232.InitAllEntries(); pktLen = (long)((DataIn[4] * 256) + DataIn[5] + 8); // temporarily save the length field if (s.DP5Proto.RS232BytesInBuffer == s.DP5Proto.RS232_RThreshold) { //for (int i=0; i< 64;i++) { // m_DP5_Status.RAW[i] = DataIn[i+3078]; //} //s.DP5Stat.Process_Status(&m_DP5_Status); for (int i=0; i< pktLen;i++) { buffer[i] = DataIn[i+6]; } FindDppRS232.AddEntry(&FindDppRS232.DppEntries[0], buffer, 0); CString strStatus; cboComPort.GetWindowText(strPort); strStatus = FindDppRS232.EntryToStrRS232(&FindDppRS232.DppEntries[0], ("RS232 - " + strPort)); GetDlgItem(IDC_EDIT_DPPCOMM_STATUS)->SetWindowText(strStatus); lblDppFound[RS232].ShowWindow(SW_SHOW); s.isDppFound[RS232] = TRUE; } s.DP5Proto.RS232_RThreshold = 8; } else { if (s.DP5Proto.RS232BytesInBuffer >= s.DP5Proto.RS232_RThreshold) { if ((DataIn[0] == 0xF5) && (DataIn[1] == 0xFA)) { pktLen = (long)((DataIn[4] * 256) + DataIn[5] + 8); // temporarily save the length field if (pktLen > 0) { s.DP5Proto.RS232_RThreshold = pktLen; // additional bytes to read = data length //SetDataRcvTimer(RS232_RThreshold); s.DP5Proto.RS232HeaderReceived = TRUE; } else { //ParsePacket(DataIn[]); } } else { memset(&DataIn, 0, sizeof(DataIn)); // flush the buffer by emptying it } } } } void CSelectDP5::ReqDataStatus() { //----------------------------------------------------------------- // request data and status //----------------------------------------------------------------- //unsigned char buff[8] = { 0xF5, 0xFA, 0x02, 0x03, 0x00, 0x00, 0xFE, 0x0C }; // NetFinder Packet unsigned char buff[8] = { 0xF5, 0xFA, 0x03, 0x07, 0x00, 0x00, 0xFE, 0x07 }; memset(&DataIn, 0, sizeof(DataIn)); s.DP5Proto.RS232BytesInBuffer = 0; s.DP5Proto.RS232HeaderReceived = FALSE; s.DP5Proto.RS232_RThreshold = 8; s.m_DppPort.WriteData(buff, sizeof(buff)); } void CSelectDP5::OnBnClickedButtonSerialReqData() { lblDppFound[RS232].ShowWindow(SW_HIDE); s.isDppFound[RS232] = FALSE; ReqDataStatus(); } void CSelectDP5::OnBnClickedRadioCommtype1() { s.CurrentInterface = RS232; EnableCommFunctions(RS232); } void CSelectDP5::OnBnClickedRadioCommtype2() { s.CurrentInterface = USB; EnableCommFunctions(USB); OnBnClickedButtonCountDevices(); OnBnClickedButtonFindUsbDevice(); } void CSelectDP5::OnBnClickedRadioCommtype3() { s.CurrentInterface = ETHERNET; EnableCommFunctions(ETHERNET); OnBnClickedButtonFindDp5Ip(); } void CSelectDP5::OnBnClickedButtonFindUsbDevice() { TRANSMIT_PACKET_TYPE XmtCmd; BOOL HaveBuffer; BOOL SentPkt; string strUSBStatus; DPP_USB_STATUS USBStatus; //0=do nothing, 1=new connect, 2=cannot connect long pktLen; unsigned char buffer[4096] = {0}; //CONFIG_OPTIONS CfgOptions; //CfgOptions.DppType = s.DP5Stat.m_DP5_Status.DEVICE_ID; //CfgOptions.HwCfgDP5Out = s.HwCfgDP5Out; //CfgOptions.PC5_PRESENT = s.DP5Stat.m_DP5_Status.PC5_PRESENT; //CfgOptions.SendCoarseFineGain = FALSE; OnBnClickedButtonCloseDevice(); USBStatus = s.DppWinUSB.FindUSBDevice(s.DppWinUSB.CurrentDevice - 1); if (USBStatus != dusbStatNoAction) { strUSBStatus = s.DppWinUSB.USBStatusString(USBStatus); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText(strUSBStatus.c_str()); if (USBStatus == dusbStatNewConnect) { s.CurrentInterface = USB; } } if (s.DppWinUSB.devInfo.deviceConnected) { lblDppFound[USB].ShowWindow(SW_SHOW); s.isDppFound[USB] = TRUE; XmtCmd = XMTPT_SEND_NETFINDER_PACKET; HaveBuffer = s.SndCmd.DP5_CMD(s.DP5Proto.BufferOUT, XmtCmd); if (HaveBuffer) { s.DP5Proto.ACK_Received = false; s.DP5Proto.Packet_Received = false; // a response packet is always expected s.DP5Proto.PIN.STATUS = 0xFF; // packet invalid - will be overwritten soon by response/ACK packet SentPkt = s.DppWinUSB.SendPacketUSB(s.DP5Proto.BufferOUT, s.DppWinUSB.devInfo, s.DP5Proto.PacketIn); } if (SentPkt) { FindDppUSB.InitAllEntries(); pktLen = (long)((s.DP5Proto.PacketIn[4] * 256) + s.DP5Proto.PacketIn[5] + 8); // temporarily save the length field for (int i=0; i< pktLen;i++) { buffer[i] = s.DP5Proto.PacketIn[i+6]; } FindDppUSB.AddEntry(&FindDppUSB.DppEntries[0], buffer, 0); CString strStatus; CString strDeviceInfo; strDeviceInfo.Format("USB Device %d of %d", s.DppWinUSB.CurrentDevice, s.DppWinUSB.NumDevices); strStatus = FindDppUSB.EntryToStrUSB(&FindDppUSB.DppEntries[0], strDeviceInfo); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText(strStatus); } } else { lblDppFound[USB].ShowWindow(SW_HIDE); s.isDppFound[USB] = FALSE; } } void CSelectDP5::EnableCommFunctions(int ControlGroup) { BOOL isSComEnabled; BOOL isDUSBEnabled; BOOL isINETEnabled; int ShowSComEnabled; int ShowDUSBEnabled; int ShowINETEnabled; isSComEnabled = FALSE; isDUSBEnabled = FALSE; isINETEnabled = FALSE; ShowSComEnabled = SW_HIDE; ShowDUSBEnabled = SW_HIDE; ShowINETEnabled = SW_HIDE; switch (ControlGroup) { case RS232: isSComEnabled = TRUE; ShowSComEnabled = isSComEnabled * SW_SHOW; break; case USB: isDUSBEnabled = TRUE; ShowDUSBEnabled = isDUSBEnabled * SW_SHOW; break; case ETHERNET: isINETEnabled = TRUE; ShowINETEnabled = isINETEnabled * SW_SHOW; break; } // Serial Comm GetDlgItem(IDC_BUTTON_OPEN_PORT)->EnableWindow(isSComEnabled); GetDlgItem(IDC_BUTTON_CLOSE_PORT)->EnableWindow(isSComEnabled); GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(isSComEnabled); GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(isSComEnabled); GetDlgItem(IDC_EDIT_DPPCOMM_STATUS)->EnableWindow(isSComEnabled); GetDlgItem(IDC_EDIT_DPPCOMM_STATUS)->ShowWindow(ShowSComEnabled); if (ControlGroup == RS232) { if (s.m_DppPort.IsPortOpen()) { GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(TRUE); GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(FALSE); } else { GetDlgItem(IDC_BUTTON_SERIAL_REQ_DATA)->EnableWindow(FALSE); GetDlgItem(IDC_COMBO_COMPORT)->EnableWindow(TRUE); } } if (ControlGroup != RS232) { lblDppFound[RS232].SetNullColor(); } else { lblDppFound[RS232].SetLastColor(); } // WinUsb GetDlgItem(IDC_BUTTON_FIND_USB_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_EDIT_STATUS2)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_BUTTON_COUNT_DEVICES)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_NUM_DEVICES)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_SELECT_USB_DEVICE_LABEL)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_SPIN_SELECT_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_STATIC_NUM_DEVICES2)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_BUTTON_CLOSE_DEVICE)->EnableWindow(isDUSBEnabled); GetDlgItem(IDC_EDIT_STATUS2)->ShowWindow(ShowDUSBEnabled); if (ControlGroup != USB) { lblDppFound[USB].SetNullColor(); } else { lblDppFound[USB].SetLastColor(); } // Sockets GetDlgItem(IDC_BUTTON_FIND_DP5_IP)->EnableWindow(isINETEnabled); GetDlgItem(IDC_EDIT_IP_ADDR1)->EnableWindow(isINETEnabled); GetDlgItem(IDC_EDIT_IP_ADDR2)->EnableWindow(isINETEnabled); GetDlgItem(IDC_EDIT_IP_ADDR3)->EnableWindow(isINETEnabled); GetDlgItem(IDC_EDIT_IP_ADDR4)->EnableWindow(isINETEnabled); GetDlgItem(IDC_EDIT_IP_PORT)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_IP_ADDRESS_LABEL)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_PORT_LABEL)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER1)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER2)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER3)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER4)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_1)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_2)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_3)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_4)->EnableWindow(isINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER1)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER2)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER3)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_NETFINDER4)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_1)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_2)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_3)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_STATIC_LABEL_IP_4)->ShowWindow(ShowINETEnabled); GetDlgItem(IDC_BUTTON_CLIPBOARD_COPY)->ShowWindow(ShowINETEnabled); // Shared by ALL Comm GetDlgItem(IDC_RADIO_COMMTYPE1)->EnableWindow(TRUE); // Serial Comm GetDlgItem(IDC_RADIO_COMMTYPE2)->EnableWindow(TRUE); // WinUsb GetDlgItem(IDC_RADIO_COMMTYPE3)->EnableWindow(TRUE); // Sockets if (ControlGroup != ETHERNET) { lblNetFinder[0].SetNullColor(); lblNetFinder[1].SetNullColor(); lblNetFinder[2].SetNullColor(); lblNetFinder[3].SetNullColor(); lblDppFound[ETHERNET].SetNullColor(); } else { lblNetFinder[0].SetLastColor(); lblNetFinder[1].SetLastColor(); lblNetFinder[2].SetLastColor(); lblNetFinder[3].SetLastColor(); lblDppFound[ETHERNET].SetLastColor(); } for(int idxIP=0;idxIP<4;idxIP++){ GetDlgItem(IpSelectBtn[idxIP])->EnableWindow(isINETEnabled); } GetDlgItem(IDC_STATIC_IP_SERIAL_NUM)->EnableWindow(isINETEnabled); } void CSelectDP5::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); } void CSelectDP5::OnBnClickedButtonIpSelect1() { if (!bTypeInIPAddress) { UpdateNetfinderIpAddr(IpSelectAddr[0]); } if (GetDlgItem(IDC_BUTTON_FIND_DP5_IP)->IsWindowEnabled()) { OnBnClickedButtonFindDp5Ip(); } } void CSelectDP5::OnBnClickedButtonIpSelect2() { UpdateNetfinderIpAddr(IpSelectAddr[1]); if (GetDlgItem(IDC_BUTTON_FIND_DP5_IP)->IsWindowEnabled()) { OnBnClickedButtonFindDp5Ip(); } } void CSelectDP5::OnBnClickedButtonIpSelect3() { UpdateNetfinderIpAddr(IpSelectAddr[2]); if (GetDlgItem(IDC_BUTTON_FIND_DP5_IP)->IsWindowEnabled()) { OnBnClickedButtonFindDp5Ip(); } } void CSelectDP5::OnBnClickedButtonIpSelect4() { UpdateNetfinderIpAddr(IpSelectAddr[3]); if (GetDlgItem(IDC_BUTTON_FIND_DP5_IP)->IsWindowEnabled()) { OnBnClickedButtonFindDp5Ip(); } } void CSelectDP5::OnStnClickedStaticNetfinder1() { if (GetDlgItem(IpSelectBtn[0])->IsWindowEnabled()) { if (GetDlgItem(IpSelectBtn[0])->IsWindowVisible()) { OnBnClickedButtonIpSelect1(); } } } void CSelectDP5::OnStnClickedStaticNetfinder2() { if (GetDlgItem(IpSelectBtn[1])->IsWindowEnabled()) { if (GetDlgItem(IpSelectBtn[1])->IsWindowVisible()) { OnBnClickedButtonIpSelect2(); } } } void CSelectDP5::OnStnClickedStaticNetfinder3() { if (GetDlgItem(IpSelectBtn[2])->IsWindowEnabled()) { if (GetDlgItem(IpSelectBtn[2])->IsWindowVisible()) { OnBnClickedButtonIpSelect3(); } } } void CSelectDP5::OnStnClickedStaticNetfinder4() { if (GetDlgItem(IpSelectBtn[3])->IsWindowEnabled()) { if (GetDlgItem(IpSelectBtn[3])->IsWindowVisible()) { OnBnClickedButtonIpSelect4(); } } } void CSelectDP5::UpdateNetfinderIpAddr(unsigned long IpAddr) { CString strIp; in_addr inSockAddr; inSockAddr.S_un.S_addr = IpAddr; strIp.Format("%d",inSockAddr.S_un.S_un_b.s_b1); GetDlgItem(IDC_EDIT_IP_ADDR1)->SetWindowText(strIp); strIp.Format("%d",inSockAddr.S_un.S_un_b.s_b2); GetDlgItem(IDC_EDIT_IP_ADDR2)->SetWindowText(strIp); strIp.Format("%d",inSockAddr.S_un.S_un_b.s_b3); GetDlgItem(IDC_EDIT_IP_ADDR3)->SetWindowText(strIp); strIp.Format("%d",inSockAddr.S_un.S_un_b.s_b4); GetDlgItem(IDC_EDIT_IP_ADDR4)->SetWindowText(strIp); } void CSelectDP5::OnBnClickedButtonCountDevices() { CDppWinUSB DppWinUSB1; CString strNumDevices; s.DppWinUSB.NumDevices = DppWinUSB1.CountDP5WinusbDevices(); if (s.DppWinUSB.CurrentDevice > s.DppWinUSB.NumDevices) { s.DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetPos(1); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("1"); } if (s.DppWinUSB.NumDevices > 1) { m_SpinSelectDevice.SetRange(1,s.DppWinUSB.NumDevices); m_SpinSelectDevice.EnableWindow(TRUE); } else { s.DppWinUSB.CurrentDevice = 1; m_SpinSelectDevice.SetPos(1); m_SpinSelectDevice.SetRange(1,1); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText("1"); m_SpinSelectDevice.EnableWindow(FALSE); } strNumDevices.Format("%d",s.DppWinUSB.NumDevices); GetDlgItem(IDC_STATIC_NUM_DEVICES)->SetWindowText(strNumDevices); UpdateData(FALSE); // DDEX from var to cntrl, ie sends var value to control } void CSelectDP5::OnDeltaposSpinSelectDevice(NMHDR *pNMHDR, LRESULT *pResult) { LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR); CString strPos; int iMax = 1; int iMin = 1; int iVal = 1; // old value int iPos = 1; // new value (if updated) int iDelta = 0; // direction of change int iNewPos = 1; // final value m_SpinSelectDevice.GetRange(iMin, iMax); if ((iMin != 1) || (iMax < 1)) { return; } // max/min value errors if (iMax == 1) { // 0 or 1 devices iNewPos = 1; } else { // max must be greater than 1 GetDlgItem(IDC_STATIC_NUM_DEVICES2)->GetWindowText(strPos); iVal = atoi(strPos); iPos = pNMUpDown->iPos; iDelta = pNMUpDown->iDelta; if (iVal != iPos) { // value was updated iNewPos = iPos; } else { // control just enabled, has delta but pos not updated iNewPos = iPos + iDelta; // update value if (iNewPos > iMax) { // do wrap if needed iNewPos = iMin; } else if (iNewPos < iMin) { iNewPos = iMax; } } } //strPos.Format("iVal %d iPos %d iDelta %d iNewPos %d", iVal, iPos, iDelta, iNewPos); strPos.Format("%d", iNewPos); //AfxMessageBox(strPos); s.DppWinUSB.CurrentDevice = iNewPos; GetDlgItem(IDC_STATIC_NUM_DEVICES2)->SetWindowText(strPos); *pResult = 0; } void CSelectDP5::OnBnClickedButtonCloseDevice() { s.DppWinUSB.devInfo.deviceConnected = FALSE; s.DppWinUSB.CloseWinUsbDevice(); s.WinusbNotify.RemoveDeviceNotification(); GetDlgItem(IDC_EDIT_STATUS2)->SetWindowText("Device Connection Closed"); lblDppFound[USB].ShowWindow(SW_HIDE); s.isDppFound[USB] = FALSE; } void CSelectDP5::OnBnClickedCheckTypeIpAddress() { bTypeInIPAddress = m_TypeInIPAddressCheck.GetCheck(); } void CSelectDP5::OnBnClickedConnectOK() { CString strDataRefresh; long lDataRefresh; if (m_bModified) { m_SpectrumRefreshTimeCtrl.GetWindowText(strDataRefresh); lDataRefresh = atoi(strDataRefresh); if ((lDataRefresh < 500) || (lDataRefresh > 20000)) { // 500-20,000 milli seconds lDataRefresh = 1000; s.profile->data_refresh = 1000; } else { s.profile->data_refresh = lDataRefresh; } } CDialog::OnOK(); } void CSelectDP5::OnOK() { //DISABLE DEFAULT } void CSelectDP5::CopyTextToClip(CString strTitle, CString strDesc) { CString strText; strText = strTitle + "\r\n" + "\r\n" + strDesc + "\r\n" ; AfxGetApp()->m_pMainWnd->OpenClipboard() ; EmptyClipboard(); CSharedFile sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); sf.Write(strText, strText.GetLength()); HGLOBAL hMem = sf.Detach(); if (!hMem) return; SetClipboardData(CF_TEXT, hMem); CloseClipboard(); } void CSelectDP5::OnBnClickedButtonClipboardCopy() { CString strText; CString strTextAll; strTextAll = ""; GetDlgItem(IDC_STATIC_NETFINDER1)->GetWindowText(strText); strTextAll += strText + "\r\n"; GetDlgItem(IDC_STATIC_NETFINDER2)->GetWindowText(strText); strTextAll += strText + "\r\n"; GetDlgItem(IDC_STATIC_NETFINDER3)->GetWindowText(strText); strTextAll += strText + "\r\n"; GetDlgItem(IDC_STATIC_NETFINDER4)->GetWindowText(strText); strTextAll += strText + "\r\n"; CopyTextToClip("Ethernet NetFinder",strTextAll); } void CSelectDP5::SetModified(BOOL bChanged) { m_bModified = bChanged; } void CSelectDP5::OnEnChangeSpectrumRefreshTime() { SetModified(TRUE); } void CSelectDP5::OnBnClickedButtonSpectrumRefreshDefault() { m_SpectrumRefreshTimeCtrl.SetWindowText("1000"); s.profile->data_refresh = 1000; SetModified(FALSE); } BOOL CSelectDP5::OnHelpInfo(HELPINFO* pHelpInfo) { CString strHelp; if (pHelpInfo->iContextType == HELPINFO_WINDOW) { if (pHelpInfo->dwContextId == 0) { ::HtmlHelp(this->GetSafeHwnd(), theApp.m_pszHelpFilePath, HH_HELP_CONTEXT, HIDD_DP5_CONNECT); } else { // } } return true; }