123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- /**
- * @file SerialPort.h
- * @author itas109 (itas109@qq.com) \n\n
- * Blog : https://blog.csdn.net/itas109 \n
- * Github : https://github.com/itas109 \n
- * QQ Group : 12951803
- * @brief a lightweight library of serial port, which can easy to read and write serical port on windows and linux with
- * C++ 轻量级跨平台串口读写类库
- * @version 4.1.0.201010
- * @date 2020-10-10
- * @copyright The CSerialPort is Copyright (C) 2021 itas109. \n
- * Contact: itas109@qq.com \n\n
- * You may use, distribute and copy the CSerialPort under the terms of \n
- * GNU Lesser General Public License version 3, which is displayed below.
- */
- #ifndef __CSERIALPORT_H__
- #define __CSERIALPORT_H__
- #include "SerialPort_global.h"
- #include <string>
- #include "sigslot.h"
- using namespace sigslot;
- #include "CSerialPort/SerialPortUnixBase.h"
- #define CSERIALPORTBASE CSerialPortUnixBase
- #include <iostream>
- class CSerialPortBase;
- namespace itas109
- {
- /**
- * @brief the CSerialPort class 串口类库
- * @see reference 引用 CSerialPortBase
- */
- class DLL_EXPORT CSerialPort : public has_slots<>
- {
- public:
- /**
- * @brief Construct a new CSerialPort object 构造函数
- *
- */
- CSerialPort(){
- p_serialPortBase = new CSERIALPORTBASE();
- p_serialPortBase->setMinByteReadNotify(1);
- ((CSERIALPORTBASE *)p_serialPortBase)->readReady.connect(this, &CSerialPort::onReadReady);
- }
- /**
- * @brief Construct a new CSerialPort object 通过串口名称构造函数
- *
- * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
- */
- CSerialPort(const std::string &portName){
- p_serialPortBase = new CSERIALPORTBASE(portName);
- p_serialPortBase->setMinByteReadNotify(1);
- ((CSERIALPORTBASE *)p_serialPortBase)->readReady.connect(this, &CSerialPort::onReadReady);
- }
- /**
- * @brief Destroy the CSerialPort object 析构函数
- *
- */
- ~CSerialPort(){
- ((CSERIALPORTBASE *)p_serialPortBase)->readReady.disconnect_all();
- if (p_serialPortBase)
- {
- delete p_serialPortBase;
- p_serialPortBase = NULL;
- }
- }
- /**
- * @brief init 初始化函数
- *
- * @param portName [in] the port name串口名称 Windows:COM1 Linux:/dev/ttyS0
- * @param baudRate [in] the baudRate 波特率
- * @param parity [in] the parity 校验位
- * @param dataBits [in] the dataBits 数据位
- * @param stopbits [in] the stopbits 停止位
- * @param flowControl [in] flowControl type 流控制
- * @param readBufferSize [in] the read buffer size 读取缓冲区大小
- */
- void init(std::string portName,
- int baudRate = itas109::BaudRate115200,
- itas109::Parity parity = itas109::ParityNone,
- itas109::DataBits dataBits = itas109::DataBits8,
- itas109::StopBits stopbits = itas109::StopOne,
- itas109::FlowControl flowControl = itas109::FlowNone,
- int64 readBufferSize = 512){
- if (p_serialPortBase)
- {
- p_serialPortBase->init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);
- }
- }
- /**
- * @brief Set the Operate Mode object 设置串口操作模式
- *
- * @param operateMode [in] the operate mode 串口操作模式 {@link itas109::OperateMode}
- */
- void setOperateMode(itas109::OperateMode operateMode = itas109::AsynchronousOperate){
- if (p_serialPortBase)
- {
- p_serialPortBase->setOperateMode(operateMode);
- }
- }
- /**
- * @brief open serial port 打开串口
- *
- * @return
- * @retval true open success 打开成功
- * @retval false open failed 打开失败
- */
- bool open(){
- if (p_serialPortBase)
- {
- return p_serialPortBase->openPort();
- }
- else
- {
- return false;
- }
- }
- /**
- * @brief close 关闭串口
- *
- */
- void close(){
- if (p_serialPortBase)
- {
- p_serialPortBase->closePort();
- }
- }
- /**
- * @brief if serial port is open success 串口是否打开成功
- *
- * @return
- * @retval true serial port open success 串口打开成功
- * @retval false serial port open failed 串口打开失败
- */
- bool isOpened(){
- if (p_serialPortBase)
- {
- return p_serialPortBase->isOpened();
- }
- else
- {
- return false;
- }
- }
- /**
- * @brief read specified length data 读取指定长度数据
- *
- * @param data [out] read data result 读取结果
- * @param maxSize [in] read length 读取长度
- * @return return number Of bytes read 返回读取字节数
- * @retval -1 read error 读取错误
- * @retval [other] return number Of bytes read 返回读取字节数
- */
- int readData(char *data, int maxSize){
- if (p_serialPortBase)
- {
- return p_serialPortBase->readData(data, maxSize);
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief read all data 读取所有数据
- *
- * @param data [out] read data result 读取结果
- * @return return number Of bytes read 返回读取字节数
- * @retval -1 read error 读取错误
- * @retval [other] return number Of bytes read 返回读取字节数
- */
- int readAllData(char *data){
- if (p_serialPortBase)
- {
- return p_serialPortBase->readAllData(data);
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief read line data 读取一行字符串
- * @todo Not implemented 未实现
- *
- * @param data
- * @param maxSize
- * @return int
- */
- int readLineData(char *data, int maxSize){
- if (p_serialPortBase)
- {
- return p_serialPortBase->readLineData(data, maxSize);
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief write specified lenfth data 写入指定长度数据
- *
- * @param data [in] write data 待写入数据
- * @param maxSize [in] wtite length 写入长度
- * @return return number Of bytes write 返回写入字节数
- * @retval -1 read error 写入错误
- * @retval [other] return number Of bytes write 返回写入字节数
- */
- int writeData(const char *data, int maxSize){
- if (p_serialPortBase)
- {
- return p_serialPortBase->writeData(data, maxSize);
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief Set Debug Model 设置调试模式
- * @details output serial port read and write details info 输出串口读写的详细信息
- * @todo Not implemented 未实现
- *
- * @param isDebug true if enable true为启用
- */
- void setDebugModel(bool isDebug){
- if (p_serialPortBase)
- {
- p_serialPortBase->setDebugModel(isDebug);
- }
- }
-
- /**
- * @brief Set the Read Time Interval object
- * @details use timer import effectiveness 使用定时器提高效率
- * @todo Not implemented 未实现
- *
- * @param msecs read time micro second 读取间隔时间,单位:毫秒
- */
- void setReadTimeInterval(int msecs){
- if (p_serialPortBase)
- {
- p_serialPortBase->setReadTimeInterval(msecs);
- }
- }
- /**
- * @brief setMinByteReadNotify set minimum byte of read notify 设置读取通知触发最小字节数
- * @param minByteReadNotify minimum byte of read notify 读取通知触发最小字节数
- */
- void setMinByteReadNotify(unsigned int minByteReadNotify = 2){
- if (p_serialPortBase)
- {
- p_serialPortBase->setMinByteReadNotify(minByteReadNotify);
- }
- }
- /**
- * @brief Get the Last Error object 获取最后的错误代码
- *
- * @return return last error code, refrence {@link itas109::SerialPortError} 错误代码
- */
- int getLastError() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getLastError();
- }
- else
- {
- // null error
- return itas109::/*SerialPortError::*/ SystemError;
- }
- }
- /**
- * @brief clear error 清除错误信息
- *
- */
- void clearError(){
- if (p_serialPortBase)
- {
- p_serialPortBase->clearError();
- }
- }
- /**
- * @brief Set the Port Name object 设置串口名称
- *
- * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
- */
- void setPortName(std::string portName){
- if (p_serialPortBase)
- {
- p_serialPortBase->setPortName(portName);
- }
- }
- /**
- * @brief Get the Port Name object 获取串口名称
- *
- * @return return port name 返回串口名称
- */
- std::string getPortName() const{
- if (p_serialPortBase)
- {
- p_serialPortBase->getPortName();
- }
- }
- /**
- * @brief Set the Baud Rate object 设置波特率
- *
- * @param baudRate [in] the baudRate 波特率
- */
- void setBaudRate(int baudRate){
- if (p_serialPortBase)
- {
- p_serialPortBase->setBaudRate(baudRate);
- }
- }
- /**
- * @brief Get the Baud Rate object 获取波特率
- *
- * @return return baudRate 返回波特率
- */
- int getBaudRate() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getBaudRate();
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief Set the Parity object 设置校验位
- *
- * @param parity [in] the parity 校验位 {@link itas109::Parity}
- */
- void setParity(itas109::Parity parity){
- if (p_serialPortBase)
- {
- p_serialPortBase->setParity(parity);
- }
- }
- /**
- * @brief Get the Parity object 获取校验位
- *
- * @return return parity 返回校验位 {@link itas109::Parity}
- */
- itas109::Parity getParity() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getParity();
- }
- else
- {
- // should retrun error
- return itas109::/*Parity::*/ ParityNone;
- }
- }
- /**
- * @brief Set the Data Bits object 设置数据位
- *
- * @param dataBits [in] the dataBits 数据位 {@link itas109::DataBits}
- */
- void setDataBits(itas109::DataBits dataBits){
- if (p_serialPortBase)
- {
- p_serialPortBase->setDataBits(dataBits);
- }
- }
- /**
- * @brief Get the Data Bits object 获取数据位
- *
- * @return return dataBits 返回数据位 {@link itas109::DataBits}
- */
- itas109::DataBits getDataBits() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getDataBits();
- }
- else
- {
- // should retrun error
- return itas109::/*DataBits::*/ DataBits8;
- }
- }
- /**
- * @brief Set the Stop Bits object 设置停止位
- *
- * @param stopbits [in] the stopbits 停止位 {@link itas109::StopBits}
- */
- void setStopBits(itas109::StopBits stopbits){
- if (p_serialPortBase)
- {
- p_serialPortBase->setStopBits(stopbits);
- }
- }
- /**
- * @brief Get the Stop Bits object 获取停止位
- *
- * @return return stopbits 返回停止位 {@link itas109::StopBits}
- */
- itas109::StopBits getStopBits() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getStopBits();
- }
- else
- {
- // should retrun error
- return itas109::/*StopBits::*/ StopOne;
- }
- }
- /**
- * @brief Set the Flow Control object 设置流控制
- * @todo Not implemented 未实现
- *
- * @param flowControl [in]
- */
- void setFlowControl(itas109::FlowControl flowControl){
- if (p_serialPortBase)
- {
- p_serialPortBase->setFlowControl(flowControl);
- }
- }
- /**
- * @brief Get the Flow Control object 获取流控制
- * @todo Not implemented 未实现
- *
- * @return itas109::FlowControl
- */
- itas109::FlowControl getFlowControl() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getFlowControl();
- }
- else
- {
- // should retrun error
- return itas109::/*FlowControl::*/ FlowNone;
- }
- }
- /**
- * @brief Set the Read Buffer Size object
- *
- * @param size [in] read buffer size 设置缓冲区大小
- */
- void setReadBufferSize(int64 size){
- if (p_serialPortBase)
- {
- p_serialPortBase->setReadBufferSize(size);
- }
- }
- /**
- * @brief Get the Read Buffer Size object 获取读取缓冲区大小
- *
- * @return return read buffer size 返回读取缓冲区大小
- */
- int64 getReadBufferSize() const{
- if (p_serialPortBase)
- {
- return p_serialPortBase->getReadBufferSize();
- }
- else
- {
- return -1;
- }
- }
- /**
- * @brief Set the Dtr object 设置DTR
- * @todo Not implemented 未实现
- *
- * @param set [in]
- */
- void setDtr(bool set = true){
- if (p_serialPortBase)
- {
- p_serialPortBase->setDtr(set);
- }
- }
- /**
- * @brief Set the Rts object 设置RTS
- * @todo Not implemented 未实现
- *
- * @param set [in]
- */
- void setRts(bool set = true){
- if (p_serialPortBase)
- {
- p_serialPortBase->setRts(set);
- }
- }
- /**
- * @brief Get the Version object 获取版本信息
- *
- * @return return version 返回版本信息
- */
- std::string getVersion(){
- return "https://github.com/itas109/CSerialPort - V4.1.0.201010";
- }
- public:
- void onReadReady(){
- readReady._emit();
- }
- sigslot::signal0<> readReady; // sigslot
- private:
- CSerialPortBase *p_serialPortBase;
- };
- } // namespace itas109
- #endif //__CSERIALPORT_H__
|