Click here to Skip to main content
15,886,761 members
Articles / Desktop Programming / MFC

CSerialPort v1.03 - Serial Port Wrapper

Rate me:
Please Sign up or sign in to vote.
4.94/5 (58 votes)
3 Mar 2000 1M   21.7K   249  
A freeware MFC class for Win32 serial ports.
/*
Module : APP.CPP
Purpose: Provides the WinMain function for testing the serial port class
Created: PJN / 23-05-1999
History: None

Copyright (c) 1999 by PJ Naughter.  
All rights reserved.

*/

/////////////////////////////////  Includes  //////////////////////////////////

#include "stdafx.h"
#include "SerialPort.h"



///////////////////////////////// Defines /////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



//////////////////////////////// Implementation ///////////////////////////////


class CSerialPortApp : public CWinApp
{
public:
  virtual BOOL InitInstance();
};

CSerialPortApp theApp;

BOOL CSerialPortApp::InitInstance()
{
  BYTE* pBuf = new BYTE[10000];

  try
  {
    COMMCONFIG config;
    CSerialPort::GetDefaultConfig(1, config);

    CSerialPort port;
    port.Open(1, 1200, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);

    HANDLE hPort = port.Detach();
    port.Attach(hPort);

    DWORD dwModemStatus;
    port.GetModemStatus(dwModemStatus);

    DCB dcb;
    port.GetState(dcb);

    dcb.BaudRate = 9600;
    port.SetState(dcb);    

    DWORD dwErrors;                      
    port.ClearError(dwErrors);

    port.SetBreak();
    port.ClearBreak();

    COMSTAT stat;
    port.GetStatus(stat);

    COMMTIMEOUTS timeouts;
    port.GetTimeouts(timeouts);

    port.Setup(10000, 10000);

    port.GetConfig(config);

    config.dcb.BaudRate = 9600;
    port.SetConfig(config);

    port.Set0WriteTimeout();
    port.Set0ReadTimeout();

    char sBuf[] = "This should appear on the serial port";
    port.Write(sBuf, strlen(sBuf));

    DWORD dwMask;
    port.GetMask(dwMask);

    port.SetMask(EV_TXEMPTY); 

    //port.WaitEvent(dwMask);

    port.TerminateOutstandingWrites();

    port.TransmitChar('p');

    port.Set0Timeout();

    char sRxBuf[10];
    DWORD dwRead = port.Read(sRxBuf, 10);

    port.TerminateOutstandingReads();

    port.ClearDTR();

    port.ClearRTS();

    port.SetDTR();

    port.SetRTS();

    port.SetXOFF();

    port.SetXON();

    COMMPROP properties;
    port.GetProperties(properties);

    port.ClearWriteBuffer();

    port.ClearReadBuffer();

    port.Flush();

    port.Close();


    //Try out the overlapped functions
    CSerialPort port2;
    port2.Open(1, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);

    CEvent event(FALSE, TRUE);
    OVERLAPPED overlapped;
    ZeroMemory(&overlapped, sizeof(OVERLAPPED));
    overlapped.hEvent = event;
    if (!port2.Write(pBuf, 10000, overlapped))
    {
      DWORD dwBytesWritten;
      WaitForSingleObject(event, INFINITE);
      port2.GetOverlappedResult(overlapped, dwBytesWritten, TRUE);
    }
    if (!port2.Read(pBuf, 10, overlapped))
    {
      DWORD dwBytesRead;
      if (WaitForSingleObject(event, 1000) == WAIT_OBJECT_0)
      {
        TRACE(_T("Data was read from the serial port\n"));
        port2.GetOverlappedResult(overlapped, dwBytesRead, FALSE);
      }
      else
        TRACE(_T("No data was read from the serial port\n"));
    }

    port2.SetMask(EV_TXEMPTY); 
    port2.WaitEvent(dwMask, overlapped);                  

    /* for testing on NT only
    port2.WriteEx(sBuf, strlen(sBuf));
    SleepEx(INFINITE, TRUE);
    port2.ReadEx(pBuf, 10);
    SleepEx(INFINITE, TRUE);
    */

  }
  catch (CSerialException* pEx)
  {
    TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage());
    pEx->Delete();
  }

  delete [] pBuf;

  return FALSE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions