⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writer.c

📁 用于串口的测试调试
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (osWrite.hEvent == NULL)
        ErrorInComm("CreateEvent (overlapped write hEvent)");

    hArray[0] = osWrite.hEvent;
    hArray[1] = ghThreadExitEvent;
    
    //
    // issue write
    //
    if (!WriteFile(COMDEV(TTYInfo), lpBuf, dwToWrite, &dwWritten, &osWrite)) {
        if (GetLastError() == ERROR_IO_PENDING) { 
            //
            // write is delayed
            //
            dwRes = WaitForMultipleObjects(2, hArray, FALSE, INFINITE);
            switch(dwRes)
            {
                //
                // write event set
                //
                case WAIT_OBJECT_0:
                            SetLastError(ERROR_SUCCESS);
                            if (!GetOverlappedResult(COMDEV(TTYInfo), &osWrite, &dwWritten, FALSE)) {
                                if (GetLastError() == ERROR_OPERATION_ABORTED)
                                    UpdateStatus("Write aborted\r\n");
                                else
                                    ErrorInComm("GetOverlappedResult(in Writer)");
                            }
                            
                            if (dwWritten != dwToWrite) {
                                if ((GetLastError() == ERROR_SUCCESS) && SHOWTIMEOUTS(TTYInfo))
                                    UpdateStatus("Write timed out. (overlapped)\r\n");
                                else
                                    ErrorReporter("Error writing data to port (overlapped)");
                            }
                            break;

                //
                // thread exit event set
                //
                case WAIT_OBJECT_0 + 1:
                            break;

                //                
                // wait timed out
                //
                case WAIT_TIMEOUT:
                            UpdateStatus("Wait Timeout in WriterGeneric.\r\n");
                            break;

                case WAIT_FAILED:
                default:    ErrorInComm("WaitForMultipleObjects (WriterGeneric)");
                            break;
            }
        }
        else    
            //
            // writefile failed, but it isn't delayed
            //
            ErrorInComm("WriteFile (in Writer)");
    }
    else {
        //
        // writefile returned immediately
        //
        if (dwWritten != dwToWrite)
            UpdateStatus("Write timed out. (immediate)\r\n");
    }

    CloseHandle(osWrite.hEvent);
    
    return;
}

