Code Examples
  Home arrow Code Examples arrow Page 2 - 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 
Mobile Linux 
App Generation ROI 
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 / 84
    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


    Writing a Serial Communication Library for Windows - Opening the Selected Serial Port


    (Page 2 of 6 )

    The CreateFile () function opens a communications port. There are two ways to call CreateFile () to open the port:

    • Overlapped: Overlapped operations enable a thread to execute a time-consuming I/O operation in the background, leaving the thread free to perform other tasks. Now while reading or writing to the opened device in overlapped mode of operation, the calling thread must specify a pointer to an OVERLAPPED structure. This mode is mostly used to monitor the activity on the port or to do I/O intensive operations and this library is written to handle Non-Overlapped operations so I’d suggest to interested readers to peek in the MSDN documentation for more details.

    • Non-Overlapped: One may open a Communication Port for either Overlapped IO operation or Non-Overlapped IO operation. This class/library is written for Non-Overlapped IO Operation. So we’re not going to discuss about the overlapped mode.

    Configuring the Selected Serial Port

    When the CreateFile() function opens a handle to a serial communications resource, the system initializes and configures the resource according to the values set up the last time the resource was opened. Preserving the previous settings enables the user to retain the settings specified through a mode command when the device is reopened. The values inherited from the previous open operation include the configuration settings of the device control block (a DCB structure) and the time-out values used in I/O operations. If the device has never been opened, it is configured with the system defaults.

    One may use the GetCommState and SetCommState APIs to determine the initial configuration of a serial communications resource or to set a new configuration.

    The most critical part in serial communication programming is configuring the port settings with the DCB structure. The DCB structure defines the control setting for a serial communications device. Initializing the DCB structure incorrectly is a common problem among beginners. When a serial communications function does not produce the expected results, the DCB structure must be the first place to check. A call to the CreateFile() function opens a serial port with default port settings. Usually, you’d need to change the defaults as per your requirements. You must set the Baud rate for communication, Parity functions, number of Stop Bits, etc. in accordance with the requirements of the interfaced hardware device by calling appropriate Win32 API Functions.

    Configuring Time-Out value

    An application must always set communication time-outs by using the COMMTIMEOUTS structure each time it opens a communication port. If this structure is not configured, the port uses default time-outs supplied by the driver, or time-outs from a previous communication application as explained above. By assuming specific time-out settings when the settings are actually different, an application can have read/write operations that never complete or complete too often. You must configure the read & write time-outs by calling the appropriate Win32 API Functions.

    Reading/Writing to the Serial Port

    The WriteFile() function transfers data through the serial connection to another device. Before calling this function, an application must open and configure a serial port.

    An application calls the ReadFile() function to receive data from a device at the other end of a serial connection.

    Closing a Serial Port

    You must close the communications port after serial transmission in order to make this port available for other applications which use this resource. As long as you are working with a port (i.e. the port is in an open state), other threads or applications will not be able to access to this port till you close the handle to that port in NON-OVERLAPPED IO Operation. Call the CloseHandle() function to close the serial port. CloseHandle() has one parameter, which is the handle returned by the CreateFile() call that opened the port.

    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 5 hosted by Hostway
    Stay green...Green IT