Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » [SUCCESS] Struggle with Serial port class
[SUCCESS] Struggle with Serial port class [message #27809] Thu, 05 August 2010 07:55 Go to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

I have downloaded a serial port class from message 25665 by nixnixnix from this thread
http://www.ultimatepp.org/forum/index.php?t=msg&goto=134 34&&srch=serial+class#msg_13434

Being totally new to C++ and UPP coding, I'd like some help to make the attached project work. The least I need to do is to poll the serial port during the lifetime of the program so that I can display those characters on the docedit box. I may possibly need to do a timedcallback to check for characters or better still event driven receiver.

I'd appreciate any pointers or guidance you can provide.

Thank You
Jerson
  • Attachment: CommPak.zip
    (Size: 6.05KB, Downloaded 225 times)

[Updated on: Sat, 11 September 2010 16:02]

Report message to a moderator

Re: Struggle with Serial port class [message #27811 is a reply to message #27809] Thu, 05 August 2010 09:00 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 936
Registered: July 2009
Location: Germany
Experienced Contributor
this topic is a field on its own Smile nontrivial. it is hard to set up for single thread to be callback like. you either use another worker thread who takes care of reading and writing who can PostCallback or invoke the Callback directly (wouldnt be a good if performance critical...needs to handover to MainThread, and wait for it anyway).

i have coded a overlapped driven communication class that can do that, but it needs some refactoring, maybe it will help someone later..

Re: Struggle with Serial port class [message #27813 is a reply to message #27811] Thu, 05 August 2010 10:00 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
I would guess you need a worker Thread that collects data from a serial port and writes it to a buffer (using a Mutex to be thread safe of course). The main thread can then update the display from the buffer using a timer.

I've never used the class myself so that's the best advice I can give.

For threading see the GuiMT threading example/reference and remember to set your project to compile with MT flags.
Re: Struggle with Serial port class [message #27815 is a reply to message #27811] Thu, 05 August 2010 10:17 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 936
Registered: July 2009
Location: Germany
Experienced Contributor
EDIT: sorry, wrong thread posted...

[Updated on: Thu, 05 August 2010 10:18]

Report message to a moderator

Re: Struggle with Serial port class [message #27817 is a reply to message #27809] Thu, 05 August 2010 11:22 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

I have made some changes based on my ideas and I can see that I am able to gather characters in the Rx Buffer. Can anyone help me to append the data that I get from the class to the DocEdit box?

The relevant file is the main.cpp The section I need help with is in CommPak::CheckRxTmr()

I am not sure if the timer gets destroyed when I close the topwindow. I tried to do a WhenClose, but I think I need to do more than what I am doing right now since it crashes if I use that callback. Without the callback it seems to work fine.
  • Attachment: CommPak.zip
    (Size: 6.32KB, Downloaded 212 times)
Re: Struggle with Serial port class [message #27818 is a reply to message #27817] Thu, 05 August 2010 11:42 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Single-threaded:
void	CommPak::CheckRxTmr()
{
	char buff[65]; // Last byte reserved for 0 terminator
	
	while (CommPort.ReadDataWaiting() > 16) {
		int bytes_read = CommPort.ReadData(buff, 64);
		buff[bytes_read] = 0;
		esText.Insert(esText.GetLength(), buff);		
	}
}


Simplest multi-threaded implementation (a more complex version with a locked buffer would be better):
void ReadSerialData(int packet_size, DocEdit *edit) {
	char buff[65]; // Last byte reserved for 0 terminator
	
	for (;;) {
		while (CommPort.ReadDataWaiting() > 16) {
			int bytes_read = CommPort.ReadData(buff, 64);
			buff[bytes_read] = 0;
			{ 
				GUILock _; // This locks the GUI thread (until end of scope) 
						   // so that we can safely update the Ctrl
				edit->Insert(edit->GetLength(), buff);		
			}
		}
	}
}

To run the thread:
CoWork().Do(THISBACK2(ReadSerialData(16, &esText));

Re: Struggle with Serial port class [message #27819 is a reply to message #27809] Thu, 05 August 2010 11:49 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

mrjt

Perfect. Thank you very much. It works as expected. Could you please clarify my second question about the timer? Do I need to KillTimeCallback or will it be killed automatically?

Jerson

[Updated on: Thu, 05 August 2010 11:52]

Report message to a moderator

Re: Struggle with Serial port class [message #27820 is a reply to message #27819] Thu, 05 August 2010 12:04 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
The timer will automatically be desroyed when the TopWindow is deallocated. In this situation closing the window will be enough since it gets deallocated straight afterwards.

If you need to do something on window closing you are better off overriding TopWindow::Close (just remember to call TopWindow::Close in your function). WhenClose is just the Callback that gets trggered when the window cross is clicked.
Re: Struggle with Serial port class [message #27839 is a reply to message #27820] Fri, 06 August 2010 05:52 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

Does anyone know why the Serial::Close() followed by Serial::Open command not work? It seems to hang the code and I have to kill the process to regain control. Is this a Windows quirk?

The problem occurs when I try to change com ports wherein I first close the existing open com port, set new parameters and Open the new com port. If I do not do this, the program always works reliably.

this is the relevant code that is causing me grief
void	CommPak::SetupComms()
{
int Cnt;
int Port,Baud;

	WithCommSetup<TopWindow>	CommSetup;

	// stop the timer before changing anything
	if (ExistsTimeCallback(idRxTimer))
	{
		KillTimeCallback(idRxTimer);
		PromptOK("Killed the timer");
	}

	// if the port is open, close it
	if (CommPort.IsOpened())	CommPort.Close();
	
	CtrlLayoutOKCancel(CommSetup, "Setup Comms");
	for (Cnt=1;Cnt < 9;Cnt++)
		CommSetup.dlPort.Add(Format("COM%d",Cnt));
	CommSetup.dlBaud.Add("1200");
	CommSetup.dlBaud.Add("2400");
	CommSetup.dlBaud.Add("4800");
	CommSetup.dlBaud.Add("9600");
	CommSetup.dlBaud.Add("19200");
	
	CommSetup.dlPort = "COM6";	// choose to display COM6
	CommSetup.dlBaud= "9600";	// and 9600 baud
	
	if (CommSetup.Execute() == IDOK)
	{
		Port = CommSetup.dlPort.GetIndex()+1;
		Baud = CommSetup.dlBaud.GetIndex();
		DUMP(Port);
		DUMP(Baud);
		
		// Now open the port at the baud rate
		if (!CommPort.Open(Port,Baud))
			PromptOK(Format("Cannot open COM%d",Port));
		else
		{
			PromptOK(Format("COM%d opened successfully",Port));
			SetTimeCallback(-100,THISBACK(CheckRxTmr), idRxTimer);
		}		
	}
}


Thanks
Re: Struggle with Serial port class [message #27843 is a reply to message #27839] Fri, 06 August 2010 11:22 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
It seems to be Windows problem, maybe driver related, with the call to CloseHandle.

It's not a complete freeze btw, the program resumes execution after ~10 seconds.
Re: Struggle with Serial port class [message #27845 is a reply to message #27843] Fri, 06 August 2010 11:31 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

Thanks mrjt for checking it out for me. I really appreciate your help.
SUCCESS : Struggle with Serial port class [message #27851 is a reply to message #27809] Fri, 06 August 2010 19:29 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

I think I found the problem.

I've been able to work with this successfully and contribute it here as a THANK YOU to all those who helped a newbie walk an extra mile. Very Happy

This package is a very simple replacement for Windows Hyperterm when used with my hardware devices. All it does is to send a couple of commands on the serial port which is selected and listen for the response. The response goes to the text box which can be saved to a file.

thanks
  • Attachment: CommPak.zip
    (Size: 8.10KB, Downloaded 315 times)

[Updated on: Fri, 06 August 2010 20:02]

Report message to a moderator

Re: [SUCCESS] Struggle with Serial port class [message #39394 is a reply to message #27809] Fri, 15 March 2013 14:44 Go to previous messageGo to next message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
Hi,

Please, see this message. Thks.
Re: [SUCCESS] Struggle with Serial port class [message #39446 is a reply to message #39394] Sat, 16 March 2013 15:53 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 741
Registered: January 2008
Location: Moscow, Russia
Contributor

If anybody find linux working example...
I developed Linux analog CommPack and used in my projects.
(but it normal work only under root user)
So if needed, I'll prepare to upload my variant of CommPack.


SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
Re: [SUCCESS] Struggle with Serial port class [message #39447 is a reply to message #39446] Sat, 16 March 2013 16:39 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Hi,

Quote:


If anybody find linux working example...
I developed Linux analog CommPack and used in my projects.
(but it normal work only under root user)
So if needed, I'll prepare to upload my variant of CommPack.



yes, will be appreciated.


Warm Regards

Deepak
Re: [SUCCESS] Struggle with Serial port class [message #39458 is a reply to message #39447] Sun, 17 March 2013 08:44 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 741
Registered: January 2008
Location: Moscow, Russia
Contributor

CommPackPuloon (Puloon-2000 - some pay equipment)
This program to diagnostic this device.



SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
Re: [SUCCESS] Struggle with Serial port class [message #39469 is a reply to message #39458] Sun, 17 March 2013 17:14 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Sergey,

Thank you.


Warm Regards

Deepak
Re: [SUCCESS] Struggle with Serial port class [message #39474 is a reply to message #39446] Tue, 19 March 2013 09:56 Go to previous message
Didier is currently offline  Didier
Messages: 593
Registered: November 2008
Location: France
Contributor
sergeynikitin wrote on Sat, 16 March 2013 15:53

If anybody find linux working example...
I developed Linux analog CommPack and used in my projects.
(but it normal work only under root user)
So if needed, I'll prepare to upload my variant of CommPack.

You can make it work for any user if you add the group 'dialout' to the user that needs access to the serial port
Previous Topic: THISBACK
Next Topic: Changing the slider thumb
Goto Forum:
  


Current Time: Fri Jan 22 17:58:51 CET 2021

Total time taken to generate the page: 0.01660 seconds