📄 serialcomm.cpp
字号:
// SerialComm.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "SerialComm.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100
// 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);
// Vincent added
#define TEXTSIZE 256
LRESULT CALLBACK DoDemo (HWND, UINT, WPARAM, LPARAM);
HANDLE InitCommunication (HWND hWnd, LPTSTR pszDevName, INT nSpeed);
DWORD ReadThread (PVOID pArg);
DWORD SendThread (PVOID pArg);
HANDLE hComPort = INVALID_HANDLE_VALUE;
HANDLE g_hSendEvent = INVALID_HANDLE_VALUE;
HANDLE hReadThread = INVALID_HANDLE_VALUE;
BOOL fContinue = TRUE;
int TermInstance (HINSTANCE hInstance, int nDefRC);
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_SERIALCOMM);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return TermInstance (hInstance, msg.wParam);
// 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_SERIALCOMM));
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[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_SERIALCOMM, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
g_hSendEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
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;
}
//
// 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)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
// Vincent added
DialogBox(hInst, (LPCTSTR)IDD_DIALOG_DEMO, hWnd, (DLGPROC)DoDemo);
DestroyWindow(hWnd);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
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))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// ================================
// Vincent added all codes below
// ================================
LRESULT CALLBACK DoDemo(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
INT nSpeed;
TCHAR szDev[32];
HANDLE hThread;
DWORD rc;
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);
}
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("COM1:"));
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("COM2:"));
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("COM3:"));
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_SETCURSEL, 1, 0);
SendDlgItemMessage(hDlg, IDC_COMBO_BAUD_RATE, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("9600"));
SendDlgItemMessage(hDlg, IDC_COMBO_BAUD_RATE, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("19200"));
SendDlgItemMessage(hDlg, IDC_COMBO_BAUD_RATE, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("38400"));
SendDlgItemMessage(hDlg, IDC_COMBO_BAUD_RATE, CB_INSERTSTRING , -1, (LPARAM)(LPCSTR)TEXT("115200"));
SendDlgItemMessage(hDlg, IDC_COMBO_BAUD_RATE, CB_SETCURSEL, 1, 0);
hThread = CreateThread (NULL, 0, SendThread, hDlg, 0, &rc);
if (hThread)
CloseHandle (hThread);
else
{
EndDialog(hDlg, LOWORD(wParam));
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
else if (LOWORD(wParam) == IDC_BUTTON_OPEN)
{
switch (SendDlgItemMessage (hDlg, IDC_COMBO_BAUD_RATE, CB_GETCURSEL, 0, 0))
{
case 0 :
nSpeed = CBR_9600;
break;
case 1 :
nSpeed = CBR_19200;
break;
case 2 :
nSpeed = CBR_38400;
break;
case 3 :
nSpeed = CBR_115200;
break;
default :
break;
}
switch (SendDlgItemMessage (hDlg, IDC_COMBO_COM_PORT, CB_GETCURSEL, 0, 0))
{
case 0 :
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_GETLBTEXT, 0, (LPARAM)szDev);
break;
case 1 :
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_GETLBTEXT, 1, (LPARAM)szDev);
break;
case 2 :
SendDlgItemMessage(hDlg, IDC_COMBO_COM_PORT, CB_GETLBTEXT, 2, (LPARAM)szDev);
break;
default :
break;
}
if (InitCommunication(hDlg, szDev, nSpeed) == INVALID_HANDLE_VALUE)
MessageBox(hDlg, TEXT("Open COM port failed"), TEXT("Open COM port"), MB_OK);
}
else if (LOWORD(wParam) == ID_SENDBTN)
{
SetEvent (g_hSendEvent);
SetFocus (GetDlgItem (hDlg, ID_SENDTEXT));
}
else
{
}
break;
}
return FALSE;
}
HANDLE InitCommunication (HWND hWnd, LPTSTR pszDevName, INT nSpeed) {
DCB dcb;
TCHAR szDbg[128];
COMMTIMEOUTS cto;
HANDLE hLocal;
DWORD dwTStat;
hLocal = hComPort;
hComPort = INVALID_HANDLE_VALUE;
if (hLocal != INVALID_HANDLE_VALUE)
CloseHandle (hLocal); // This causes WaitCommEvent to return.
// The com port name is the last 5 characters of the string.
hLocal = CreateFile (pszDevName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if (hLocal != INVALID_HANDLE_VALUE) {
// Configure port.
GetCommState (hLocal, &dcb);
dcb.BaudRate = nSpeed;
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
SetCommState (hLocal, &dcb);
// Set the timeouts. Set infinite read timeout.
cto.ReadIntervalTimeout = 0;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 0;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hLocal, &cto);
wsprintf (szDbg, TEXT ("Port %s opened\r\n"), pszDevName);
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
(LPARAM)szDbg);
// Start read thread if not already started.
hComPort = hLocal;
if (!GetExitCodeThread (hReadThread, &dwTStat) ||
(dwTStat != STILL_ACTIVE)) {
hReadThread = CreateThread (NULL, 0, ReadThread, hWnd,
0, &dwTStat);
if (hReadThread)
CloseHandle (hReadThread);
}
} else {
wsprintf (szDbg, TEXT ("Couldn\'t open port %s. rc=%d\r\n"),
pszDevName, GetLastError());
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL,
0, (LPARAM)szDbg);
}
return hComPort;
}
DWORD SendThread (PVOID pArg) {
HWND hWnd, hwndSText;
DWORD cBytes, rc;
TCHAR szText[TEXTSIZE], szOutputText[TEXTSIZE];
hWnd = (HWND)pArg;
hwndSText = GetDlgItem (hWnd, ID_SENDTEXT);
while (1) {
rc = WaitForSingleObject (g_hSendEvent, INFINITE);
if (rc == WAIT_OBJECT_0) {
if (!fContinue)
{
break;
}
// Disable send button while sending
EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), FALSE);
GetWindowText (hwndSText, szText, dim(szText));
lstrcat (szText, TEXT ("\r\n"));
rc = WriteFile (hComPort, szText,
lstrlen (szText)*sizeof (TCHAR), &cBytes, 0);
if (rc) {
// Copy sent text to output window -> recieve text box
wsprintf(szOutputText, TEXT("Send -> "));
lstrcat (szOutputText, szText);
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0, (LPARAM)szOutputText);
SetWindowText (hwndSText, TEXT ("")); // Clear send text box
} else
// Else, print error message
{
wsprintf (szText, TEXT ("Send failed rc=%d\r\n"),
GetLastError());
}
EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), TRUE);
} else
{
MessageBox(hWnd, TEXT("Time out"), TEXT("SendThread"), MB_OK);
break;
}
}
return 0;
}
//======================================================================
// ReadThread - Receives characters from the serial port
//
DWORD ReadThread (PVOID pArg) {
HWND hWnd;
int i;
DWORD cBytes;
BYTE szText[TEXTSIZE], *pPtr;
TCHAR tch, szOutputText[TEXTSIZE];
hWnd = (HWND)pArg;
while (fContinue) {
tch = 0;
pPtr = szText;
for (i = 0; i < sizeof (szText)-sizeof (TCHAR); i++) {
while (!ReadFile (hComPort, pPtr, 1, &cBytes, 0))
if (hComPort == INVALID_HANDLE_VALUE)
return 0;
// This syncs the proper byte order for Unicode.
tch = (tch << 8) & 0xff00;
tch |= *pPtr++;
if (tch == TEXT ('\n'))
break;
}
*pPtr++ = 0; // Avoid alignment problems by addressing as bytes.
*pPtr++ = 0;
// If out of byte sync, move bytes down one.
if (i % 2) {
pPtr = szText;
while (*pPtr || *(pPtr+1)) {
*pPtr = *(pPtr+1);
pPtr++;
}
*pPtr = 0;
}
wsprintf(szOutputText, TEXT("Recieved <- "));
lstrcat (szOutputText, (unsigned short *)szText);
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
(LPARAM)szOutputText);
}
return 0;
}
int TermInstance (HINSTANCE hInstance, int nDefRC) {
HANDLE hPort = hComPort;
fContinue = FALSE;
hComPort = INVALID_HANDLE_VALUE;
if (hPort != INVALID_HANDLE_VALUE)
CloseHandle (hPort);
if (g_hSendEvent != INVALID_HANDLE_VALUE) {
PulseEvent (g_hSendEvent);
Sleep(100);
CloseHandle (g_hSendEvent);
}
return nDefRC;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -