📄 comm.cpp
字号:
// Create by lixp. Email address: gzlixp@yahoo.com.cn
#include "stdafx.h"
#include <afxext.h>
#include "Comm.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100
#define CmdTest 0x01
#define CmdCreateFile 0x02
#define CmdOpenFile 0x03
#define CmdWriteFile 0x04
#define CmdReadFile 0x05
#define CmdFileSeek 0x06
#define CmdCloseFile 0x07
#define CmdGetFileAttr 0x08
#define CmdSetFileAttr 0x09
#define CmdGetFileSize 0x0A
#define CmdGetFileTime 0x0B
#define CmdSetFileTime 0x0C
#define CmdCreateDir 0x0D
#define CmdRemoveDir 0x0E
#define CmdCopyFile 0x0F
#define CmdDeleteFile 0x10
#define CmdFdFirstFile 0x11
#define CmdFdNextFile 0x12
#define CmdFdClose 0x13
#define CmdEnd 0x80
HINSTANCE g_hInst; // The current instance
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
#define SET 1
#define CLEAR 0
uint32 RTS,DTR;//1 set,0 clear
volatile HANDLE m_hComPort = (HANDLE)NULL;
HWND InitDlg (HINSTANCE, int);
LRESULT CALLBACK DlgProc (HWND, UINT, WPARAM, LPARAM);
HANDLE ComOpen(uint32 com,uint32 baud);
void ComClose(HANDLE hCom);
static DWORD WINAPI ReadThread( PVOID pContext );
char* Wstr2str(TCHAR *wstr, char* str);
LPWSTR str2Wstr(char *str, TCHAR* buf);
CFile gFile;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_COMM);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// It is important to call this function so that the application
// will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_COMM));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[] = _T("Com test"); // The title bar text
TCHAR szWindowClass[] = _T("Com test"); // The window class name
hInst = hInstance; // Store instance handle in our global variable
MyRegisterClass(hInstance, szWindowClass);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hwndCB)
CommandBar_Show(hwndCB, TRUE);
return TRUE;
}
void ReSetComm(HWND hWnd)
{
int com, baud;
HWND hList;
hList = GetDlgItem(hWnd,ID_COMBO_COM);
com = SendMessage(hList,CB_GETCURSEL,0,0);
hList = GetDlgItem(hWnd,ID_COMBO_BAUD);
baud = SendMessage(hList,CB_GETCURSEL,0,0);
if(m_hComPort)
CloseHandle(m_hComPort);
m_hComPort = ComOpen(com,baud);
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt;
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
HWND hEdit, hButton;
TCHAR szTitle[8];
uint32 lbyte,ubyte,i,len;
uint32 textlen;
TCHAR * buf;
char str[1024];
switch (message)
{
case WM_COMMAND:
lbyte = 0;
ubyte = 0;
lbyte = wParam&0x0000ffff;
ubyte = (wParam&0xffff0000)/0x10000;
switch(lbyte)
{
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
case ID_COM_OPEN:
hEdit = GetDlgItem(hWnd, ID_SEND_DATA);
SetWindowText(hEdit, _T("OPEN"));
ReSetComm(hWnd);
break;
case ID_COM_CLOSE:
hEdit = GetDlgItem(hWnd, ID_SEND_DATA);
SetWindowText(hEdit, _T("CLOSE"));
ComClose(m_hComPort);
m_hComPort = (HANDLE)NULL;
break;
case ID_SEND_COMMAND:
hEdit = GetDlgItem(hWnd, ID_SEND_DATA);
textlen = GetWindowTextLength(hEdit);
if(textlen)
{
buf = (TCHAR *)malloc((textlen + 1) * 2);
GetWindowText(hEdit, buf, (textlen + 1) * 2);
if( m_hComPort != (HANDLE)NULL)
WriteFile(m_hComPort, Wstr2str(buf, str), strlen(Wstr2str(buf, str)) + 1, &len, NULL);
free(buf);
}
break;
case ID_SEND_CLEAR:
hEdit = GetDlgItem(hWnd, ID_SEND_DATA);
SetWindowText(hEdit, _T(""));
break;
case ID_RECEIVE_CLEAR:
hEdit = GetDlgItem(hWnd, ID_RECEIVE_DATA);
SetWindowText(hEdit, _T(""));
break;
case ID_SET_RTS:
hButton = GetDlgItem(hWnd, ID_SET_RTS);
GetWindowText(hButton, szTitle, sizeof(szTitle));
if(!wcscmp(szTitle, _T("Set RTS")))
{
RTS = SET;
SetWindowText(hButton, _T("Clear RTS"));
}
else
{
RTS = CLEAR;
SetWindowText(hButton, _T("Set RTS"));
}
ReSetComm(hWnd);
break;
case ID_SET_DTR:
hButton = GetDlgItem(hWnd, ID_SET_DTR);
GetWindowText(hButton, szTitle, sizeof(szTitle));
if(!wcscmp(szTitle, _T("Set DTR")))
{
DTR = SET;
SetWindowText(hButton, _T("Clear DTR"));
}
else
{
DTR = CLEAR;
SetWindowText(hButton, _T("Set DTR"));
}
ReSetComm(hWnd);
break;
case ID_GET_CTS:
//TODO what ???
break;
case ID_GET_DSR:
//TODO what ???
break;
}
break;
case WM_CREATE:
{
HWND hButton,hList;
char* szCOM[]= {{"COM1"},{"COM2"},{"COM3"},{"COM4"},{"COM5"},{"COM6"},{"COM7"},{"COM8"}};
char* szBAUD[]= {{"2400"},{"4800"},{"9600"},{"19200"},{"38400"},{"57600"},{"115200"},{"230400"},{"460800"}};
TCHAR buf[1024];
hList = CreateWindow(_T("COMBOBOX"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER |CBS_DROPDOWNLIST ,
10, 10, 160, 220, hWnd, (HMENU)ID_COMBO_COM,
g_hInst, NULL);
for(i=0;i<8;i++)
SendMessage (hList, CB_INSERTSTRING, i, (LPARAM) str2Wstr((char*)szCOM[i], buf));
SendMessage(hList,CB_SETCURSEL,0,0);
hButton = CreateWindow(_T("BUTTON"),_T("OPEN"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
180, 10, 50, 30, hWnd, (HMENU)ID_COM_OPEN,
g_hInst, NULL );
hList = CreateWindow(_T("COMBOBOX"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER |CBS_DROPDOWNLIST ,
10, 40, 160, 220, hWnd, (HMENU)ID_COMBO_BAUD,
g_hInst, NULL);
for(i=0;i<9;i++)
SendMessage (hList, CB_INSERTSTRING, i, (LPARAM) str2Wstr((char*)szBAUD[i], buf));
SendMessage(hList,CB_SETCURSEL,0,0);
hButton = CreateWindow(_T("BUTTON"),_T("CLOSE"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
180, 40, 50, 30, hWnd, (HMENU)ID_COM_CLOSE,
g_hInst, NULL );
hEdit = CreateWindow(_T("EDIT"), NULL,
WS_VISIBLE | WS_BORDER |ES_MULTILINE |ES_LEFT ,
10, 70, 160, 70, hWnd, (HMENU)ID_SEND_DATA,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("SEND"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
180, 80, 50, 30, hWnd, (HMENU)ID_SEND_COMMAND,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("CLEAR"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
180, 110, 50, 30, hWnd, (HMENU)ID_SEND_CLEAR,
g_hInst, NULL);
hEdit = CreateWindow(_T("EDIT"), NULL,
WS_VISIBLE | WS_BORDER |ES_MULTILINE |ES_LEFT | ES_AUTOHSCROLL | ES_READONLY ,
10, 150, 220, 60, hWnd, (HMENU)ID_RECEIVE_DATA,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("CLEAR"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 10, 70, 30, hWnd, (HMENU)ID_RECEIVE_CLEAR,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("Get CTS"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 40, 70, 30, hWnd, (HMENU)ID_RECEIVE_CLEAR,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("Get DSR"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 80, 70, 30, hWnd, (HMENU)ID_RECEIVE_CLEAR,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("Set RTS"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 110, 70, 30, hWnd, (HMENU)ID_SET_RTS,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("Set DTR"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 140, 70, 30, hWnd, (HMENU)ID_SET_DTR,
g_hInst, NULL);
hButton = CreateWindow(_T("BUTTON"), _T("Exit"),
WS_CHILD | WS_VISIBLE | BS_CENTER ,
240, 180, 70, 30, hWnd, (HMENU)IDM_FILE_EXIT,
g_hInst, NULL);
}
break;
/*
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
break;
*/
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
rt.top = 210;
rt.left = 320;
rt.bottom = 240;
rt.right = 10;
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
DrawText(hdc, szHello, _tcslen(szHello), &rt, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -