Solved

Problem with WriteFile() and the serial port

Posted on 1997-07-21
4
1,380 Views
Last Modified: 2013-12-03
I am writing a program for Windows 95 which takes joystick input and sends data about the joystick across the serial port.  My data gets there all in one piece, but my program causes the computer to crash practically constanty.  I have isolated the problem down to the WriteFile call in which a 32 to 64 byte string is sent.  I have tried sending the data using the OVERLAPPED stuff and without it.  I can't get it to work.  Please help.

thanks,

mitch
0
Comment
Question by:etcetera99
  • 3
4 Comments
 

Accepted Solution

by:
kbg earned 100 total points
Comment Utility
refer to 06/16/97 Tapi, reading/writing to com ports in Previously Asked Windows Programming Questions: [1-25] of 957.
and

//
//Sends a block of characters
//PARAMETERS:
//    pWrite - pointer to write request packet
//COMMENTS: WRITEREQUEST packet contains the following:
//            lpBuf       : Address of data buffer
//            dwSize      : size of data buffer
void WriterBlock(PWRITEREQUEST pWrite)
{  

    WriterGeneric(pWrite->lpBuf, pWrite->dwSize);
    return;
}




//PURPOSE: Handles sending all types of data
//PARAMETER:
//    lpBuf     - pointer to data buffer
//    dwToWrite - size of buffer
void WriterGeneric(char * lpBuf, DWORD dwToWrite)
{
    OVERLAPPED osWrite = {0};
    HANDLE hArray[2];
    DWORD dwWritten;
    DWORD dwRes;

   
    // If no writing is allowed, then just return
    if (NOWRITING(TTYInfo))
        return ;

    // create this writes overlapped structure hEvent
    osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    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;
}

//////////////////////////////////////////////////////

typedef struct WRITEREQUEST;

struct WRITEREQUEST *gpWriterHead;
struct WRITEREQUEST *gpWriterTail;

typedef struct WRITEREQUEST
{
  DWORD      dwWriteType;        // char, file start, file abort, file packet
  DWORD      dwSize;             // size of buffer
  char       ch;                 // ch to send
  char *     lpBuf;              // address of buffer to send
  HANDLE     hHeap;              // heap containing buffer
  HWND       hWndProgress;       // status bar window handle
  struct WRITEREQUEST *pNext;    // next node in the list
  struct WRITEREQUEST *pPrev;    // prev node in the list
} WRITEREQUEST, *PWRITEREQUEST;

 --------------- I wish youer success ! ------------
0
 

Author Comment

by:etcetera99
Comment Utility
nope, didn't help.  i'm using visual c++.  this code didn't even compile right.  is there other code to go with it or an include file somewhere or something?
0
 

Expert Comment

by:kbg
Comment Utility
Sorry This code is sample block write for principal and interest.
used functions are making by me, and include nothing another.

0
 

Expert Comment

by:kbg
Comment Utility
/*
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
*/
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;


//
#define WRITE_BLOCK         0x06
    LeaveCriticalSection(&gcsWriterHeap);typedef struct STATUS_MESSAGE;

struct STATUS_MESSAGE * glpStatusMessageHead;
struct STATUS_MESSAGE * glpStatusMessageTail;

typedef struct STATUS_MESSAGE
{
    struct STATUS_MESSAGE * lpNext;     // pointer to next node
    char chMessageStart;                // variable length string start here
} STATUS_MESSAGE;


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

you must insert member function/variable in youer classs.
 removed error handler functions and etc functions, or make function
do allocation value.

 This code used linked list for memoy block Write
and write com port by byte. "WriterGeneric" is memory pointer  increased by byte and write in com port.

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now