YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
VX++ Cross-Platform C/C++
Overview
Download
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

Serial Port Communication, Read data from port Visual C++ Samples.


First time here?

Product Tour
E-XD++ Workflow Component product walkthrough

Screenshots
Applications built on E-XD++ Workflow Component

Product feature comparison

 


Introduction

I have searched a lot of samples supporting synchronous and asynchronous serial communication, but the drawback is that I need to implement a thread or a timer to always keep reading the serial data, so I encapsulated this serial communication class with a thread to make it easy to handle the event (READ/WRITE/OPEN/CLOSE) of accessing serial port.

The SerialCtrlDemo project illustrates how to use CSerialIO class and is easy to present the serial communication event information as depicted in the following UI:

serial.JPG

CSerialIO Class

The CSerialIO class is defined for external API and the primary methods listed below:

class CSerialIO{ 
public: 
CSerialIO(); 
virtual ~CSerialIO(); 
void OpenPort(CString strPortName,CString strBaudRate);//open serial port with 
					//parameter port name and baud rate 
virtual void OnEventOpen(BOOL bSuccess);	//handle the event whether the port is 
					//successfully opened 
void ClosePort();//close serial port 
virtual void OnEventClose(BOOL bSuccess);	//handle the event whether the port 
					//is successfully closed 
virtual void OnEventRead(char *inPacket,int inLength);	//handle the received data 
						//from serial 
void Write(char *outPacket,int outLength);	// write data directly 
virtual void OnEventWrite(int nWritten); 	//handle the event of how many bytes 
					//have been written 
… 
}; 

How to Use CSerialIO Class

Take the following steps to use the CSerialIO class:

  1. Add the SerialCtrl.h & SerialCtrl.cpp files in your VC++ project.
  2. Add the line #include "SerialCtrl.h" in your dialog's header file.
  3. Inherit the CSerialIO class in your dialog.
  4. Handle the event (READ/WRITE/OPEN/CLOSE) in your dialog class via overwrite the virtual function.
//Handle the event of opening serial port
void CSerialCtrlDemoDlg::OnEventOpen(BOOL bSuccess)
{
 CString str;
 if (bSuccess)
 {
  str=m_strPortName+" open successfully";
  bPortOpened=TRUE;
  m_btnOpen.SetWindowText("Close");  
 }
 else
 {
  str=m_strPortName+" open failed";
 }
 m_staticInfo.SetWindowText(str);
}

//handle the event of closing serial port
void CSerialCtrlDemoDlg::OnEventClose(BOOL bSuccess)
{
 CString str;
 if (bSuccess)
 {
  str=m_strPortName+" close successfully";
  bPortOpened=FALSE;
  m_btnOpen.SetWindowText("Open");
 }
 else
 {
  str=m_strPortName+" close failed";
 }
 m_staticInfo.SetWindowText(str);
}

//Handle the event of reading data from serial port
void CSerialCtrlDemoDlg::OnEventRead(char *inPacket,int inLength)
{ 
 m_listboxRead.AddString(inPacket);
 CString str;
 str.Format("%d bytes read",inLength);
 m_staticInfo.SetWindowText(str);
}

//Handle the event of writing data 
void CSerialCtrlDemoDlg::OnEventWrite(int nWritten)
{
 if (nWritten>0)
 {
  CString str;
  str.Format("%d bytes written",nWritten);
  m_staticInfo.SetWindowText(str);
 }
 else
 {
  m_staticInfo.SetWindowText("Write failed");
 }
}

Note

This code has been verified with RS-232 connector, and you can easily customize the event handler in run function of SerialThread class.

Contact UCanCode Software

To buy the source code or learn more about with:

 

Ask any questions by MSN: ucancode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2023 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net