If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Urgent Help needed. ComPort monitoring works only once.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-13-04, 09:42
Indian Indian is offline
Registered User
 
Join Date: Sep 2003
Posts: 30
Unhappy Urgent Help needed. ComPort monitoring works only once.

ERNSTES PROBLEM mit Serial Port und 2 Dialogen

--------------------------------------------------------------------------------

Hi,


got a serious problem, need urgend help.I use a class which monitor the comports, that not difficult, very easy. See description

DESCRIPTION
Code:
Background:
I've searched for a good communication class for a while and could not find one.
That's when I decided to write my own and it should be one that's easy to use.

In the newgroups there are many questions about serial communication so I thought:
make it public! It's freeware. The only thing I expect from users is that they drop me a mail.
All modifications on this class are free, but please let me know if it solvers a bug
or adds some good features. Also comment your code and don't let me solve your bugs!

Target:

The class is not intended to use as a baseclass for modemcommunication but
more for driving hardware or reading hardware via the serial port. 

From the classes included there is only one class important: CSerialPort.
The other classes are only there to illustrate the use of this class.

Usage:

In your software you only need to create an instance of the CSerialPort class
and call InitPort.

BOOL CSerialPort::InitPort(CWnd* pPortOwner,		// the owner (CWnd) of the port (receives message)
						   UINT  portnr,			// portnumber (1..4)
						   UINT  baud,				// baudrate
						   char  parity,			// parity 
						   UINT  databits,			// databits 
						   UINT  stopbits,			// stopbits 
						   DWORD dwCommEvents,		// EV_RXCHAR, EV_CTS etc
						   UINT  writebuffersize)	// size of the writebuffer

The dwCommEvents flag can be used for communication with the owner of this class.

The flags can be one of the following (or combined with |):
WM_COMM_BREAK_DETECTED	A break was detected on input.
WM_COMM_CTS_DETECTED		The CTS (clear-to-send) signal changed state. 
WM_COMM_DSR_DETECTED		The DSR (data-set-ready) signal changed state. 
WM_COMM_ERR_DETECTED		A line-status error occurred. Line-status errors are 					CE_FRAME, CE_OVERRUN, and CE_RXPARITY. 
WM_COMM_RING_DETECTED		A ring indicator was detected. 
WM_COMM_RLSD_DETECTED		The RLSD (receive-line-signal-detect) signal changed state. 
WM_COMM_RXCHAR			A character was received and placed in the input buffer. 
WM_COMM_RXFLAG_DETECTED	The event character was received and placed in the input 
					buffer.  

Accept the first parameter all parameters are optional. The standard values are:

portnr		= 1
baud		= 19200
parity		= 'N'
databits		= 8, 
stopsbits	= 1, 
dwCommEvents	= EV_RXCHAR | EV_CTS,
nBufferSize	= 512);

So the follwing code is enough to make communication possible:

in the header of the owner:
	CSerialPort	m_Serial;

in the code:
	m_Serial.InitPort(this);
	m_Serial.StartMonitoring();

Then the tread that watches the port is started and all events on the port are send to
the owner. The receive a character the owner needs a messageentry in the messagemap:

BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
	//{{AFX_MSG_MAP(CCommtestDlg)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

and they must be handled:

LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
	// do something with the received character

	return 0;
}

This is it for reading. Writing can be done with WriteChar or WriteToPort
Problem:
The monitoring works perfect in my main dialog, i scan with a scanner a product and true the message handler i can do whatever i want. IF i open a child dialog (Configuration Dialog), a dialog from my main dialog, it wont work in the configuration dialog. I did exactly the same coding what i did in the main class, coded exactly the same. I stop the monitoring in the main class and start the monitoring the child class. BUT if i never start the monitoring in the main class it works only once in the Confguration Dialog, wont work if i again start the dialog Config Dialog. WHY ? i have no clue


Code:
IMPLEMENT_DYNAMIC(CAddItemDlg, CDialog)
CAddItemDlg::CAddItemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAddItemDlg::IDD, pParent)
{
}

CAddItemDlg::~CAddItemDlg()
{

}

void CAddItemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAddItemDlg, CDialog)
	ON_MESSAGE(WM_COMM_RXCHAR, AddItemOnCommunication) //handler 
END_MESSAGE_MAP()

BOOL CAddItemDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// init the ports
	if (m_Ports[0].InitPort(this, 1, 9600, 'N',8,1,EV_RXCHAR,512))
	m_Ports[0].StartMonitoring(); //Start monitoring the comports, sameway i did in main class, but i stop it before i call this dialog

	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


//CAddItemDlg message handlers

//event if BarCode scans, but the function never reacts or it reacts only once when the monitoring in the main dialog is never called.
LONG CAddItemDlg::AddItemOnCommunication(WPARAM ch, LPARAM port)
{
	if (ch == 13)
	{    
		SetDlgItemText(IDC_BARCODE,m_strReceived[port-1]);
		(m_strReceived[port-1]).Empty();  //clear string
		
//	if (exist==true) //show message box if articel is not found in database
//		MessageBox("Verkaufsartikel schon in Datenbank.", "ALREADY EXISTS",MB_ICONWARNING | MB_OK);
	}
	
	if (!((ch == 13)||(ch == 10))) //keep m_strReceived clean from junk sign 10
	{
		m_strReceived[port-1] += (char)ch;
	}
	return 0;
}
[color="red"]DO I HAVE TO KILL ALL THREAD IN THE PARENT DIALOG BEFORE I START A THREAD TO MONITOR THE COMPORTS IN THE CHILD DIALOG ??
Please someone help me

Last edited by Indian; 05-13-04 at 09:52.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On