2 u013743516 u013743516 于 2014.09.05 18:20 提问

VC++6.0开发工具,打开串口,重叠IO操作,提示有访问冲突,求大神指点

#include
#include

LRESULT 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 (NULL, "The window class is wrong!", szAppName, MB_OK);

hwnd = CreateWindow (szAppName, "First 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,
                  0, 0, 0, 0,
                  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,
                                        0, 0, 0, 0,
                                        hwnd, (HMENU)i, hInstance, NULL);
        hwndText[i - 1] = CreateWindow ("static", name[i-1], WS_CHILD | WS_VISIBLE | SS_CENTER ,
                                        0, 0, 0, 0,
                                        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, temp, "OK", MB_OK);
    PostQuitMessage (0);
    return 0;

case WM_TRANSFINISHED:
    EnableWindow (hwndButton, TRUE);
    CloseHandle (hCom);
    MessageBox (NULL, "Transform 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 (NULL, "CANNOT 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, &params);

        //WriteFile (hCom, trans, cbToWrite, &cbWritten, &OverlapWrite);
        WriteFile (hCom, "a", 1, &cbWritten, &OverlapWrite);
        GetOverlappedResult (hCom, &OverlapWrite, NULL, TRUE);
    }

    return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
win32串口同步编程、异步编程(重叠IO)
在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信。串口通信方便易行,应用广泛。 一般情况下,工控机和各智能仪表通过RS485总线进行通信。RS485的通信方式是半双工的,只能由作为主节点的工控PC机依次轮询网络上的各智能控制单元子节点。每次通信都是由PC机通过串口向智能控制单元发布命令,智能控制单元在接收到正确的命令后作出应答。   在Win32下,可以使
异步重叠操作之串口
在工控行业,目前总线型通信依然占据半壁江山。常用的如485,CAN等。而面对小型系统时,尤其监控区域遍布在两公里范围内时,通过485-232或者CAN-232转换模块,使用上位机的串口与下属硬件通信组成系统便成了相对节约的一种设计方案。          本文的重点在于讲解异步重叠操作串口的思想以及过程。          所有的IO设备的操作都遵循:创建、参数设置、读写、关闭三个步骤。
〖997〗-重叠 I/O 操作在进行中 的原因和解决方法
原因:其他程序正在占用串口 解决:停到其他占用串口的程序
串口开发中重叠操作要注意的一点
ReadFile在开始一个重叠操作时,会把OVERLAPPED结构中的Event对象重设未 non-signaled状态.在读取的重叠操作时,ReadFile指定的读取字节数和实际要接受的字节数不要相差太大,如果指定的数太大,则操作"GetOverlappedResult(ghComm,&oRead,&dwByteRead,TRUE);"会等待较长时间.ReadFile 的重叠操作,在两者情形下正
重叠IO的四种机制
http://www.cnblogs.com/my_life/articles/1982091.html  说到重叠模型首先还是提一下异步IO比较好,因为从本质上讲,重叠模型也是一种异步IO模型。        我们知道,相对于计算机执行的其他操作而言,设备IO(文件、管道、套接字等)是比较慢的。于是在多线程结构中就考虑到采用异步的方式进行设备读写操作,即我们告诉系统对设备的读写数据,而同时应
安装SQL Server提示“重叠的IO操作正在进行”解决
单位新做了一个虚机,打算部署一套.Net SQL 的系统 系统是Server 2008 R2,机器除了系统,其他软件都没有。 所以需要安装SQL Server啊,.Net环境啊,配置IIS==   恰巧的事情啊,上面给了我一个域账户去访问这个虚机,管理员权限也给了 但是呢,安装SQL Server 2008 R2的时候,到安装支持程序的卡关处,没过几分钟就出错,提示“重叠的IO操作正在进
C/C++串口通信(2)-重叠操作
重叠操作时,操作还未完成函数就返回。 重叠I/O非常灵活,它也可以实现阻塞。有两种方法可以等待操作完成: 一种方法是用象WaitForSingleObject这样的等待函数来等待OVERLAPPED结构的hEvent成员; 另一种方法是调用GetOverlappedResult函数等待。 //OVERLAPPED结构 typedef struct _OVERLAPPED { // o DW
什么是重叠操作
也叫异步操作,若调用的函数立即返回则此操作就叫重叠操作。   背景: 1.在串口通信时,使用ReadFile和WriteFile时,既可以同步操作也可以执行重叠操作。在异步操作时要用到OVERLAPPED结构,此结构中包含通知事件的句柄。   在使用CreateFile时就可指定是用同步方式还是异步方式,如:     hCom = CreateFile( "COM1", GENERIC_
安装SEP时报Error 997:重叠I/O操作在进行中
安装SEP的时候可能遇到报错:
VC网络编程模型示例 - 重叠IO模型
VC网络编程模型示例之 - 重叠IO模型 配套讲解请参考我的blog http://blog.csdn.net/PiggyXP