Use C++ to control serial port
1 post • Page 1 of 1
Use C++ to control serial port
Creating the project
Make a simple Dialog workspace in MSVC via the MFC App wizard. The options don't really matter, but make sure you check the 'ActiveX Controls' option in when the Wizard prompts you in step 2.
Adding CMSComm to your project
Open your resource editor so you can change the main dialog. Look on the toolbar, and verify that you don't already have the MSComm control there (someone else may have modified the default workspace). It looks like a little phone.
Add the control to your project. Click the Project >> Add To Project >> Components and Controls command from the menu. This command will take a while as MSVC scans the registry for registered controls. Select 'Registered ActiveX Controls' and click insert. Find the object 'Microsoft Communications Control, version 6.0.' (or another version), select it, then click insert, then Ok. At this point MSVC will prompt you for which parts of the ActiveX control you want to add. There should only be one listed, CMSComm, keep it checked. MSVC will generate a wrapper class for the object so you can use it. You can change the file names and class name if you want, but for this purpose, I'll assume you left them as is.
Close the add components dialog.
Now, look at your dialog toolbar, in it, you should see a new icon for the CMSComm object. Select it, and draw one of these onto your main form. An icon will appear, but don't worry, the control itself is invisible at run time.
Initializing the CMSComm control
All this can be done at run time, but for now, let's just set the properties at design time. Right click on the icon for the comm control in your dialog resource and pull up the properties dialog. Under the Control, Buffers, and Hardware tabs, set the properties you desire, such as buad rate, parity, port number, etc.
Add a member for the control
To use the control, you need to create a class member. Open the Class Wizard, and from the Member Variables tab, select the comm control (by default it will be called IDC_MSCOMM1) then click the 'Add Variable...' button. Create a name, I used m_comm. Double check that the variable is a control variable, and that it's of the right class (CMSComm). You can now use this member variable to communitcate with your com port.
Using the CMSComm control
Here's a quick example of how to use the control. It's important to wait until your OnInitDialog() call (or anytime after) before trying to use it because not everything will be created/initialized before this.
code:--------------------------------------------------------------------------------
CMSComm m_comm; // MFC generated member in the .h file
#include "atlbase.h" // CComVariant support
#define COM_MS_TIMEOUT 5000 // COM1 timeout in milliseconds
// Generate an output.
CString sOut;
// Build your output...
sOut = _T("Hello world");
// Generate a variant for the output.
CComVariant vOut(sOut);
// Send the command.
m_comm.SetOutput(vOut);
// Wait for a reply.
// This is NOT a processor efficient way of doing it,
// but it gets the idea across.
DWORD dwNow, dwTimeOut, dwStart;
dwStart = GetTickCount();
dwTimeOut = COM_MS_TIMEOUT;
CString sIn, sInBuffer;
CComVariant vIn;
for (;;)
{
dwNow = GetTickCount();
if (((dwNow - dwStart) > dwTimeOut) // Timeout elapsed
|| (dwNow < dwStart)) // Happens if tick count wraps past 2^32
break;
vIn.Clear();
vIn = m_comm.GetInput();
sIn = vIn.bstrVal;
sInBuffer += sIn;
// Check a for terminating \r character.
// If we find one, break the loop, otherwise,
// more data is coming.
// The hardware you'll be interfacing with may have a
// a different terminator sequence.
if (sIn.Right(1) == _T("\r"))
break;
}
--------------------------------------------------------------------------------
This is the easy way of doing it. If you're not using a dialog at all, it will be trickier creating the object, since it won't be done automatically by your dialog's ActveX handlers.
Back to Top
Make a simple Dialog workspace in MSVC via the MFC App wizard. The options don't really matter, but make sure you check the 'ActiveX Controls' option in when the Wizard prompts you in step 2.
Adding CMSComm to your project
Open your resource editor so you can change the main dialog. Look on the toolbar, and verify that you don't already have the MSComm control there (someone else may have modified the default workspace). It looks like a little phone.
Add the control to your project. Click the Project >> Add To Project >> Components and Controls command from the menu. This command will take a while as MSVC scans the registry for registered controls. Select 'Registered ActiveX Controls' and click insert. Find the object 'Microsoft Communications Control, version 6.0.' (or another version), select it, then click insert, then Ok. At this point MSVC will prompt you for which parts of the ActiveX control you want to add. There should only be one listed, CMSComm, keep it checked. MSVC will generate a wrapper class for the object so you can use it. You can change the file names and class name if you want, but for this purpose, I'll assume you left them as is.
Close the add components dialog.
Now, look at your dialog toolbar, in it, you should see a new icon for the CMSComm object. Select it, and draw one of these onto your main form. An icon will appear, but don't worry, the control itself is invisible at run time.
Initializing the CMSComm control
All this can be done at run time, but for now, let's just set the properties at design time. Right click on the icon for the comm control in your dialog resource and pull up the properties dialog. Under the Control, Buffers, and Hardware tabs, set the properties you desire, such as buad rate, parity, port number, etc.
Add a member for the control
To use the control, you need to create a class member. Open the Class Wizard, and from the Member Variables tab, select the comm control (by default it will be called IDC_MSCOMM1) then click the 'Add Variable...' button. Create a name, I used m_comm. Double check that the variable is a control variable, and that it's of the right class (CMSComm). You can now use this member variable to communitcate with your com port.
Using the CMSComm control
Here's a quick example of how to use the control. It's important to wait until your OnInitDialog() call (or anytime after) before trying to use it because not everything will be created/initialized before this.
code:--------------------------------------------------------------------------------
CMSComm m_comm; // MFC generated member in the .h file
#include "atlbase.h" // CComVariant support
#define COM_MS_TIMEOUT 5000 // COM1 timeout in milliseconds
// Generate an output.
CString sOut;
// Build your output...
sOut = _T("Hello world");
// Generate a variant for the output.
CComVariant vOut(sOut);
// Send the command.
m_comm.SetOutput(vOut);
// Wait for a reply.
// This is NOT a processor efficient way of doing it,
// but it gets the idea across.
DWORD dwNow, dwTimeOut, dwStart;
dwStart = GetTickCount();
dwTimeOut = COM_MS_TIMEOUT;
CString sIn, sInBuffer;
CComVariant vIn;
for (;;)
{
dwNow = GetTickCount();
if (((dwNow - dwStart) > dwTimeOut) // Timeout elapsed
|| (dwNow < dwStart)) // Happens if tick count wraps past 2^32
break;
vIn.Clear();
vIn = m_comm.GetInput();
sIn = vIn.bstrVal;
sInBuffer += sIn;
// Check a for terminating \r character.
// If we find one, break the loop, otherwise,
// more data is coming.
// The hardware you'll be interfacing with may have a
// a different terminator sequence.
if (sIn.Right(1) == _T("\r"))
break;
}
--------------------------------------------------------------------------------
This is the easy way of doing it. If you're not using a dialog at all, it will be trickier creating the object, since it won't be done automatically by your dialog's ActveX handlers.
Back to Top
- glitch (legacy member)
1 post • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest