问题描述
- VC++6.0开发工具,打开串口,重叠IO操作,提示有访问冲突,求大神指点
- #include
#includeLRESULT CALLBACK WndProc (HWND hwnd UINT msg WPARAM wParam LPARAM lParam);
#define WM_TRANSFINISHED (WM_USER + 1)
typedef struct
{
HWND hwnd;
HANDLE hComm;
}PARAMS *PPARAMS;int WINAPI WinMain (HINSTANCE hInstance HINSTANCE hPreInstance LPSTR lpCmdLine int nShowCmd)
{
char* szAppName = ""TEST"";
MSG msg;
HWND hwnd;
WNDCLASS wndclass;wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hIcon = LoadIcon (0 IDI_APPLICATION);wndclass.hCursor = LoadCursor (0 IDC_ARROW);wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);wndclass.hInstance = hInstance;wndclass.lpszClassName = szAppName;wndclass.lpszMenuName = NULL;if (!RegisterClass (&wndclass)) MessageBox (NULLThe window class is wrong!"" szAppName MB_OK);hwnd = CreateWindow (szAppNameFirst Show!"" WS_OVERLAPPEDWINDOW CW_USEDEFAULT CW_USEDEFAULT CW_USEDEFAULT CW_USEDEFAULT NULL NULL hInstance NULL);ShowWindow (hwnd nShowCmd);UpdateWindow (hwnd);while (GetMessage (&msg NULL 0 0)){ TranslateMessage (&msg); DispatchMessage (&msg);}return msg.wParam;
}
VOID ThreadProc (PVOID pvoid)
{
BOOL fWait;
DWORD Event;
PPARAMS pparams;
OVERLAPPED OverlapWait;pparams = (PPARAMS)pvoid;OverlapWait.hEvent = CreateEvent (NULL TRUE TRUE NULL);fWait = WaitCommEvent (pparams->hComm &Event &OverlapWait);fWait = GetOverlappedResult (pparams->hComm &OverlapWait NULL TRUE);if (fWait) if (EV_TXEMPTY == Event) { SendMessage (pparams->hwnd WM_TRANSFINISHED 0 0); }
}
LRESULT CALLBACK WndProc (HWND hwnd UINT message WPARAM wParam LPARAM lParam)
{
static char* name[5] = {""f1(K)""f2(K)""RU/RD""PHASE""OTHER""};
static HINSTANCE hInstance;
static int cxChar cyChar cxClient cyClient;
static HWND hwndEdit[5] hwndButton hwndText[5];
int i;
PAINTSTRUCT ps;
HDC hdc;char temp[13];float data[5];char* trans;DWORD cbToWrite = 20 cbWritten;static HANDLE hCom;static OVERLAPPED OverlapWrite;DCB myDCB;COMMTIMEOUTS timeout = {0 0 0 30 30};static PARAMS params;switch (message){case WM_CREATE: hInstance = (HINSTANCE)GetWindowLong (hwnd GWL_HINSTANCE); hwndButton = CreateWindow (""button""SEND"" WS_CHILD | WS_VISIBLE | WS_BORDER hwnd (HMENU)0 hInstance NULL); for (i = 1; i < 6; i++) { hwndEdit[i - 1] = CreateWindow (""edit""0"" WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_NUMBER hwnd (HMENU)i hInstance NULL); hwndText[i - 1] = CreateWindow (""static"" name[i-1] WS_CHILD | WS_VISIBLE | SS_CENTER hwnd (HMENU)(i + 5) hInstance NULL); } return 0;case WM_SIZE: cxClient = LOWORD (lParam); cyClient = HIWORD (lParam); cxChar = LOWORD (GetDialogBaseUnits ()); cyChar = HIWORD (GetDialogBaseUnits ()); MoveWindow (hwndButton cxChar*2 cyChar*2 cxChar*8 cyChar*3/2 TRUE); for (i = 0; i < 5; i++) { MoveWindow (hwndEdit[i] cxChar*12 cyChar*(5 + 3*i) cxChar*8 cyChar*3/2 TRUE); MoveWindow (hwndText[i] cxChar*2 cyChar*(5 + 3*i) cxChar*8 cyChar*3/2 TRUE); } return 0;case WM_CTLCOLORSTATIC: SetBkColor ((HDC)wParam GetSysColor (COLOR_BTNHIGHLIGHT)); return (LRESULT) (HBRUSH)GetStockObject (WHITE_BRUSH);case WM_CTLCOLORBTN: SetBkColor ((HDC)wParam GetSysColor (COLOR_BTNHIGHLIGHT)); return (LRESULT) (HBRUSH)GetStockObject (WHITE_BRUSH);case WM_PAINT: hdc = BeginPaint (hwnd &ps); EndPaint (hwnd &ps); return 0;case WM_DESTROY: GetWindowText (hwndEdit[0] temp 13); MessageBox (NULL tempOK"" MB_OK); PostQuitMessage (0); return 0;case WM_TRANSFINISHED: EnableWindow (hwndButton TRUE); CloseHandle (hCom); MessageBox (NULLTransform done successfully!""Comm"" MB_OK); return 0;case WM_COMMAND: if (hwndButton == (HWND)lParam && (HIWORD (wParam) == BN_CLICKED || HIWORD (wParam) == BN_DBLCLK)) { EnableWindow (hwndButton FALSE); for (i = 0; i < 5; i++) { GetWindowText (hwndEdit[i] temp 13); data[i] = atof (temp); } trans = (char*) data; hCom = CreateFile (""COM3"" GENERIC_WRITE 0 NULL OPEN_EXISTING FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED NULL); if (hCom == INVALID_HANDLE_VALUE) { MessageBox (NULLCANNOT OPEN THE PORT!""Comm"" MB_OK); return 0; } SetupComm (hCom 1024 512); GetCommState (hCom &myDCB); myDCB.BaudRate = 9600; myDCB.fBinary = TRUE; myDCB.fParity = FALSE; myDCB.ByteSize = 8; myDCB.Parity = NOPARITY; myDCB.StopBits = ONESTOPBIT; if (!SetCommState (hCom &myDCB)) { MessageBox (NULL TEXT (""port set failure"") NULL MB_OK); return 0; } if (!SetCommTimeouts (hCom &timeout)) { MessageBox (NULL TEXT (""Timeout set failure"") NULL MB_OK); return 0; } if (!SetCommMask (hCom EV_TXEMPTY)) { MessageBox (NULL TEXT (""event set failure"") NULL MB_OK); return 0; } OverlapWrite.hEvent = CreateEvent (NULL TRUE TRUE NULL); params.hComm = hCom; params.hwnd = hwnd; _beginthread(ThreadProc 0 ¶ms); //WriteFile (hCom trans cbToWrite &cbWritten &OverlapWrite); WriteFile (hComa"" 1 &cbWritten &OverlapWrite); GetOverlappedResult (hCom &OverlapWrite NULL TRUE); } return 0;}return DefWindowProc (hwnd message wParam lParam);
}