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
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 previous 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));

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: THISBACK
Next Topic: Changing the slider thumb
Goto Forum:
  


Current Time: Wed Aug 20 10:13:03 CEST 2025

Total time taken to generate the page: 0.04638 seconds