Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,561 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




Writing To a Serial Port

 
Reply to this topicStart new topic

Writing To a Serial Port

MikeRaines
10 Apr, 2008 - 06:52 PM
Post #1

D.I.C Head
**

Joined: 30 Oct, 2007
Posts: 76



Thanked: 1 times
My Contributions
CODE

BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   DWORD dwRes;
   BOOL fRes;

   // Create this write operation's OVERLAPPED structure's hEvent.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // error creating overlapped event handle
      return FALSE;

   // Issue write.
   if (!WriteFile(outputPort, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
      if (GetLastError() != ERROR_IO_PENDING) {
         // WriteFile failed, but isn't delayed. Report error and abort.
         fRes = FALSE;
      }
      else
         // Write is pending.
         dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
         switch(dwRes)
         {
            // OVERLAPPED structure's event has been signaled.
            case WAIT_OBJECT_0:
                 if (!GetOverlappedResult(outputPort, &osWrite, &dwWritten, FALSE))
                       fRes = FALSE;
                 else
                  // Write operation completed successfully.
                  fRes = TRUE;
                 break;
            
            default:
                 // An error has occurred in WaitForSingleObject.
                 // This usually indicates a problem with the
                // OVERLAPPED structure's event handle.
                 fRes = FALSE;
                 break;
         }
   }
   else
   {
       fRes = TRUE;
   }
   CloseHandle(osWrite.hEvent);
   return fRes;
}

This seems to be the common example for writing to a serial port.

My question is, what do the paramaters lpBuf and dwToWrite represent?

This post has been edited by MikeRaines: 10 Apr, 2008 - 06:52 PM
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Writing To A Serial Port
10 Apr, 2008 - 07:13 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 343



Thanked: 10 times
Dream Kudos: 100
My Contributions
lpBuf is the buffer containing the data that will be written out. In this case to the serial port. dwToWrite is the number of bytes to write to the serial port.

http://msdn2.microsoft.com/en-us/library/a...747(VS.85).aspx
User is online!Profile CardPM
+Quote Post

MikeRaines
RE: Writing To A Serial Port
11 Apr, 2008 - 10:37 AM
Post #3

D.I.C Head
**

Joined: 30 Oct, 2007
Posts: 76



Thanked: 1 times
My Contributions
That helps a ton, Thanks.


-Mike
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month