proto-debuger/protoDebuger/serialcontroller.cpp

97 lines
2.5 KiB
C++

#include "serialcontroller.h"
SerialController::SerialController(QObject *parent) : QObject(parent)
,mCurrentPort(nullptr)
,mProto(nullptr)
{
}
int SerialController::OpenSerial(QString port, PortSettings setting)
{
//PortSettings settings = {BAUD115200, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 100};
mCurrentPort = new QextSerialPort(port, setting, QextSerialPort::EventDriven);
if(nullptr == mCurrentPort){
return NULLPOINTER;
}
mCurrentPort->setPortName(port);
mCurrentPort->open(QIODevice::ReadWrite);
if(!mCurrentPort->isOpen()){
qDebug()<< mCurrentPort->errorString();
return -1;
}
connect(mCurrentPort, SIGNAL(readyRead()), SLOT(ReadyRead()));
connect(mCurrentPort, SIGNAL(aboutToClose()),SLOT(AboutClose()));
this->mConnected = true;
this->mSetting = setting;
return OK;
}
int SerialController::CloseSerial()
{
if(this->mConnected){
this->mCurrentPort->close();
}
return OK;
}
void SerialController::FlushPorts()
{
mPorts = QextSerialEnumerator::getPorts();
int strLenMax = 0;
int index = 0;
int maxIndex = 0;
qDebug() << "List of ports:";
foreach (QextPortInfo info, mPorts) {
qDebug() << "port name:" << info.portName;
qDebug() << "friendly name:" << info.friendName;
qDebug() << "physical name:" << info.physName;
qDebug() << "enumerator name:" << info.enumName;
qDebug() << "vendor ID:" << info.vendorID;
qDebug() << "product ID:" << info.productID;
qDebug() << "===================================";
if (info.friendName.length() > strLenMax){
strLenMax = info.friendName.length();
maxIndex = index;
}
index ++;
}
}
QList<QextPortInfo> SerialController::GetPorts()
{
FlushPorts();
return mPorts;
}
int SerialController::SetProto(QSerialProto * proto)
{
this->mProto = proto;
return OK;
}
int SerialController::SendData(uint8_t *data, uint8_t len)
{
if(mConnected){
return mCurrentPort->write((char *)data,len);
}
return ERROR_SEND;
}
void SerialController::ReadyRead()
{
if (this->mCurrentPort->bytesAvailable() > 3) {
auto data = mCurrentPort->readAll();
std::string str = data.toStdString().c_str();
qDebug()<< QString::fromStdString(str);
if(nullptr != this->mProto){
mProto->OnSerailData(data);
}
}
}
void SerialController::AboutClose() {
}