Problem with WriteFile() and the serial port

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
etcetera99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kbgCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
etcetera99Author Commented:
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
kbgCommented:
Sorry This code is sample block write for principal and interest.
used functions are making by me, and include nothing another.

0
kbgCommented:
/*
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.