// wifisetDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "wifiset.h"
#include "wifisetDlg.h"
#include <string>
#include <windows.h>
#include <vector>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma warning(disable:4996)
#define WM_RECV_SERIAL_DATA WM_USER + 101
// CwifisetDlg 对话框
int isScan=0;
static DWORD UserWirelessLanProcessThread(LPVOID pParam);
static DWORD UserWireLanProcessThread(LPVOID pParam);
CRITICAL_SECTION g_crit;
#define CTL_CODE( DeviceType, Function, Method, Access ) ( \
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)
// Cam3352AppSetDlg 对话框
#define METHOD_BUFFERED 0
#define METHOD_IN_DIRECT 1
#define METHOD_OUT_DIRECT 2
#define METHOD_NEITHER 3
#define FILE_ANY_ACCESS 0
#define FILE_READ_ACCESS ( 0x0001 ) // file & pipe
#define FILE_WRITE_ACCESS ( 0x0002 ) // file & pipe
#define FILE_DEVICE_BEEP 0x00000001
#define FILE_DEVICE_CD_ROM 0x00000002
#define FILE_DEVICE_CD_ROM_FILE_SYSTEM 0x00000003
#define FILE_DEVICE_CONTROLLER 0x00000004
#define FILE_DEVICE_DATALINK 0x00000005
#define FILE_DEVICE_DFS 0x00000006
#define FILE_DEVICE_DISK 0x00000007
#define FILE_DEVICE_DISK_FILE_SYSTEM 0x00000008
#define FILE_DEVICE_FILE_SYSTEM 0x00000009
#define FILE_DEVICE_INPORT_PORT 0x0000000a
#define FILE_DEVICE_KEYBOARD 0x0000000b
#define FILE_DEVICE_MAILSLOT 0x0000000c
#define FILE_DEVICE_MIDI_IN 0x0000000d
#define FILE_DEVICE_MIDI_OUT 0x0000000e
#define FILE_DEVICE_MOUSE 0x0000000f
#define FILE_DEVICE_MULTI_UNC_PROVIDER 0x00000010
#define FILE_DEVICE_NAMED_PIPE 0x00000011
#define FILE_DEVICE_NETWORK 0x00000012
#define FILE_DEVICE_NETWORK_BROWSER 0x00000013
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 0x00000014
#define FILE_DEVICE_NULL 0x00000015
#define FILE_DEVICE_PARALLEL_PORT 0x00000016
#define FILE_DEVICE_PHYSICAL_NETCARD 0x00000017
#define FILE_DEVICE_PRINTER 0x00000018
#define FILE_DEVICE_SCANNER 0x00000019
#define FILE_DEVICE_SERIAL_MOUSE_PORT 0x0000001a
#define FILE_DEVICE_SERIAL_PORT 0x0000001b
#define FILE_DEVICE_SCREEN 0x0000001c
#define FILE_DEVICE_SOUND 0x0000001d
#define FILE_DEVICE_STREAMS 0x0000001e
#define FILE_DEVICE_TAPE 0x0000001f
#define FILE_DEVICE_TAPE_FILE_SYSTEM 0x00000020
#define FILE_DEVICE_TRANSPORT 0x00000021
#define FILE_DEVICE_UNKNOWN 0x00000022
#define FILE_DEVICE_VIDEO 0x00000023
#define FILE_DEVICE_VIRTUAL_DISK 0x00000024
#define FILE_DEVICE_WAVE_IN 0x00000025
#define FILE_DEVICE_WAVE_OUT 0x00000026
#define FILE_DEVICE_8042_PORT 0x00000027
#define FILE_DEVICE_NETWORK_REDIRECTOR 0x00000028
#define FILE_DEVICE_BATTERY 0x00000029
#define FILE_DEVICE_BUS_EXTENDER 0x0000002a
#define FILE_DEVICE_MODEM 0x0000002b
#define FILE_DEVICE_VDM 0x0000002c
#define FILE_DEVICE_MASS_STORAGE 0x0000002d
#define FILE_DEVICE_SMB 0x0000002e
#define FILE_DEVICE_KS 0x0000002f
#define FILE_DEVICE_CHANGER 0x00000030
#define FILE_DEVICE_SMARTCARD 0x00000031
#define FILE_AUTOGENERATED_DEVICE_NAME 0x00000100
//
#define IOCTL_GPIO_SETBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0300, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_CLRBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0301, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_UPDATEBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0302, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_GETBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0303, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_SETMODE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0304, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_GETMODE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0305, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_GETIRQ \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0306, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_SET_POWER_STATE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0307, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_GET_POWER_STATE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0308, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_SET_DEBOUNCE_TIME \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0309, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_GET_DEBOUNCE_TIME \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0310, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_INIT_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0311, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_ACK_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0312, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_GPIO_DISABLE_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0313, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define SUCC 0 /*操作成功*/
#define FAIL -1 /*操作失败*/
CwifisetDlg::CwifisetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CwifisetDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CwifisetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, m_cbxNetMode);
DDX_Control(pDX, IDC_EDITPASS, m_lineEditPassword);
DDX_Control(pDX, IDC_EDIT3, m_lineEditResult);
DDX_Control(pDX, IDC_COMBOXCOM, m_cbxCom);
DDX_Control(pDX, IDC_LIST2, m_ListBoxWifiList);
DDX_Control(pDX, IDC_STATICSSID, m_SSID);
DDX_Control(pDX, IDC_IP1, lineIP1);
DDX_Control(pDX, IDC_IP2, lineIP2);
DDX_Control(pDX, IDC_IP3, lineIP3);
DDX_Control(pDX, IDC_IP4, lineIP4);
DDX_Control(pDX, IDC_BUTTON1, btnStartScan);
DDX_Control(pDX, IDC_BUTTON2, btnSetWifi);
DDX_Control(pDX, IDC_BUTTON6, btnSetWireNet);
}
BEGIN_MESSAGE_MAP(CwifisetDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CwifisetDlg::OnBtnStartScanwifi)
ON_BN_CLICKED(IDC_BUTTON2, &CwifisetDlg::OnBtnSetWifi)
ON_BN_CLICKED(IDC_BUTTON3, &CwifisetDlg::OnBtnOpenCom)
ON_LBN_SELCHANGE(IDC_LIST2, &CwifisetDlg::OnLbnSelchangeWifiList)
ON_BN_CLICKED(IDC_BUTTON4, &CwifisetDlg::OnBtnReset)
ON_MESSAGE(WM_RECV_SERIAL_DATA,OnRecvSerialData)
ON_BN_CLICKED(IDC_BUTTON5, &CwifisetDlg::OnBtnUpWifiList)
ON_BN_CLICKED(IDC_BUTTON6, &CwifisetDlg::OnBtnSetCableNetWork)
ON_BN_CLICKED(IDC_BUTTON7, &CwifisetDlg::OnBtnSetComMode)
END_MESSAGE_MAP()
// CwifisetDlg 消息处理程序
typedef vector<string> STRVECTOR;
typedef string::size_type STRPOS;
int SAU_split_str(string strSrc, STRVECTOR& vecDest, char cSep)
{
//当前分隔符位置和前一个分隔符位置
STRPOS pos = 0;
STRPOS prev_pos = 0;
STRPOS pos2 = 0;
//轮流查找
while ( (pos = strSrc.find_first_of(cSep,pos)) != string::npos )
{
string strTemp = strSrc.substr(prev_pos, pos-prev_pos);
vecDest.push_back(strTemp);
prev_pos = ++pos;
}
//如果分隔符后没有数据也要拷入
if (!strSrc.empty())
vecDest.push_back(&strSrc[prev_pos]);
//返回分割后子串个数
return vecDest.size();
}
int CwifisetDlg::ReadFileString(char * filePath,char *srcField,char *desField)
{
FILE *fp = fopen(filePath, "rb+");
long fileZize=0;
long InfoLen=0;
char szFileData[1250] = "\0";//数据最大为1K
char TempData[1250] = "\0";
int prePos = 0;
int pos = 0;
CString cs;
if (fp)
{
fseek(fp,0,SEEK_END); //定位到文件末
fileZize = ftell(fp); //文件长度
fseek(fp,0,SEEK_SET);
InfoLen = fread(szFileData,1 ,fileZize,fp);
fclose(fp);
while (1)
{
if (InfoLen > strlen(srcField)) //如果拷贝的数据比实际的小则退出
{
memcpy(TempData,&szFileData[pos],strlen(srcField));//拷贝文件内存的数据到
if (strcmp(TempData, srcField) == 0)
{
strcpy(desField, &szFileData[pos+strlen(srcField)+1]);
for (int i=0; i<strlen(desField); i++)
{
if (desField[i] == '\r') {
desField[i] = 0;
break;
}
}
break;
}
//cs = CString(TempData);
// AfxMessageBox(cs);
InfoLen--;
pos++;
} else {
cs.Format(_T("未找到相应字符串"));
AfxMessageBox(cs);
return FAIL;
}
}
return SUCC;
} else {
AfxMessageBox(_T("文件不存在"));
return FAIL;
}
}
int GetVaildStr(string str,int len,STRVECTOR& vecDest)
{
SAU_split_str(str,vecDest,0x0d);
return vecDest.size();
}
void getWifiName(string strSrc,char * szBuff)
{
//当前分隔符位置和前一个分隔符位置
STRPOS pos = 4;
STRPOS prev_pos = 0;
STRPOS pos2 = 4;
char cSep=0x20;
//WCHAR wBuff[300];
char szTempBuff[200]={0};
//轮流查找
CString cs;
while ((pos = strSrc.find_first_of(cSep,pos)) != string::npos )
{
break;
}
if(pos>4)
{
string strTemp = strSrc.substr(4, pos-4);
memcpy(szBuff, strTemp.c_str(), pos-4);//AfxMessageBox(_T("222"));
}
return ;
}
void CwifisetDlg::EnableContrl(bool isEnable)
{
btnStartScan.EnableWindow(isEnable);
btnSetWifi.EnableWindow(isEnable);
btnSetWireNet.EnableWindow(isEnable);
}
BOOL CwifisetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码.InsertString(0,);
char szCom[255] = "\0";
char szWifiMode[255]="\0";
char szIP[255]={0};
char TempData[10250];//数据最大为10K
char szPassword[200] = "\0";
char szSSID[255] = "\0";
WCHAR wPassword[200] = _T("\0");
WCHAR szSsid[255];
WCHAR szPass[255];
WCHAR szIP1[255];
WCHAR szIP2[255];
WCHAR szIP3[255];
WCHAR szIP4[255];
DWORD dwNum;
long fileZize=0;
int InfoLen;
int iReturn = 0;
int iMode=0;
int iCom=0;
int count = 0;
CString cs;
m_cbxCom.SetCurSel(0);
m_cbxNetMode.SetCurSel(0);
m_pSerial = NULL;
InitializeCriticalSection(&g_crit);
iReturn = ReadFileString("Hard Disk\\config.ini","SerialPort",szCom);
if (iReturn == SUCC)
{
iCom=atoi(szCom);
m_cbxCom.SetCurSel(iCom-1);
}
iReturn = ReadFileString("Hard Disk\\config.ini","ssid",szSSID);
if (iReturn == SUCC)
{
//cs = CString(szPassword);
dwNum = MultiByteToWideChar (CP_ACP,0, szPassword, -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,szSSID
,strlen(szSSID),szSsid,strlen(szSSID));
cs.Format(_T("%s"),szSsid);
SetDlgItemText(IDC_STATICSSID,cs);
}
iReturn = ReadFileString("Hard Disk\\config.ini","mode",szCom);
if (iReturn == SUCC)
{
iCom=atoi(szCom);
m_cbxNetMode.SetCurSel(iCom);
}
iReturn = ReadFileString("Hard Disk\\config.ini","password",szPassword);
if (iReturn == SUCC)
{
//cs = CString(szPassword);
dwNum = MultiByteToWideChar (CP_ACP,0, szPassword, -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,szPassword
,strlen(szPassword),wPassword,strlen(szPassword));
cs.Format(_T("%s"),wPassword);
SetDlgItemText(IDC_EDITPASS,cs);
}
iReturn = ReadFileString("Hard Disk\\config.ini","IP",szIP);
if (iReturn == SUCC)
{
STRVECTOR vecDest2;
//vecDest.clear();
count = SAU_split_str(szIP,vecDest2,'.');
dwNum = MultiByteToWideChar (CP_ACP,0, vecDest2[0].c_str(), -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,vecDest2[0].c_str()
,vecDest2[0].length(),szIP1,dwNum);
dwNum = MultiByteToWideChar (CP_ACP,0, vecDest2[1].c_str(), -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,vecDest2[1].c_str()
,vecDest2[1].length(),szIP2,dwNum);
dwNum = MultiByteToWideChar (CP_ACP,0, vecDest2[2].c_str(), -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,vecDest2[2].c_str()
,vecDest2[2].length(),szIP3,dwNum);
dwNum = MultiByteToWideChar (CP_ACP,0, vecDest2[3].c_str(), -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,vecDest2[3].c_str()
,vecDest2[3].length(),szIP4,dwNum);
cs.Format(_T("%s"),szIP1);
SetDlgItemText(IDC_IP1,cs);
cs.Format(_T("%s"),szIP2);
SetDlgItemText(IDC_IP2,cs);
cs.Format(_T("%s"),szIP3);
SetDlgItemText(IDC_IP3,cs);
cs.Format(_T("%s"),szIP4);
SetDlgItemText(IDC_IP4,cs);
}
SetDlgItemText(IDC_EDITTIP,_T("第一节 串口wifi设置\r\n1,选择串口1,打开提示成功\r\n2,选择串口设置模式\r\n3,开始扫描\r\n4,更新列表\r\n5,选择wifi\r\n6,修改模块IP地址\r\n7,设置网络,等待wifi_led灯重新闪烁查看IP地址是否变化\r\n\
第二节 有线设置\r\n1,将网线连接路由器和板子wan口\r\n\
第三节 恢复出厂设置\r\n1,选择串口1,打开提示成功\r\n2,选择串口设置模式\r\n3,点击恢复出厂设置,等待wifi_led灯重新闪烁查看IP地址是否变化"));
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
int Sign=0;
//定义串口接收数据函数类型
void CALLBACK CwifisetDlg::OnSerialRead(void * pOwner,BYTE* buf,DWORD bufLen)
{
BYTE *pRecvBuf = NULL; //接收缓冲区
//得到父对象指针
EnterCriticalSection(&g_crit);
CwifisetDlg* pThis = (CwifisetDlg*)pOwner;
pRecvBuf = new BYTE[bufLen];
ZeroMemory(pRecvBuf,bufLen);
memcpy(pRecvBuf,buf,bufLen);
Sign = 1;
pRecvBuf[bufLen] = '\0';
pThis->PostMessage(WM_RECV_SERIAL_DATA,WPARAM(pRecvBuf),bufLen);
//释放内存
Sleep(10);
ZeroMemory(pRecvBuf,bufLen);
delete[] pRecvBuf;
pRecvBuf = NULL;
LeaveCriticalSection(&g_crit);
}
// 串口接收数据处理函数
LONG CwifisetDlg::OnRecvSerialData(WPARAM wParam,LPARAM lParam)
{
CString strOldRecv = L"";
CString strRecv = L"";
DWORD dwBufLen = lParam;
CHAR *pBuf = (CHAR*)wParam;
m_lineEditResult.GetWindowTextW(strOldRecv);
strRecv = CString(pBuf);
strOldRecv = strOldRecv + strRecv;
m_lineEditResult.SetWindowTextW(strOldRecv);
m_lineEditResult.LineScroll(m_lineEditResult.GetLineCount());
Sleep(1);
//delete[] pBuf;
//pBuf = NULL;
return 0;
}
BOOL CwifisetDlg::openserial()
{
CString strTemp;
CString m_com;
//判断串口是否已经打开
if (m_pSerial != NULL)
{
m_pSerial->ClosePort();
delete m_pSerial;
m_pSerial = NULL;
}
m_cbxCom.GetWindowText(strTemp);
//int i=atoi((char*)LPCTSTR(strTemp));
int i=m_cbxCom.GetCurSel();;
switch(i)
{
case 0:
m_com="COM1:";
break;
case 1:
m_com="COM2:";
break;
case 2:
m_com="COM3:";
break;
case 3:
m_com="COM4:";
case 4:
m_com="COM5:";
case 5:
m_com="COM9:";
break;
}
//新建串口通讯对象
m_pSerial = new CCESeries();
m_pSerial->m_OnSeriesRead = OnSerialRead; //
CString a;
a="112500";
if (m_pSerial->OpenPort(this,m_com,a))
{
SetDlgItemText(IDC_EDIT3,_T("打开串口")+m_com+_T(" 成功"));
}
else
{
SetDlgItemText(IDC_EDIT3,_T("打开串口")+m_com+_T(" 失败"));
}
return TRUE;
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CwifisetDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_WIFISET_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_WIFISET_DIALOG));
}
}
#endif
int Delay(int micsecond)
{
Sign = 0;
while ((Sign != 1) && (micsecond > 0))
{
micsecond--;
Sleep(1);
}
if(micsecond < 1) return -1;
return 0;
}
void CwifisetDlg::OnBtnStartScanwifi()
{
// TODO: 在此添加控件通知处理程序代码
char commands_wifi[3000] = "at+wifi_Scan=?\r\n";
int iReturn = 0;
if (m_pSerial == NULL)
{
AfxMessageBox(L"device not open!");
return ;
}
EnableContrl(false);
m_lineEditResult.SetWindowTextW(_T(""));
m_pSerial->WriteSyncPort((BYTE*)commands_wifi, strlen(commands_wifi));
m_lineEditResult.SetWindowTextW(_T("等待3秒"));
iReturn = Delay(1000);
m_lineEditResult.SetWindowTextW(_T("完成"));
EnableContrl(true);
if (iReturn != 0) {
goto error;
} else {
return;
}
error:
m_lineEditResult.SetWindowTextW(_T("操作超时"));
}
static DWORD UserWirelessLanProcessThread(LPVOID pParam)
{
CwifisetDlg *dlg = (CwifisetDlg*) pParam;//new CuserkeyDlg;//
char * ROperMode = "at+ROperMode=0 \r\n";
char RstaWIFI[100] = "at+RstaWIFI=\0";
char * C2_uartpacklen = "at+C2_uartpacklen=100\r\n";
char * C2_uartpacktimeout = "at+C2_uartpacktimeout=100\r\n";
char * C2_uartpackintertimeout = "at+C2_uartpackintertimeout=100\r\n";
char * C2_TCPRealTime = "at+C2_TCPRealTime=1\r\n";
char * RBrDhcpc = "at+RBrDhcpc=0\r\n";
char RLANIp[100] = "at+RLANIp=";
char * RNoti = "at+RNoti=1\r\n";
char * mode1 = "at+mode=none\r\n";
char * Rdhcpd = "at+Rdhcpd=0\r\n";
char * RCommit = "at+RCommit=1\r\n";
char * uart = "at+uart=115200,8,n,1\r\n";
char * remotepro = "at+remotepro=tcp\r\n";
char * mode2 = "at+mode=server\r\n";
char * remoteip = "at+remoteip=192.168.100.4\r\n";
char * remoteport = "at+remoteport=8080\r\n";
char * net_commit = "at+net_commit=1\r\n";
char * reconn = "at+reconn=1\r\n";
char strSSID[255] = "\0";
char strWifiMode[2][255];
char strWifiPassword[255] = "\0";
char commands_wifi[3000] = "\0";
char strIP[4][4] = {0};
char commands_wifi_set[10240] = {0};
CString ssid;
CString WifiPassword;
CString strIP1;
CString strIP2;
CString strIP3;
CString strIP4;
int delay=5000;
int iReturn=0;
dlg->EnableContrl(false);
memset(strSSID, 0, 255);
memset(strWifiPassword, 0, 255);
dlg->lineIP1.GetWindowTextW(strIP1);
dlg->lineIP2.GetWindowTextW(strIP2);
dlg->lineIP3.GetWindowTextW(strIP3);
dlg->lineIP4.GetWindowTextW(strIP4);
dlg->m_SSID.GetWindowTextW(ssid);
dlg->m_lineEditPassword.GetWindowTextW(WifiPassword);
if (WifiPassword.GetLength() < 8)
{
AfxMessageBox(L"密码不足8位");
return -1;
}
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, ssid.GetBuffer(ssid.GetLength()),
ssid.GetLength(), strSSID, ssid.GetLength()*2, NULL, NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, WifiPassword.GetBuffer(WifiPassword.GetLength()),
WifiPassword.GetLength(), strWifiPassword, WifiPassword.GetLength()*2, NULL, NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, strIP1.GetBuffer(strIP1.GetLength()),
strIP1.GetLength(), strIP[0], strIP1.GetLength()*2, NULL, NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, strIP2.GetBuffer(strIP2.GetLength()),
strIP2.GetLength(), strIP[1], strIP2.GetLength()*2, NULL, NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, strIP3.GetBuffer(strIP3.GetLength()),
strIP3.GetLength(), strIP[2], strIP3.GetLength()*2, NULL, NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK, strIP4.GetBuffer(strIP4.GetLength()),
strIP4.GetLength(), strIP[3], strIP4.GetLength()*2, NULL, NULL);
strcpy(strWifiMode[0],"wpa2_aes");
strcpy(strWifiMode[1],"wpa_aes");
strcat(RstaWIFI,strSSID);//帐号
strcat(RstaWIFI,",");
if(dlg->m_cbxNetMode.GetCurSel()==0)//模式
strcat(RstaWIFI,strWifiMode[0]);
else if(dlg->m_cbxNetMode.GetCurSel()==1)
strcat(RstaWIFI,strWifiMode[1]);
strcat(RstaWIFI,",");
strcat(RstaWIFI,strWifiPassword);//密码
strcat(RstaWIFI,"\r\n");
strcat(RLANIp,strIP[0]);//IP地址
strcat(RLANIp,".");
strcat(RLANIp,strIP[1]);
strcat(RLANIp,".");
strcat(RLANIp,strIP[2]);
strcat(RLANIp,".");
strcat(RLANIp,strIP[3]);
strcat(RLANIp,",255.255.255.0\r\n");//子网掩码
//串口如果没有打开,直接返回
if (dlg->m_pSerial == NULL)
{
AfxMessageBox(L"device not open!");
return -1;
}
//发送数据
dlg->m_pSerial->WriteSyncPort((BYTE*)ROperMode, strlen(ROperMode));iReturn = Delay(1000);if (iReturn != 0) {goto error;}
dlg->m_pSerial->WriteSyncPort((BYTE*)RstaWIFI, strlen(RstaWIFI));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)C2_uartpacklen, strlen(C2_uartpacklen)); iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpacklen"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)C2_uartpacktimeout, strlen(C2_uartpacktimeout));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpacktimeouts"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)C2_uartpackintertimeout, strlen(C2_uartpackintertimeout));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpackintertimeout"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)C2_TCPRealTime, strlen(C2_TCPRealTime));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_TCPRealTime"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)RBrDhcpc, strlen(RBrDhcpc));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RBrDhcpc"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)RLANIp, strlen(RLANIp));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RLANIp"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)RNoti, strlen(RNoti));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RNoti"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)mode1, strlen(mode1));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("mode1"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)Rdhcpd, strlen(Rdhcpd));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("Rdhcpd"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)RCommit, strlen(RCommit));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RCommit"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)uart, strlen(uart));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("uart"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remotepro, strlen(remotepro));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//AfxMessageBox(_T("remotepro"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)mode2, strlen(mode2));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("mode2"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remoteip, strlen(remoteip));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("remoteip"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remoteport, strlen(remoteport));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("remoteport"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)net_commit, strlen(net_commit));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("net_commit"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)reconn, strlen(reconn));iReturn = Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("reconn"));
dlg->EnableContrl(true);
return SUCC;
error:
dlg->m_lineEditResult.SetWindowTextW(_T("操作超时"));
dlg->EnableContrl(true);
return FAIL;
}
static DWORD UserWireLanProcessThread(LPVOID pParam)
{
CwifisetDlg *dlg=(CwifisetDlg*) pParam;//new CuserkeyDlg;//
char * netmode="at+netmode=1 \r\n";
char * dhcpc="at+dhcpc=0 \r\n";
char net_ip[100]="at+net_ip=";
char * net_dns="0.0.0.0,0.0.0.0\r\n";
char * remoteip="at+remoteip=192.168.100.4\r\n";
char * remoteport="at+remoteport=8080\r\n";
char * remotepro="at+remotepro=tcp\r\n";
char * timeout="at+timeout=0\r\n";
char * mode2="at+mode=server\r\n";
char * uart="at+uart=115200,8,n,1\r\n";
char * uartpacklen="at+uartpacklen=64\r\n";
char * uartpacktimeout="at+uartpacktimeout=10\r\n";
char * net_commit="at+net_commit=1\r\n";
char * reconn="at+reconn=1\r\n";
char strSSID[255]="\0";
char strWifiPassword[255]="\0";
char commands_wifi[3000]="\0";
char strIP[4][4]={0};
char commands_wifi_set[10240]={0};
CString ssid;
CString WifiPassword;
CString strIP1;
CString strIP2;
CString strIP3;
CString strIP4;
int delay=5000;
int iReturn=0;
dlg->EnableContrl(false);
memset(strSSID,0,255);
memset(strWifiPassword,0,255);
dlg->lineIP1.GetWindowTextW(strIP1);
dlg->lineIP2.GetWindowTextW(strIP2);
dlg->lineIP3.GetWindowTextW(strIP3);
dlg->lineIP4.GetWindowTextW(strIP4);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strIP1.GetBuffer(strIP1.GetLength())
,strIP1.GetLength(),strIP[0],strIP1.GetLength()*2,NULL,NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strIP2.GetBuffer(strIP2.GetLength())
,strIP2.GetLength(),strIP[1],strIP2.GetLength()*2,NULL,NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strIP3.GetBuffer(strIP3.GetLength())
,strIP3.GetLength(),strIP[2],strIP3.GetLength()*2,NULL,NULL);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strIP4.GetBuffer(strIP4.GetLength())
,strIP4.GetLength(),strIP[3],strIP4.GetLength()*2,NULL,NULL);
strcat(net_ip,strIP[0]);
strcat(net_ip,".");
strcat(net_ip,strIP[1]);
strcat(net_ip,".");
strcat(net_ip,strIP[2]);
strcat(net_ip,".");
strcat(net_ip,strIP[3]);
strcat(net_ip,",255.255.255.0\r\n");
dlg->m_pSerial->WriteSyncPort((BYTE*)netmode,strlen(netmode)); iReturn=Delay(1000);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpacklen"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)dhcpc,strlen(dhcpc));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpacktimeouts"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)net_ip,strlen(net_ip));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_uartpackintertimeout"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)net_dns,strlen(net_dns));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("C2_TCPRealTime"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remoteip,strlen(remoteip));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RBrDhcpc"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)net_ip,strlen(net_ip));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RLANIp"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remoteport,strlen(remoteport));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RNoti"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)remotepro,strlen(remotepro));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("mode1"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)timeout,strlen(timeout));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("Rdhcpd"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)mode2,strlen(mode2));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("RCommit"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)uart,strlen(uart));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("uart"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)uartpacklen,strlen(uartpacklen));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//AfxMessageBox(_T("remotepro"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)uartpacktimeout,strlen(uartpacktimeout));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("mode2"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)net_commit,strlen(net_commit));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("remoteip"));//Sleep(1000);
dlg->m_pSerial->WriteSyncPort((BYTE*)reconn,strlen(reconn));iReturn=Delay(delay);if(iReturn!=0) {goto error;}//MessageBox(_T("remoteport"));//Sleep(1000);
dlg->EnableContrl(true);
return 1;
error:
dlg->m_lineEditResult.SetWindowTextW(_T("操作超时"));
dlg->EnableContrl(true);
return 1;
}
void CwifisetDlg::OnBtnSetWifi()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE commandThread;
if (m_pSerial == NULL)
{
AfxMessageBox(L"device not open!");
return;
}else
{
//AfxMessageBox(L"device open succed!");
}
m_lineEditResult.SetWindowTextW(_T(""));
isScan = 0;
commandThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)UserWirelessLanProcessThread, this, 0, &threadID);
}
void CwifisetDlg::OnBtnOpenCom()
{
openserial();
// TODO: 在此添加控件通知处理程序代码
}
void CwifisetDlg::OnLbnSelchangeWifiList()
{
// TODO: 在此添加控件通知处理程序代码
CString strText;
int nCurSel;
nCurSel = m_ListBoxWifiList.GetCurSel(); // 获取当前选中列表项
m_ListBoxWifiList.GetText(nCurSel, strText); // 获取选中列表项的字符串
SetDlgItemText(IDC_STATICSSID, strText); // 将选中列表项的字符串显
}
void CwifisetDlg::OnBtnReset()
{
// TODO: 在此添加控件通知处理程序代码
char *commands_wifi="at+default=1\r\nat+reboot=1\r\n";
char strSSID[255]="\0";
char strWifiPassword[255]="\0";
CString cs;
if (m_pSerial == NULL)
{
AfxMessageBox(L"device not open!");
}
m_lineEditResult.SetWindowTextW(_T(""));
//发送字符串
m_pSerial->WriteSyncPort((BYTE*)commands_wifi,strlen(commands_wifi));
}
void CwifisetDlg::OnBtnUpWifiList()
{
// TODO: 在此添加控件通知处理程序代码
STRVECTOR vecDest;
CString strOldRecv;
char szWifiLiest[20000];
int dwNum=0;
WCHAR wListName[200]={0};
m_lineEditResult.GetWindowTextW(strOldRecv);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strOldRecv.GetBuffer(strOldRecv.GetLength())
,strOldRecv.GetLength(),szWifiLiest,strOldRecv.GetLength()*2,NULL,NULL);
int count = SAU_split_str(szWifiLiest,vecDest,0xa);
CString cs;char szWifiName[200]="\0";
cs.Format(_T("%d"),count);
m_ListBoxWifiList.ResetContent();
for(int i=2;i<count;i++)
{
memset(szWifiName,0,200);
memset(wListName,0,200);
getWifiName(vecDest[i].c_str(),szWifiName);
dwNum = MultiByteToWideChar (CP_ACP,0, szWifiName, -1, NULL, 0);
MultiByteToWideChar(CP_ACP,0,szWifiName
,strlen(szWifiName),wListName,dwNum);
cs.Format(_T("%s"),wListName);
//MessageBox(cs);
m_ListBoxWifiList.AddString(wListName);
}
}
void CwifisetDlg::OnBtnSetCableNetWork()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE commandThread;
if (m_pSerial == NULL)
{
AfxMessageBox(L"device not open!");
return ;
}
m_lineEditResult.SetWindowTextW(_T(""));
isScan = 0;
commandThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)UserWireLanProcessThread, this, 0, &threadID);
}
void CwifisetDlg::OnBtnSetComMode()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE g_hGpio = NULL;
CString cs;
g_hGpio=CreateFile(_T("GIO1:"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
DWORD config[2];
config[0] = 3*32+18;
config[1] = 0;
DeviceIoControl(g_hGpio,IOCTL_GPIO_SETMODE,config,sizeof(config),NULL,0,NULL,NULL);
DeviceIoControl(g_hGpio,IOCTL_GPIO_CLRBIT,config,sizeof(config),NULL,0,NULL,NULL);
Sleep(3000);
DeviceIoControl(g_hGpio,IOCTL_GPIO_SETBIT,config,sizeof(config),NULL,0,NULL,NULL);
CloseHandle(g_hGpio);
}