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 » Developing U++ » UppHub » SerialPort & SerialPortListener class (yet another serial port class)
SerialPort & SerialPortListener class [message #46123] Sat, 12 March 2016 14:05
omari is currently offline  omari
Messages: 243
Registered: March 2010
Experienced Member
Hello,

SerialPort with non blocking read/write, and can show setting dialog when flagGUI is defined

SerialPortListener run on separate thread when flagMT is defined

Example of use:

SerialPort:
#include <Core/Core.h>
#include <SerialPort/SerialPort.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	SerialPort rs;
	
	rs.Settings().Baud9600().ParityOdd().StopBits1().DataBitsEight();
	
	rs.Open ( "Com1" );
	
	rs.Write("hello");
	
	if(rs.WaitReply())
	{
		Cout() <<  rs.Available();
		Cout() <<  rs.ReadAll();
	}
	
	rs.Write("quit");
	
	rs.Close();
	
	ReadStdIn();
}


SerialPortListener:
#include <SerialPort/SerialPort.h>

using namespace Upp;

bool quit = false;

String ProcessCommande(String& request)
{
	String response = "";
	request = ToLower(request);
	
	if(request == "help")
	{
		response = " time date quit help";
	}
	else
	if(request == "quit")
	{
		quit = true;
	}
	else
	if(request == "time")
	{
		response = AsString(GetSysTime());
	}
	else
	if(request == "date")
	{
		response = AsString(GetSysDate());
	}
	else
	{
		response = request + " : unknown request";
	}
	
	response << "\r\n";
	
	return response;
}

bool OnRequest(String request, String& response)
{
	static String buf = "";
	static int count = 0;

	// simple protocol : each frame has 4 bytes	
	for(int i = 0; i < request.GetCount(); i++)
	{
		buf.Cat(request[i]);
		count++;
		if(count == 4)
		{
			response << ProcessCommande(buf);
			count = 0;
			buf = "";
		}
	}

	return quit;
}

CONSOLE_APP_MAIN
{
	SerialPortListener listener;
	listener.PortSettings().Baud9600();
	listener.WhenDataReceived = callback(OnRequest);
	listener.Start("com2");

	listener.Wait();
}


regards
omari.

[Updated on: Sat, 12 March 2016 19:39]

Report message to a moderator

Previous Topic: Fixing bazaar - and usage survey
Next Topic: [SOLVED] OfficeAutomation: how to not open Excel file
Goto Forum:
  


Current Time: Mon Oct 18 09:28:37 CEST 2021

Total time taken to generate the page: 0.01107 seconds