/*-----------------------------------------------------------------------------

FUNCTION: WriterAddNewNode(DWORD, DWORD, char, char *, HANDLE, HWND)

PURPOSE: Adds a new write request packet

PARAMETERS:
    dwRequestType - write request packet request type
    dwSize        - size of write request
    ch            - character to write
    lpBuf         - address of buffer to write
    hHeap         - heap handle of data buffer
    hProgress     - hwnd of transfer progress bar

RETURN:
    TRUE if node is added to linked list
    FALSE if node can't be allocated.

COMMENTS: Allocates a new packet and fills it based on the
          parameters passed in.

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL WriterAddNewNode(  DWORD dwRequestType, 
                        DWORD dwSize, 
                        char ch, 
                        char * lpBuf, 
                        HANDLE hHeap, 
                        HWND hProgress)
{
    PWRITEREQUEST pWrite;

    //
    // allocate new packet
    //
    pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
    if (pWrite == NULL) {
        ErrorReporter("HeapAlloc (writer packet)");
        return FALSE;
    }

    //
    // assign packet info
    //
    pWrite->dwWriteType  = dwRequestType;
    pWrite->dwSize       = dwSize;
    pWrite->ch           = ch;
    pWrite->lpBuf        = lpBuf;
    pWrite->hHeap        = hHeap;
    pWrite->hWndProgress = hProgress;

    AddToLinkedList(pWrite);
    
    return TRUE;
}

/*-----------------------------------------------------------------------------

FUNCTION: WriterAddNewNodeTimeout(DWORD, DWORD, char, char *, 
                                    HANDLE, HWND, DWORD)

PURPOSE: Adds a new write request packet, timesout if can't allocate packet.

PARAMETERS:
    dwRequestType - write request packet request type
    dwSize        - size of write request
    ch            - character to write
    lpBuf         - address of buffer to write
    hHeap         - heap handle of data buffer
    hProgress     - hwnd of transfer progress bar
    dwTimeout     - timeout value for waiting

RETURN:
    TRUE if node is added to linked list
    FALSE if node can't be allocated.

COMMENTS: Allocates a new packet and fills it based on the
          parameters passed in.  If the first attemp to allocate packet
          fails, then the function sleeps and tries again when it resumes.

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL WriterAddNewNodeTimeout(   DWORD dwRequestType, 
                                DWORD dwSize, 
                                char ch, 
                                char * lpBuf, 
                                HANDLE hHeap, 
                                HWND hProgress,
                                DWORD dwTimeout  )
{
    PWRITEREQUEST pWrite;

    //
    // attempt first allocation
    //
    pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
    if (pWrite == NULL) {
        Sleep(dwTimeout);
        //
        // attempt second allocation
        //
        pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
        if (pWrite == NULL) {
            ErrorReporter("HeapAlloc (writer packet)");
            return FALSE;
        }
    }

    //
    // assign packet info
    //
    pWrite->dwWriteType  = dwRequestType;
    pWrite->dwSize       = dwSize;
    pWrite->ch           = ch;
    pWrite->lpBuf        = lpBuf;
    pWrite->hHeap        = hHeap;
    pWrite->hWndProgress = hProgress;

    AddToLinkedList(pWrite);
    
    return TRUE;
}

/*-----------------------------------------------------------------------------

FUNCTION: WriterAddFirstNodeTimeout(DWORD, DWORD, char, char *, 
                                    HANDLE, HWND, DWORD)

PURPOSE: Adds a new write request packet and places it at the front of the 
         list, timesout if can't allocate packet.

PARAMETERS:
    dwRequestType - write request packet request type
    dwSize        - size of write request
    ch            - character to write
    lpBuf         - address of buffer to write
    hHeap         - heap handle of data buffer
    hProgress     - hwnd of transfer progress bar
    dwTimeout     - timeout value for waiting

RETURN:
    TRUE if node is added to linked list
    FALSE if node can't be allocated.

COMMENTS: This function differs from WriterAddNewNodeTimeout only in that
          it places the node at the front of the list.

HISTORY:   Date:      Author:     Comment:
            1/26/96   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL WriterAddFirstNodeTimeout(   DWORD dwRequestType, 
                                DWORD dwSize, 
                                char ch, 
                                char * lpBuf, 
                                HANDLE hHeap, 
                                HWND hProgress,
                                DWORD dwTimeout  )
{
    PWRITEREQUEST pWrite;

    //
    // attempt first allocation
    //
    pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
    if (pWrite == NULL) {
        Sleep(dwTimeout);
        //
        // attempt second allocation
        //
        pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
        if (pWrite == NULL) {
            ErrorReporter("HeapAlloc (writer packet)");
            return FALSE;
        }
    }

    //
    // assign packet info
    //
    pWrite->dwWriteType  = dwRequestType;
    pWrite->dwSize       = dwSize;
    pWrite->ch           = ch;
    pWrite->lpBuf        = lpBuf;
    pWrite->hHeap        = hHeap;
    pWrite->hWndProgress = hProgress;

    AddToFrontOfLinkedList(pWrite);
    
    return TRUE;
}

/*-----------------------------------------------------------------------------

FUNCTION: WriterAddExistingNode

PURPOSE: Adds a write request packet

PARAMETERS:
    dwRequestType - write request packet request type
    dwSize        - size of write request
    ch            - character to write
    lpBuf         - address of buffer to write
    hHeap         - heap handle of data buffer
    hProgress     - hwnd of transfer progress bar

RETURN: always TRUE

COMMENTS: Similar to WriterAddNewNode, except that the
          memory has already been allocated.

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL WriterAddExistingNode( PWRITEREQUEST pNode, 
                            DWORD dwRequestType, 
                            DWORD dwSize, 
                            char ch, 
                            char * lpBuf, 
                            HANDLE hHeap, 
                            HWND hProgress)
{
    //
    // assign packet info
    //
    pNode->dwWriteType  = dwRequestType;
    pNode->dwSize       = dwSize;
    pNode->ch           = ch;
    pNode->lpBuf        = lpBuf;
    pNode->hHeap        = hHeap;
    pNode->hWndProgress = hProgress;

    AddToLinkedList(pNode);
 
    return TRUE;
}

/*-----------------------------------------------------------------------------

FUNCTION: AddToLinkedList(PWRITEREQUEST)

PURPOSE: Adds a node to the write request linked list

PARAMETERS:
    pNode - pointer to write request packet to add to linked list

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void AddToLinkedList(PWRITEREQUEST pNode)
{
    PWRITEREQUEST pOldLast;
    //
    // add node to linked list
    //
    EnterCriticalSection(&gcsWriterHeap);

    pOldLast = gpWriterTail->pPrev;

    pNode->pNext = gpWriterTail;
    pNode->pPrev = pOldLast;

    pOldLast->pNext = pNode;
    gpWriterTail->pPrev = pNode;

    LeaveCriticalSection(&gcsWriterHeap);

    //
    // notify writer thread that a node has been added
    // 
    if (!SetEvent(ghWriterEvent))
        ErrorReporter("SetEvent( writer packet )");
    
    return;    
}

/*-----------------------------------------------------------------------------

FUNCTION: AddToFrontOfLinkedList(PWRITEREQUEST)

PURPOSE: Adds a node to the front of the write request linked list

PARAMETERS:
    pNode - pointer to write request packet to add to linked list

HISTORY:   Date:      Author:     Comment:
            1/26/96   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void AddToFrontOfLinkedList(PWRITEREQUEST pNode)
{
    PWRITEREQUEST pNextNode;
    //
    // add node to linked list
    //
    EnterCriticalSection(&gcsWriterHeap);

    pNextNode = gpWriterHead->pNext;
    
    pNextNode->pPrev = pNode;
    gpWriterHead->pNext = pNode;
    
    pNode->pNext = pNextNode;
    pNode->pPrev = gpWriterHead;

    LeaveCriticalSection(&gcsWriterHeap);

    //
    // notify writer thread that a node has been added
    // 
    if (!SetEvent(ghWriterEvent))
        ErrorReporter("SetEvent( writer packet )");
    
    return;    
}

/*-----------------------------------------------------------------------------

FUNCTION: RemoveFromLinkedList(PWRITEREQUEST)

PURPOSE: Deallocates the head node and makes the passed in node 
         the new head node.
         Sets the head node point to node just after the passed in node.
         Returns the node pointed to by the head node.

PARAMETERS:
    pNode - pointer to node to make the new head

RETURN:
    Pointer to next node.  This will be NULL if there are no
    more nodes in the list.

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
PWRITEREQUEST RemoveFromLinkedList(PWRITEREQUEST pNode)
{
    PWRITEREQUEST pNextNode;
    PWRITEREQUEST pPrevNode;
    BOOL bRes;

    EnterCriticalSection(&gcsWriterHeap);
    
    pNextNode = pNode->pNext;
    pPrevNode = pNode->pPrev;    
    
    bRes = HeapFree(ghWriterHeap, 0, pNode);
    
    pPrevNode->pNext = pNextNode;
    pNextNode->pPrev = pPrevNode;

    LeaveCriticalSection(&gcsWriterHeap);

    if (!bRes)
        ErrorReporter("HeapFree(write request)");

    return pNextNode;     // return the freed node's pNext (maybe the tail)
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -