article

Using MSComm for serial communication

Email
Submitted on: 1/3/2015 5:43:00 AM
By: Paul R. Richards (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: Microsoft Visual C++
Views: 1348
 
     How to use Microsofts Communication Control MSCOMM

 
				






If you would like to use the Microsoft 
<--[if gte mso 9]>
 
 Normal
 Paul Richards
 2
 0
 2002-01-31T02:59:00Z
 2002-01-31T02:59:00Z
 2
 273
 1558
  
 12
 3
 1913
 9.2720
 




If you would like to use the Microsoft

Communications Control, here are the steps in VC:

 

1) Create a dialog in VC++

 

2) Project->Add to Project->Components and Controls

 

3) Find "Microsoft Communications Control" click insert

 

4) Drag the now visible phone icon onto your dialog

 

5) Select phone icon and hit cntrl-w for class wizard

 

6) Click on Member Variables and click on IDC_MSCOMM1

 

7) Add a member variable name (m_Comm) for the control

 

8) You can now open the serial port (1) using this code:

m_Comm.SetCommPort (1); // Com port 1

m_Comm.SetSettings ("38400,e,8,1");

m_Comm.SetInputMode (1); // Binary mode

m_Comm.SetPortOpen (TRUE); // Open it

// Note: you may want to flush the buffer here, by

// reading and throwing away the data

// m_Comm.GetInput();

 

9) To send data use this code:

CByteArray btArray;

btArray.Add (0x00); // Byte 1

btArray.Add (0x13); // Byte 2

COleVariant var(btArray);

m_Comm.SetOutput(var); // Send the data.

 

10) To read data use this code:

COleVariant myVar;

int hr;

long lLen = 0;

BYTE *pAccess;

char buffer[255];

 

myVar.Attach (m_Comm.GetInput());

// Get the length

hr = SafeArrayGetUBound (myVar.parray, 1, &lLen);

if (hr == S_OK)

{

lLen++; // upper bound is zero based index

// lock array so you can access it.

hr = SafeArrayAccess (myVar.parray,(void**)&pAccess);

if (hr == S_OK)

{

// Make a copy of the data

// Note: Need to check that lLen is < buffer length

// so you don't overrun the buffer.

for (int i 0; i < lLen; i++)

buffer[i] = pAccess[i];

 

// unlock the data

SafeArrayUnaccessData (myVar.parray);

}

}

// COleVariant cleans itself up


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.