|
|
【程序】Win32窗口程序通过WiFi与STM32单片机通信 |
一派護法 十九級 |
|
一派護法 十九級 |
【C++代码】 #include <tchar.h> #include <stdio.h> #include <Windows.h> #include <CommCtrl.h> #include "resource.h"
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Comctl32.lib") #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define send_data(str) send(sock, str, strlen(str), NULL)
HWND hDlg; SOCKET sock; char *szPartA = "CMD_UART_"; char *szPartB = "_ENDUART_END"; BOOL bBeep = FALSE;
void connect() { SOCKADDR_IN addr; sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.8"); addr.sin_port = htons(1030); connect(sock, (SOCKADDR *)&addr, sizeof(addr)); }
void control_beep() { char data[30] = "CMD_BUZZER_"; if (bBeep) strcat_s(data, "0"); else strcat_s(data, "1"); bBeep = !bBeep; send(sock, data, strlen(data), NULL); }
void showmsg(BOOL bShowContent = FALSE) { char buffer[1024] = {0}; int len = recv(sock, buffer, sizeof(buffer), NULL); buffer[len] = '\0'; SetDlgItemTextA(hDlg, IDC_EDIT2, buffer);
if (bShowContent) { int lenA = strlen(szPartA); int lenB = strlen(szPartB); int buf_len = strlen(buffer); buffer[buf_len - lenB] = '\0'; SetDlgItemTextA(hDlg, IDC_EDIT3, buffer + lenA); } }
void sendmsg() { int lenA = strlen(szPartA); int lenB = strlen(szPartB); int len = lenA + lenB + 1;
HWND hEdit = GetDlgItem(hDlg, IDC_EDIT1); int len_input = GetWindowTextLengthA(hEdit); len += len_input; char *str = new char[len]; strcpy_s(str, len, szPartA); GetWindowTextA(hEdit, str + lenA, len + 1); strcat_s(str, len, szPartB); send_data(str);
delete[] str;
showmsg(TRUE); }
void seticon() { HICON hicon = LoadIcon(NULL, IDI_APPLICATION); SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hicon); SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hicon); }
void drawimg(LPDRAWITEMSTRUCT lpDis) { FillRect(lpDis->hDC, &lpDis->rcItem, (HBRUSH)GetStockObject(WHITE_BRUSH)); }
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { int wmId; switch (uMsg) { case WM_INITDIALOG: ::hDlg = hDlg; seticon(); connect(); return TRUE; case WM_COMMAND: wmId = LOWORD(wParam); switch (wmId) { case IDOK: case IDCANCEL: EndDialog(hDlg, wmId); break; case IDC_BUTTON1: control_beep(); showmsg(); break; case IDC_CHECK1: case IDC_CHECK2: case IDC_CHECK3: { int p = SendDlgItemMessage(hDlg, wmId, BM_GETCHECK, NULL, NULL); char str[30] = "CMD_LED_1_0"; str[8] += wmId - IDC_CHECK1; str[10] += p; send(sock, str, strlen(str), NULL); showmsg(); } break; case IDC_BUTTON2: sendmsg(); break; case IDC_BUTTON3: send_data("CMD_READ_ALL_END"); showmsg(); break; } break; case WM_DESTROY: closesocket(sock); break; case WM_DRAWITEM: { LPDRAWITEMSTRUCT lpDis = (LPDRAWITEMSTRUCT)lParam; if (lpDis->CtlID == IDC_VIDEO) drawimg(lpDis); } break; } return FALSE; }
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WSADATA wsa; WSAStartup(MAKEWORD(2, 2), &wsa); INT_PTR rs = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
WSACleanup(); return rs; }
|
一派護法 十九級 |
【RC资源文件】 // Microsoft Visual C++ generated resource script. // #include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #include "afxres.h"
///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS
///////////////////////////////////////////////////////////////////////////// // Chinese (Simplified, PRC) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS) LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE //
1 TEXTINCLUDE BEGIN "resource.h\0" END
2 TEXTINCLUDE BEGIN "#include ""afxres.h""\r\n" "\0" END
3 TEXTINCLUDE BEGIN "\r\n" "\0" END
#endif // APSTUDIO_INVOKED
#endif // Chinese (Simplified, PRC) resources /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////// // English (Australia) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENA) LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_AUS
///////////////////////////////////////////////////////////////////////////// // // Dialog //
IDD_DIALOG1 DIALOGEX 0, 0, 309, 257 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Wifi 测试" FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN EDITTEXT IDC_EDIT1,7,236,239,14,ES_AUTOHSCROLL DEFPUSHBUTTON "发送",IDC_BUTTON2,252,236,50,14 PUSHBUTTON "蜂鸣器",IDC_BUTTON1,7,7,50,14 PUSHBUTTON "查看传感器状态",IDC_BUTTON3,195,7,67,14 CONTROL "LED1",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,270,7,32,10 EDITTEXT IDC_EDIT2,7,29,255,14,ES_AUTOHSCROLL | ES_READONLY CONTROL "LED2",IDC_CHECK2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,270,22,32,10 EDITTEXT IDC_EDIT3,7,47,255,14,ES_AUTOHSCROLL | ES_READONLY CONTROL "LED3",IDC_CHECK3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,270,39,32,10 CONTROL "",IDC_VIDEO,"Static",SS_OWNERDRAW,7,64,295,169 END
///////////////////////////////////////////////////////////////////////////// // // DESIGNINFO //
#ifdef APSTUDIO_INVOKED GUIDELINES DESIGNINFO BEGIN IDD_DIALOG1, DIALOG BEGIN LEFTMARGIN, 7 RIGHTMARGIN, 302 TOPMARGIN, 7 BOTTOMMARGIN, 250 END END #endif // APSTUDIO_INVOKED
#endif // English (Australia) resources /////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 3 resource. //
///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKED
|
一派護法 十九級 |
【资源头文件】 //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by WifiConnect.rc // #define IDD_DIALOG1 101 #define IDC_BUTTON1 1001 #define IDC_CHECK1 1002 #define IDC_CHECK2 1003 #define IDC_CHECK3 1004 #define IDC_EDIT1 1005 #define IDC_BUTTON2 1006 #define IDC_EDIT2 1007 #define IDC_EDIT3 1008 #define IDC_BUTTON3 1009 #define IDC_VIDEO 1010
// Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1011 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif
|
一派護法 十九級 |
|
一派護法 十九級 |
【蜂鸣器测试】
|
一派護法 十九級 |
【传感器状态】 CMD_LED_0_0_0_ENDLED_DHT11_0.0_0.0_ENDDHT11_BUZZER_0_ENDBUZZER_END
|
一派護法 十九級 |
【用复选框控制LED灯的亮灭】
|
一派護法 十九級 |
【发送和接收字符串】
|
|
|
|