Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with WriteFile() and the serial port

Posted on 1997-07-21
4
Medium Priority
?
1,403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Accepted Solution

by:
kbg earned 300 total points
ID: 1401557
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
ID: 1401558
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
ID: 1401559
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
ID: 1401560
/*
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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