|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<windows.h>
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HELLO");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = wndproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("窗口注册失败!!!"), TEXT("警告"), MB_OK);
return(0);
}
hwnd = CreateWindow
(szAppName,
TEXT("THE HELLO"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
DCB dcb;
COMMTIMEOUTS timeouts;
BOOL Write;
static HANDLE handle;
DWORD dwsize = 0;
char sbuff[] = {7};
switch (message)
{
case WM_CREATE:
handle = CreateFile(
TEXT("COM1"),
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == handle)
{
MessageBox(NULL, TEXT("串口打开失败!!!"), TEXT("警告"), MB_OK);
DestroyWindow(hwnd);
return(0);
}
SetupComm(handle, 256, 256);
GetCommState(handle, &dcb);
dcb.BaudRate = 9600;
dcb.DCBlength = sizeof(dcb);
dcb.fBinary = 1;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.fErrorChar = 0;
dcb.EvtChar = 0;
dcb.fNull = 0;
dcb.fAbortOnError = 0;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.XonLim = 2;
dcb.XoffLim = 5;
dcb.XoffChar = 0x19;
dcb.XonChar = 0x13;
SetCommMask(handle, EV_RXCHAR);
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 2000;
SetCommTimeouts(handle, &timeouts);
return(0);
case WM_LBUTTONDOWN:
if (INVALID_HANDLE_VALUE != handle)
{
Write=WriteFile(handle,
sbuff,
1,
&dwsize,
NULL);
MessageBox(NULL, TEXT("发送完成"), TEXT("警告"), MB_OK);
if (!Write)
{
MessageBox(NULL, TEXT("写入失败"), TEXT("警告"), MB_OK);
}
PurgeComm(handle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
return(0);
}
return(0);
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
LineTo(hdc, 200, 200);
EndPaint(hwnd, &ps);
return(0);
case WM_DESTROY:
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, message, wParam, lParam));
}
如果输出到TXT文件 可以正常输出 但是一旦输出到串口给单片机就是00 是不是我串口设置不对啊? |
|