Code Examples
  Home arrow Code Examples arrow Page 3 - Writing a Serial Communication Library for...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Writing a Serial Communication Library for Windows
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 81
    2004-09-28

    Table of Contents:
  • Writing a Serial Communication Library for Windows
  • Opening the Selected Serial Port
  • CSerialComm Class
  • DWORD BaudRate, Parity
  • Write and Read Timeouts
  • How To Use 'CSerialComm' Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Writing a Serial Communication Library for Windows - CSerialComm Class


    (Page 3 of 6 )

    The CSerialComm class uses eight member functions to achieve the above mentioned functionality. Six are explained above and the rest two GetBytesRead and GetBytesWritten allow you to keep track of the amount of Transacted bytes. The class Diagram of the class is as below:

    Serial Data Communications Library

    Figure 1: The CSerialComm Class Diagram


    The code for the methods is below and is self describing if you’ve understood the above theory.

    BOOL CSerialComm::OpenPort(CString strPortName)
    {
     strPortName= "//./" +strPortName;

     m_hComm = CreateFile (strPortName,
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
    OPEN_EXISTING,
        0,
        0);

     if (m_hComm==INVALID_HANDLE_VALUE)
     {
    ::MessageBox(NULL,_T("Cannot open Communication Port"),_T("Com Port Error"),MB_OK | MB_ICONERROR);
      
      return FALSE;
     }
     else
     {
      return TRUE;
     }

    }

    The OpenPort() member function opens a communication port for data transmission. The parameter to be passed to this function is a string containing the port name. For example "com1" for COM1, "com2" for COM2 etc. If the function succeeds the return value is true, otherwise it is false.

    BOOL CSerialComm::ConfigurePort(DWORD BaudRate,
    BYTE ByteSize,
    DWORD dwParity,
    BYTE Parity,
    BYTE StopBits)
    {
    if((m_bPortReady = GetCommState(m_hComm, &m_dcb))==0)
    {
     ::MessageBox(NULL,_T("GetCommState Error"),_T("Error"),MB_OK | MB_ICONERROR);  CloseHandle(m_hComm);
     return FALSE;
    }
     m_dcb.BaudRate  = BaudRate;
     m_dcb.ByteSize  = ByteSize;
     m_dcb.Parity  = Parity ;
     m_dcb.StopBits  = StopBits;
     m_dcb.fBinary  = TRUE;
     m_dcb.fDsrSensitivity = FALSE;
     m_dcb.fParity  = dwParity;
     m_dcb.fOutX  = FALSE;
     m_dcb.fInX   = FALSE;
     m_dcb.fNull   = FALSE;
     m_dcb.fAbortOnError = TRUE;
     m_dcb.fOutxCtsFlow = FALSE;
     m_dcb.fOutxDsrFlow = FALSE;
     m_dcb.fDtrControl = DTR_CONTROL_DISABLE;
     m_dcb.fDsrSensitivity= FALSE;
     m_dcb.fRtsControl = RTS_CONTROL_DISABLE;
     m_dcb.fOutxCtsFlow = FALSE;
     m_dcb.fOutxCtsFlow = FALSE;
     m_bPortReady = SetCommState(m_hComm, &m_dcb);
     if(m_bPortReady ==0)
     {
      ::MessageBox(NULL,_T("SetCommState Error"),_T("Error"),MB_OK | MB_ICONERROR);    CloseHandle(m_hComm);
      return FALSE;
     }
     return TRUE;
    }

    The ConfigurePort () member function configures a communication port for data transmission. The parameters to be passed to this function are given next.

    More Code Examples Articles
    More By Digvijay Chauhan


       · This article plus its associated download/source code are one of the few a pages on...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway