Chinaunix首页 | 论坛 | 博客
  • 博客访问: 212974
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: WINDOWS

2006-07-18 18:49:16

#include 
#include 

HANDLE hEvent;
HANDLE hMutex;
char buffer[1024];
int counts;

HANDLE hMutexR;
char b[1024];

typedef struct which
{
    OVERLAPPED overlap;
    int dir;
which;
#define DIRI    0
#define DIRO    1

which inout;

int
initialize_serial_port(HANDLE hFileint speedchar *initialize_string)
{
    DCB dcb;

    if (!GetCommState(hFile, &dcb))
    {
        return -1;
    }
    dcb.fBinary = TRUE;
    dcb.BaudRate = speed;

    // n,8,1
    dcb.fParity = FALSE;
    dcb.Parity = 0;
    dcb.ByteSize = 8;
    dcb.StopBits = 0;
    dcb.fInX  = TRUE;
    dcb.fOutX = TRUE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fOutxCtsFlow = FALSE;
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;

    if (!SetCommState(hFile, &dcb))
    {
        return -1;
    }

    if (!SetupComm(hFile10241024))
    {
        return -1;
    }

    PurgeComm(hFilePURGE_RXABORT | PURGE_RXCLEAR);

    return 0;
}

DWORD WINAPI myiocp(LPVOID param)
{
    HANDLE hPort = (HANDLE)param;
    DWORD bytes;
    ULONG_PTR key;
    which *use;
    LPOVERLAPPED overlap;

    while (TRUE)
    {
        GetQueuedCompletionStatus(hPort, &bytes, &key, &overlapINFINITE);
        use = (which *)overlap;
        if (use->dir == DIRI)
        {
            size_t ilengthfound;
            char x[512], *p;

            WaitForSingleObject(hMutexRINFINITE);
            
            for (;;)
            {
                found = 0length = strlen(b);
                for (p = xi = 0i < length && !found; ++i)
                {
                    switch (b[i])
                    {
                    case '\r':
                        break;
                    case '\n':
                        *p = '\0';
                        found = 1;
                        break;
                    default:
                        *p++ = b[i];
                        break;
                    }
                }
                if (found)
                {
                    if (x[0]) printf("%s\n"x);
                    length -= i;
                    if (lengthMoveMemory(bb + ilength);
                    ZeroMemory(b + lengthsizeof(b) - length);
                    if (!strcmp(x"OK"))
                    {
                        WaitForSingleObject(hMutexINFINITE);
                        strcpy(buffer + strlen(buffer), "AT\r");
                        if (++counts == 8)
                        {
                            SetEvent(hEvent);
                        }
                        else if (strlen(buffer) == 3)
                        {
                            ZeroMemory(&use->overlapsizeof(use->overlap));
                            WriteFile((HANDLE)keybufferstrlen(buffer), &bytes, (LPOVERLAPPED)&out);
                        }
                        ReleaseMutex(hMutex);
                    }
                }
                if (!foundbreak;
            }
            ZeroMemory(&use->overlapsizeof(use->overlap));
            ReadFile((HANDLE)keyb + strlen(b), sizeof(b) - strlen(b) - 1, &bytes, &use->overlap);
            ReleaseMutex(hMutexR);
        }
        else
        {
            WaitForSingleObject(hMutexINFINITE);
            printf("write %d bytes: %.*sw\n"bytesbytesbuffer);
            MoveMemory(bufferbuffer + bytesstrlen(buffer + bytes) + 1);
            if (strlen(buffer) != 0)
            {
                ZeroMemory(&use->overlapsizeof(use->overlap));
                WriteFile((HANDLE)keybufferstrlen(buffer), &bytes, (LPOVERLAPPED)&out);
            }
            ReleaseMutex(hMutex);
        }
    }
}

int
main(int argcchar *argv[])
{
    int i;
    HANDLE hFilehPorthThread;
    DWORD bytes;

    hEvent = CreateEvent(NULLFALSEFALSENULL);
    hMutex = CreateMutex(NULLFALSENULL);
    hMutexR = CreateMutex(NULLFALSENULL);
    hFile = CreateFile("\\COM4"FILE_ALL_ACCESS0NULLOPEN_EXISTINGFILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPEDINVALID_HANDLE_VALUE);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        perror("\\COM4");
        return 1;
    }

    if (initialize_serial_port(hFile96000) < 0)
    {
        perror("initialize");
        return 1;
    }

    hPort = CreateIoCompletionPort(INVALID_HANDLE_VALUENULL00);
    if (!hPort)
    {
        printf("error createiocom\n");
        return 1;
    }

    CreateIoCompletionPort(hFilehPort, (ULONG_PTR)hFile0);
    in.dir = DIRI;
    out.dir = DIRO;

    for (i = 0i < 10; ++i)
    {
        DWORD id;

        hThread = CreateThread(NULL0myiocphPort0, &id);
        CloseHandle(hThread);
    }

    strcpy(buffer"AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0\r");
    bytes = -1;
    WriteFile(hFilebufferstrlen(buffer), &bytes, &out.overlap);
    ReadFile(hFilebsizeof(b), &bytes, &in.overlap);
    printf("bytes = %d\n"bytes);

    WaitForSingleObject(hEventINFINITE);

    CloseHandle(hFile);
    printf("Press any key to continue ...\n");
    _getch();

    return 0;

}
文件: iocp.rar
大小: 1KB
下载: 下载
阅读(758) | 评论(0) | 转发(0) |
0

上一篇:Windows ODBC example

下一篇:queue(3)

给主人留下些什么吧!~~