Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with WriteFile() and the serial port

Posted on 1997-07-21
4
Medium Priority
?
1,413 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 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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