GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Feb-2005, 05:01
ib_alam ib_alam is offline
Junior Member
 
Join Date: Dec 2004
Posts: 32
ib_alam is on a distinguished road

reading from comm port and displaing in editbox using MFc


Hi all,
I want to read from the comm port. Data can be integer, characters or hexa and which are sent randomly.
I want to read this data and display in the editbox.

I am able to read the file but not able to display all the data. I can able to display any one of these data. i.e I can display integers than not able to display others.

I am using.

CPP / C++ / C Code:
CSting temp;
for(i=0; i<=100; i++)
{
temp.Format("%d",buffer[i])
}
m_read.SetSel(10,0);  //name of the editbox
m_read.ReplaceSel(temp);


Also when the application is reading from the comm port and displaying, at that period I am not able to click any other buttons.

Can any one help me out.
Last edited by LuciWiz : 01-Feb-2005 at 06:16. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 01-Feb-2005, 06:19
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by ib_alam
I want to read from the comm port. Data can be integer, characters or hexa and which are sent randomly.
I want to read this data and display in the editbox.

I am able to read the file but not able to display all the data. I can able to display any one of these data. i.e I can display integers than not able to display others.

How do you store your data? Do you use some kind of general container or is there a way by which you can sort your data?

Quote:
Originally Posted by ib_alam
Also when the application is reading from the comm port and displaying, at that period I am not able to click any other buttons.

That is because you need to use a different thread for reading from the com port. That way your application won't hang until the reading is over.

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 01-Feb-2005, 07:10
ib_alam ib_alam is offline
Junior Member
 
Join Date: Dec 2004
Posts: 32
ib_alam is on a distinguished road
Quote:
Originally Posted by LuciWiz
How do you store your data? Do you use some kind of general container or is there a way by which you can sort your data?



That is because you need to use a different thread for reading from the com port. That way your application won't hang until the reading is over.

Regards,
Lucian

Dear Luci
I am reading one byte each time and putting in buffer. after that displaying in editbox. by using the code which is in my earlier thread.

can u tell me how to use different thread for reading from the com port.
  #4  
Old 01-Feb-2005, 09:35
mingcosp mingcosp is offline
New Member
 
Join Date: Jan 2005
Posts: 14
mingcosp will become famous soon enough
Quote:
Originally Posted by ib_alam
Dear Luci
I am reading one byte each time and putting in buffer. after that displaying in editbox. by using the code which is in my earlier thread.

can u tell me how to use different thread for reading from the com port.


Use a different thread to wait for a serial event and read. Your main thread can process other tasks.

You can have something like:
CPP / C++ / C Code:
m_hThread = CreateThread(0,0,ThreadProc,LPVOID(this),0,&dwThreadId);
// None Overlapping Mode
DWORD ThreadProc (void)
{
    if (/* wait until a serial event happens*/)
    {
	// Determine the event

	if (/*event of data arriving*/)
            {
                  do{
		// Your own ReadFunction
	

		} while (/*byes of data read == lenth of your buffer*/);
	}
    }
}

Make sure if None Overlapping mode is enough for your program otherwise you have to use Overlapping mode. refer to the link below for more information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
  #5  
Old 01-Feb-2005, 09:48
mingcosp mingcosp is offline
New Member
 
Join Date: Jan 2005
Posts: 14
mingcosp will become famous soon enough
as for your first question, are you trying to display whatever you got?Then Rich Edit control might be good for this

Then try the following code:
CPP / C++ / C Code:
	// Format the selection as default text
	CHARFORMAT cf;

	cf.dwMask = CFM_COLOR;

	cf.cbSize = sizeof(cf);

	m_richEdit.GetSelectionCharFormat(cf); 
	m_richEdit.SetSelectionCharFormat(cf);


	m_richEdit.SetSel(-1,-1);
	m_richEdit.ReplaceSel(pszData) // Data that you read from the com port

  #6  
Old 02-Feb-2005, 02:12
ib_alam ib_alam is offline
Junior Member
 
Join Date: Dec 2004
Posts: 32
ib_alam is on a distinguished road
Quote:
Originally Posted by mingcosp
Use a different thread to wait for a serial event and read. Your main thread can process other tasks.

You can have something like:
CPP / C++ / C Code:
m_hThread = CreateThread(0,0,ThreadProc,LPVOID(this),0,&dwThreadId);
// None Overlapping Mode
DWORD ThreadProc (void)
{
    if (/* wait until a serial event happens*/)
    {
	// Determine the event

	if (/*event of data arriving*/)
            {
                  do{
		// Your own ReadFunction
	

		} while (/*byes of data read == lenth of your buffer*/);
	}
    }
}

Make sure if None Overlapping mode is enough for your program otherwise you have to use Overlapping mode. refer to the link below for more information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp

Hi,
is there any other way without using multithreading. If not, can u explain more how to use it.
  #7  
Old 02-Feb-2005, 02:52
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
CPP / C++ / C Code:
CYourClass
{
...
...
public:
   void ReadCommPortWhatever();

static UINT ThreadFunc(LPVOID pvParam);
}

BOOL CYourClass::OnInitDialog() 
{
...
...
AfxBeginThread(InitThreadFnc,this,THREAD_PRIORITY_LOWEST);
}

UINT CYourClass::InitThreadFnc(LPVOID param)
{
	CYourClass* pAppView= (CYourClass*)param;
	
	pAppView->ReadCommPortWhatever();
	return 0;
}

sorry no time to explain this, its queite straight forward, but i'll explain later or let othr help m explain this..

gtg.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #8  
Old 02-Feb-2005, 03:11
mingcosp mingcosp is offline
New Member
 
Join Date: Jan 2005
Posts: 14
mingcosp will become famous soon enough
Quote:
Originally Posted by ib_alam
Hi,
is there any other way without using multithreading. If not, can u explain more how to use it.

Depend what project are you on. Basically writting something to a port is easier than reading. If you know when there is data coming in through a COM port and how many bytes they are, one thread is probably ok for you as you could only call your read function at that particular time. But if these are uncertain, then you probably need to call WaitComEvent function which will block your program until there is a Com Event happening (e.g. data arrives or flow control happens) That's why your program appears to be "hung up". In that case you need another thread only for waiting and reading while your main thread is performing other tasks.

Another case, if you still want to do some data transfer while your port is on waiting mode, your communication is required to be working in overlapping mode. Or,lets say if your write process is suspending by flow control (CTS line ) you won't be able to access to that port until the write function returns.this also requires overlapping communication.

I suggest you looking at the link I gave and see which scenario applies to your project and decide which way to take.

let me know your decision and then we can have a further discussion
  #9  
Old 02-Feb-2005, 04:47
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Max Payne
CPP / C++ / C Code:
CYourClass
{
public:
   void ReadCommPortWhatever();

static UINT ThreadFunc(LPVOID pvParam);
}


UINT CYourClass::InitThreadFnc(LPVOID param)
{
//...

I think Max meant:
CPP / C++ / C Code:
static UINT InitThreadFnc(LPVOID pvParam);
//instead of 
static UINT ThreadFunc(LPVOID pvParam);

Either that, or I didn't completely follow his logic. I agree his example is very good, and if you can't follow it, maybe you should post some of your code (with the reading), and we'll try to integrate that in the whole multithreading thing. And no, I don't see any other way to solve your problem - threads are the solution.

As for the displaying of your data, mingcosp's suggestion is very good - I'm just not sure about what your question was. Do you want just to be able to display it "as is", or would you like an "intelligent" display, with data interpretation. 'Cause I don't think you can achieve this without knowing what you are getting from the port.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #10  
Old 02-Feb-2005, 18:57
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
thanks Lucian, for correcting my mistake...

CPP / C++ / C Code:
CYourClass
{
...
...
public:
   // this handles all work done in the thread
   void ReadCommPortWhatever();

// this is your worker thread
static UINT ThreadFunc(LPVOID pvParam);
}

BOOL CYourClass::OnInitDialog() 
{
...
...
// create your worker thread here or where its needed
// 1st param is thread function name
// 2nd is pointer to parent
// 3rd param is thread priority
// check msdn for other params, usually ignored
AfxBeginThread(ThreadFunc,this,THREAD_PRIORITY_LOWEST);
}

// your worker thread
UINT CYourClass::ThreadFunc(LPVOID param)
{
             // param is the 'this' pointer to parent u just sent
	CYourClass* pAppView= (CYourClass*)param;
	
             // don't put too much stress on a worker thread function, 
             // so call another member function to handle all the work
	pAppView->ReadCommPortWhatever();
	return 0;
}

// read from com and display
void CYourClass::ReadCommPortWhatever()
{
     // do read

     // do display
}


Quote:
Depend what project are you on. Basically writting something to a port is easier than reading. If you know when there is data coming in through a COM port and how many bytes they are, one thread is probably ok for you as you could only call your read function at that particular time. But if these are uncertain, then you probably need to call WaitComEvent function which will block your program until there is a Com Event happening (e.g. data arrives or flow control happens) That's why your program appears to be "hung up". In that case you need another thread only for waiting and reading while your main thread is performing other tasks.

this a very good explanation, just wanna add that u cannot avoid using multi thread here cause reading from com port sure will take some time and and if you don't have a thread to read from the port, your program would certainly hang untill it finishes reading form the port.

I'm not sure about the serial thingy mingcosp posted, i'll rather make an infinite thread to read and wait for com event. but mingcosp suggestion might be better way to do it, and look into it..

CPP / C++ / C Code:
// your worker thread
UINT CYourClass::ThreadFunc(LPVOID param)
{
             while(true)
             {
                      // wait for com event

                      // read com
                      // display
             }

	return 0;
}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.