Home » Developing U++ » UppHub » SerialPort & SerialPortListener class (yet another serial port class)
| SerialPort & SerialPortListener class [message #46123] |
Sat, 12 March 2016 14:05  |
omari
Messages: 276 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
|
|
|
|
| Re: SerialPort & SerialPortListener class [message #61858 is a reply to message #46123] |
Wed, 19 November 2025 14:34  |
ortman
Messages: 1 Registered: November 2025
|
Junior Member |
|
|
Dear omari!
I'd really like to see it listed in UppHub components in TheIDE. It's not there, and installing it manually by unzipping the archive is inconvenient.
To make the component available in TheIDE, you need to host SerialPort in a public GIT repository (e.g., GitHub) and format it according to the requirements.
Are there any other issues?
Please host this component in GIT and add it to TheIDE.
If you don't have the time or inclination, please allow me to take care of this. I can create a repository, add the missing files, provide support, and attribute you as the author.
|
|
|
|
Goto Forum:
Current Time: Thu Nov 27 06:46:03 CET 2025
Total time taken to generate the page: 0.05866 seconds
|