PIDUINO
Loading...
Searching...
No Matches
serialport.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 * This file is part of the Piduino Library.
3 *
4 * SerialPort is a modified and simplified version of SerialPort,
5 * from Qt according to the LGPL and Copyright (C) 2015 The Qt Company Ltd.
6 *
7 * The Piduino Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * The Piduino Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17#pragma once
18
19#include <deque>
20#include <string>
21#include <piduino/terminal.h>
22
29namespace Piduino {
30
77 class SerialPort : public Terminal {
78 public:
79
101 enum BaudRate {
102 Baud1200 = 1200,
103 Baud2400 = 2400,
104 Baud4800 = 4800,
105 Baud9600 = 9600,
106 Baud19200 = 19200,
107 Baud38400 = 38400,
108 Baud57600 = 57600,
109 Baud115200 = 115200,
110 UnknownBaud = -1
111 };
112
136 enum DataBits {
137 Data5 = 5,
138 Data6 = 6,
139 Data7 = 7,
140 Data8 = 8,
141 UnknownDataBits = -1
142 };
143
162
196
223
244
285
286
291 class Settings {
292 public:
305
312 unsigned long onebyteTime;
313 unsigned long rs485Delay;
314
318 bool operator== (const Settings & other);
319
323 std::string toString() const;
324
328 friend std::ostream& operator<< (std::ostream& os, const Settings & s);
329
333 std::string dataBitsString() const;
334
338 std::string parityString() const;
339
343 std::string stopBitsString() const;
344
348 std::string flowControlString() const;
349
356
360 static std::string dataBitsToString (DataBits dataBits);
361
365 static std::string parityToString (Parity parity);
366
370 static std::string stopBitsToString (StopBits stopBits);
371
376 };
377
391 class Info {
392
393 public:
394
401
405 Info (const Info & other);
406
414 explicit Info (const std::string & name);
415
419 explicit Info (const SerialPort & port);
420
425 virtual ~Info();
426
430 Info& operator= (const Info &other);
431
436 void swap (Info &other);
437
442 bool operator== (const Info & other);
443
448 bool operator!= (const Info & other);
449
455 std::string portName() const;
456
462 std::string systemLocation() const;
463
468 std::string driverName() const;
469
476 std::string description() const;
477
484 std::string manufacturer() const;
485
494 std::string serialNumber() const;
495
502 uint16_t vendorIdentifier() const;
503
511
518 uint16_t productIdentifier() const;
519
527
532 bool isNull() const;
533
537 static std::deque<Info> availablePorts ();
538
543 static std::deque<int32_t> standardBaudRates();
544
551 static Info defaultPort ();
552
553 protected:
554 class Private;
556 std::unique_ptr<Private> d_ptr;
557
558 private:
560 };
561
566
571 SerialPort (const Info & serialPortInfo);
572
579 SerialPort (const std::string & path);
580
584 virtual ~SerialPort();
585
595 void discard (Directions directions = AllDirections);
596
602 void setPort (const Info & info);
603
611 void setPortName (const std::string & name);
612
620 std::string portName() const;
621
630
647 bool setBaudRate (int32_t baudRate, Directions directions = AllDirections);
648 int32_t baudRate (Directions directions = AllDirections) const;
649
666
682 Parity parity() const;
683
700
717
731 bool setDataTerminalReady (bool set);
733
751 bool setRequestToSend (bool set);
753
771
788 bool sendBreak (int duration = 0);
789
805 bool setBreakEnabled (bool set = true);
806 bool isBreakEnabled() const;
807
808
817 virtual ssize_t write (const char * data, size_t maxSize);
818 virtual ssize_t write (const char * data, size_t maxSize, bool endl);
819
820 protected:
821 class Private;
823
824 private:
826 };
827}
832/* ========================================================================== */
virtual std::string path() const
A type-safe flags class for bitwise operations on enum values.
Definition flags.h:34
Provides information about existing serial ports.
Definition serialport.h:391
Info & operator=(const Info &other)
bool operator==(const Info &other)
Info(const SerialPort &port)
std::string portName() const
std::string systemLocation() const
uint16_t vendorIdentifier() const
static std::deque< int32_t > standardBaudRates()
std::string serialNumber() const
bool hasVendorIdentifier() const
std::unique_ptr< Private > d_ptr
Definition serialport.h:556
uint16_t productIdentifier() const
std::string description() const
Info(const std::string &name)
void swap(Info &other)
std::string manufacturer() const
bool operator!=(const Info &other)
static Info defaultPort()
Information sur le port série par défaut sur la carte Pi Dépend du modèle de carte (informations stoc...
static std::deque< Info > availablePorts()
Info(const Info &other)
bool hasProductIdentifier() const
std::string driverName() const
Provides settings information about serial ports.
Definition serialport.h:291
std::string flowControlString() const
Convert current flow control to string.
std::string parityString() const
Convert current parity to string.
std::string dataBitsString() const
Convert current data bits to string.
int32_t outputBaudRate
data baud rate for the desired direction
Definition serialport.h:307
Settings(int32_t baudRate=Baud9600, DataBits dataBits=Data8, Parity parity=NoParity, StopBits stopBits=OneStop, FlowControl flowControl=NoFlowControl)
int32_t inputBaudRate
data baud rate for the desired direction
Definition serialport.h:306
friend std::ostream & operator<<(std::ostream &os, const Settings &s)
Print settings to stream.
static std::string flowControlToString(FlowControl flowControl)
Convert flow control to string.
std::string stopBitsString() const
Convert current stop bits to string.
static std::string stopBitsToString(StopBits stopBits)
Convert stop bits to string.
FlowControl flowControl
the desired flow control mode
Definition serialport.h:311
bool operator==(const Settings &other)
equality operator
unsigned long rs485Delay
the rs485 delay in microseconds before and after send
Definition serialport.h:313
unsigned long onebyteTime
time for send on byte
Definition serialport.h:312
StopBits stopBits
the number of stop bits in a frame
Definition serialport.h:310
DataBits dataBits
number of data bits used
Definition serialport.h:308
static std::string parityToString(Parity parity)
Convert parity to string.
static std::string dataBitsToString(DataBits dataBits)
Convert data bits to string.
Parity parity
the parity checking mode
Definition serialport.h:309
void updateOneByteTime()
Update the time of an byte in accordance with the baudrate and the number of bits of data,...
std::string toString() const
Convert to string.
Provides functions to access serial ports.
Definition serialport.h:77
SerialPort(const Info &serialPortInfo)
virtual ssize_t write(const char *data, size_t maxSize, bool endl)
bool setStopBits(StopBits stopBits)
void setPort(const Info &info)
StopBits stopBits() const
the number of stop bits in a frame
Flags< PinoutSignal > PinoutSignals
Definition serialport.h:284
SerialPort(const std::string &path)
bool isBreakEnabled() const
FlowControl flowControl() const
the desired flow control mode
void discard(Directions directions=AllDirections)
bool setDataBits(DataBits dataBits)
bool setBaudRate(int32_t baudRate, Directions directions=AllDirections)
bool setFlowControl(FlowControl flowControl)
bool setRequestToSend(bool set)
bool setSettings(const Settings &settings)
void setPortName(const std::string &name)
int32_t baudRate(Directions directions=AllDirections) const
the data baud rate for the desired direction
virtual ~SerialPort()
Closes the serial port, if necessary, and then destroys object.
SerialPort(Private &dd)
Parity parity() const
the parity checking mode
bool sendBreak(int duration=0)
virtual ssize_t write(const char *data, size_t maxSize)
bool setDataTerminalReady(bool set)
bool setParity(Parity parity)
Flags< Direction > Directions
Definition serialport.h:243
bool setBreakEnabled(bool set=true)
std::string portName() const
Settings settings() const
settings information
DataBits dataBits() const
the data bits in a frame
PinoutSignals pinoutSignals()
Provides an interface for reading from and writing to terminals (tty)
Definition terminal.h:33
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Definition global.h:82
Global namespace for Piduino.
Definition board.h:28