Writing a Serial Communication Library for Windows - How To Use 'CSerialComm' Class
(Page 6 of 6 )
Take the following steps to use the CSerialComm class
- Copy the SerialCom.h & SerialCom.cpp files and paste into your project directory.
- In your VC++ IDE, add the files to your project
- Add the line #include "SerialComm.h" in your dialog's header file
- Create an instance of the CSerialComm class in your dialog's header file.
You can now call the member functions of CSerialComm when you want to communicate with external device as shown below.
In Your dialog's .cpp File:
// Open Communication Port. Please check functions return value to ensure whether
// Port opened successfully.
port.OpenPort( );
// Configure Port for Communication. Please check functions return value to
// ensure whether Port is configured successfully.
port.ConfigurePort( );
// Set communication time outs. Please check functions return
// value to ensure whether communication time outs configured
// successfully.
port.SetCommunicationTimeouts( );
// call this function in a loop till all bytes are written. Please check
// functions return value to ensure whether Write operation completed
// successfully.
port.WriteByte();
// call this function in a loop till all bytes are received. Please check
// functions return value to ensure whether Read operation completed
// successfully or a time out occurred.
port.ReadByte( );
// Call this function to close the handle to the port.
// Process the received Data
port.ClosePort();
If you decide to use the extension DLL directly just include the library SCommLib.lib file in the Project | Settings | Link tab as shown in the figure below.

Figure 2: Setting the import Library
Next include the SerialComm.h file in your project (because it contains the class definition for the library).Now you’re free to make any instance of the class and the compiler would compile the code happily for you.
Source Code
You may download the source code here. It also contains a Compiled DLL that exports the CSerialComm class that will ease development of serial communication applications using MFC.
You’re free to use the code in any way. I hope you found the article informative. You may contact me at digvijay.chauhan@rediffmail.com for any comments and suggestions.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |