Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling OnComm Event in MDI App

Status
Not open for further replies.

kedrew

Programmer
Jun 22, 2001
29
US
I am working on a MDI application that has a 'widget' object that does not inherit from any of the objects in the application. The 'widget' uses the MS Communications Control (mscomm32.ocx - converted to H/CPP files when component is inserted).

I do not have any problems sending data from the MDI app to a remote computer; I cannot get the OnCommEvent to work.

Because I am new to the Com Control, I have been working from MS's VCTERM example - which uses a dialog box.

The widget object doesn't inherit from any application object, so I have tried inheriting from CObject, CCmdTarget, CWnd, and CDialog, as well as my app's Application and Frame classes along with the main document's Doc, Frame, and View classes. (Esentially the shotgun method, hoping something would work).

CObject doesn't compile: error C2039 'eventsinkmap' is not a member of CObject
CCmdTarget, CWnd, and CDialog all compile without errors or warnings, but do not respond to events on the serial port.

[I'm sending data via HyperTerminal on a second computer and have a Protocol Analyzer in the middle - I know the data is moving]

ID_COMMCTRL has been defined in 'resource.h'

My code is as follows:
widget.h
Code:
class Widget: public [CObject/CWnd/...]
{
public:
    CLaserObj();
    OnCommEvent();			
    DECLARE_EVENTSINK_MAP()
....

widget.cpp
// add eventsink map to hook OnComm event
Code:
BEGIN_EVENTSINK_MAP(CLaserObj, CDialog)
	ON_EVENT(CLaserObj, ID_COMMCTRL, 1
/* OnComm */
Code:
, OnCommEvent, VTS_NONE)
END_EVENTSINK_MAP()

Widget::Widget()
{
// Create the MSCOMM32 OLE Control.
Code:
  if (!m_comm_ctrl.Create(NULL,0,CRect (0,0,0,0),myApp.m_pMainWnd,ID_COMMCTRL))
     {
       TRACE0("Failed to create OLE Communications Control\n");
     }
...
}

void Widget::InitializeCommPort()
{
  m_comm_ctrl.SetCommPort(1);	//COM1
  m_comm_ctrl.SetHandshaking(1);//XonXoff
  m_comm_ctrl.SetInputMode(0);	//treat as text (1 - binary)
  m_comm_ctrl.SetSettings("9600,N,8,1");//9600 baud, NO parity, 8 data bits, 1 stop bit
}

void Widget::OnCommEvent()
{
	AfxMessageBox("OnCommEvent()");
}

Thanks in advance
 
I was missing the 'afxdisp.h' include, but it didn't solve the issue.
My app's header (Stdafx.h) now matches the one that the Visual Studio App Wizard generated.

Any other suggestions?
 
CWnd doesn't create any errors at compile time, nor does it during execution (debug mode).

It doesn't call my 'OnCommEvent()' function though.

It appears that I'm having problems getting event messages to the object.

[I even tried using a CSerialPort class that I found on codeguru that accesses the serial port via a file handle. It too has the same problem - can send but not receive].
 
The problem has been solved, and the answer is quite simple.

The widget object was not associated with anything, so to make the compiler happy, I blindly associated it with CWnd, not knowing everything the remainder of the necessary items.

The MSComm component was told to send the messages to the application, the application wasn't setup to receive the Comm Events, and the widget object never had a window created.

So, in short, I moved the event sink and corresponding OnCommEvent() function to the main application and everything works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top