• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1428
  • Last Modified:

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
0
etcetera99
Asked:
etcetera99
  • 3
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now