不使用Windows应用程序的串行端口通信

时间:2019-05-22 10:26:09

标签: c++ embedded

#include "RunAmrDevice.h"


extern "C" {
#include "General.h"
#include "GlobalVariables.h"
#include "Interrupt.h"
#include "Definitions.h"
#include "MQTT/PahoMqTT/MQTTClient_C/MQTTClient.h"
}


uint8_t PcMeterPortInitialised;     /*To check whther the port is intitialised or not*/
//extern void test1_sendAndReceive(MQTTClient* c, int qos, char* test_topic);
extern int readPacket(MQTTClient* c, Timer* timer);
extern  int sendPacket(MQTTClient* c, int length, Timer* timer);
extern MQTTClient MQTTClientdata;

unsigned char readbuf;

#ifndef __CCRL__
extern stPCMODE stPcMode;
#endif

QT_USE_NAMESPACE


/**
* @brief PcMeterPortHandler::TaskStart
*/

void PcMeterPortHandler::TaskStart(){
    /*Initialisse the another com port for PC/Meter communucation*/
    QList<QSerialPortInfo> List;
    List = QSerialPortInfo::availablePorts();
//    meterPortthread.comSetting.Baud = QSerialPort::Baud4800;
//    meterPortthread.comSetting.Data = QSerialPort::Data7;
//    meterPortthread.comSetting.Parity = QSerialPort::EvenParity;
    meterPortthread.comSetting.Baud = QSerialPort::Baud9600;
    meterPortthread.comSetting.Data = QSerialPort::Data8;
    meterPortthread.comSetting.Parity = QSerialPort::NoParity;
    meterPortthread.comSetting.StopBit = QSerialPort::OneStop;
    /*Currently we are enabling it we found that it is working so i have enabled it
     * Actually meter will not have flow control enabled
     * after testing we found that it si working so retained it
     */
    meterPortthread.comSetting.FlowCtrl = QSerialPort::HardwareControl;
    //This is meter port
    //Virtual com port 7
        meterPortthread.startSlave(List[1].portName(),10,"Hello ");
    //USB serial com port 5   
}

SlaveThreadMeter::SlaveThreadMeter(QObject *parent)
    : QThread(parent), waitTimeout(0), quit(false)
{
}
//! [0]
SlaveThreadMeter::~SlaveThreadMeter()
{
    mutex.lock();
    quit = true;
    mutex.unlock();
    wait();
}
//! [0]

//! [1] //! [2]
void SlaveThreadMeter::startSlave(const QString &portName, int waitTimeout, const QString &response)
{
    //! [1]
    QMutexLocker locker(&mutex);
    this->portName = portName;
    this->waitTimeout = waitTimeout;
    this->response = response;
    //! [3]
    if (!isRunning())
        start();
}
//! [2] //! [3]

//! [4]
void SlaveThreadMeter::run()
{
    bool currentPortNameChanged = false;
    unsigned char curConfig=PC_CUR_MODE_IEC;
    bool retval = false;

    mutex.lock();
    //! [4] //! [5]
    QString currentPortName;
    if (currentPortName != portName) {
        currentPortName = portName;
        currentPortNameChanged = true;
    }

    int currentWaitTimeout = waitTimeout;
    QString currentRespone = response;
    mutex.unlock();
    //! [5] //! [6]
    QSerialPort serial;

    /*Set the stream*/
    meterStream.setDevice(&serial);

    while (!quit) {
        //![6] //! [7]
        if (currentPortNameChanged) {
            serial.close();
            retval = SlaveThreadMeter::openTheSerialPort(portName,serial);
            if(retval ==  true){
                currentPortNameChanged = false;
                PcMeterPortInitialised = 1;
            }
        }

//        if(stPcMode.pcCurMode != PC_CUR_MODE_IEC){
//            stPcMode.factoryModeConfigured = 1;
//            stPcMode.dlmsModeConfigured    = 1;
//            continue;
//        }
        if(!(stPcMode.pcCurMode == PC_CUR_MODE_FACTORY || stPcMode.pcCurMode == PC_CUR_MODE_IEC))
        {
            stPcMode.dlmsModeConfigured    = 1;
            continue;
        }

        //        //Handle port configurations
        //        if(curConfig != stPcMode.pcCurMode && stPcMode.pcCurMode != 0){
        //            if(stPcMode.pcCurMode == PC_CUR_MODE_IEC){
        //                comSetting.FlowCtrl = QSerialPort::HardwareControl;
        //            }
        //            else if(stPcMode.pcCurMode == PC_CUR_MODE_FACTORY){
        //                comSetting.FlowCtrl = QSerialPort::NoFlowControl;
        //            }
        //            serial.close();
        //            retval = SlaveThreadMeter::openTheSerialPort(portName,serial);
        //            if(retval == true && stPcMode.pcCurMode == PC_CUR_MODE_IEC){
        //                stPcMode.iecModeConfigured = 1;
        //                curConfig = stPcMode.pcCurMode;
        //            }
        //            else if(retval == true && stPcMode.pcCurMode == PC_CUR_MODE_FACTORY){
        //                stPcMode.factoryModeConfigured = 1;
        //                curConfig = stPcMode.pcCurMode;
        //            }
        //        }

        //        //To handle condition, where the data is cleared & current mode of configuration is IEC
        //        if(curConfig == PC_CUR_MODE_IEC && stPcMode.iecModeConfigured == 0){
        //            stPcMode.iecModeConfigured = 1;
        //        }
        if(stPcMode.pcCurMode == PC_CUR_MODE_FACTORY && stPcMode.factoryModeConfigured == 1)
        {
            if (serial.waitForReadyRead(currentWaitTimeout)) {
                //! [7] //! [8]
                /*Read the all the data*/
                QByteArray factoryData;
               // MQTTClient factdata;
                int readCount=0;
                unsigned char RxByte[205]={0};
                do {
                    readCount = meterStream.readRawData((char*)RxByte, 200);
                    if(readCount>0){
                        factoryData.append((char*)RxByte,readCount);
                        for(int i=0;i<factoryData.size();i++){
                            //fprintf(stderr,"%c",(uint8_t)factoryData.at(i));
                          //  Meter_Recv_ISR(factoryData.at(i));
                            //virtual serial port name

                             readbuf=factoryData.at(i);
                             MQTTClientdata.readbuf=&readbuf;
                             MQTTClientdata.readbuf++;
                        }
                        factoryData.clear();
                    }
                }while (readCount>0);
                //! [8] //! [10]
                // write response

                //! [12]
                //! [10] //! [11] //! [12]

                //! [9] //! [11]
            }
            /*Add the writing part*/
            else if(UI_Win32SendCountPcMtrPort){
                // char txByte = g_Win32SendBytePcMtrPort;
                serial.write((char *)pSC_Win32SendBytePcMtrPort,UI_Win32SendCountPcMtrPort);
                serial.waitForBytesWritten(300);

                //  fprintf(stderr,"%c",g_Win32SendBytePcMtrPort);
                UI_Win32SendCountPcMtrPort = 0;
            }
            else {
                emit timeout(tr("Wait read request timeout %1")
                             .arg(QTime::currentTime().toString()));
            }
            //! [9]  //! [13]
            mutex.lock();
            if (currentPortName != portName) {
                currentPortName = portName;
                currentPortNameChanged = true;
            } else {
                currentPortNameChanged = false;
            }
            currentWaitTimeout = waitTimeout;
            currentRespone = response;
            mutex.unlock();
        }
        else if(stPcMode.pcCurMode == PC_CUR_MODE_IEC && stPcMode.iecModeConfigured ==1){
            if (serial.waitForReadyRead(currentWaitTimeout)) {
                //! [7] //! [8]
                // read request
                //            QByteArray requestData = serial.readAll();
                //            while (serial.waitForReadyRead(20))
                //                requestData += serial.readAll();
                //            fprintf(stderr,"\nRead Data is \n");
                //            for(int i=0;i<requestData.size();i++){
                //                fprintf(stderr,"%c",(uint8_t)requestData.at(i));
                //                Meter_Recv_ISR(requestData.at(i));
                //            }
                //            fprintf(stderr,"\n");
                //Read the all the data
                QByteArray meterData;
                int readCount=0;
                unsigned char RxByte[205]={0};
                do {
                    readCount = meterStream.readRawData((char*)RxByte, 200);
                    if(readCount>0){
                        meterData.append((char*)RxByte,readCount);
                        for(int i=0;i<meterData.size();i++){
                            //                        fprintf(stderr,"%c",(uint8_t)meterData.at(i));
                            Meter_Recv_ISR(meterData.at(i));
                        }
                        meterData.clear();
                    }
                }while (readCount>0);
                //! [8] //! [10]
                /* write response*/

                //! [12]
                //! [10] //! [11] //! [12]

                //! [9] //! [11]
            }
            /*Add the writing part*/
            else if(UI_Win32SendCountPcMtrPort){
                //            char txByte = g_Win32SendBytePcMtrPort;
                //            serial.write(&txByte,1);
                serial.write((char *)pSC_Win32SendBytePcMtrPort,UI_Win32SendCountPcMtrPort);
                serial.waitForBytesWritten(300);
                //            fprintf(stderr,"%c",g_Win32SendBytePcMtrPort);
                UI_Win32SendCountPcMtrPort = 0;
            }
            else {
                emit timeout(tr("Wait read request timeout %1")
                             .arg(QTime::currentTime().toString()));
            }
            //! [9]  //! [13]
            mutex.lock();
            if (currentPortName != portName) {
                currentPortName = portName;
                currentPortNameChanged = true;
            } else {
                currentPortNameChanged = false;
            }
            currentWaitTimeout = waitTimeout;
            currentRespone = response;
            mutex.unlock();
        }
    }
    //! [13]
}

bool SlaveThreadMeter::openTheSerialPort(QString currentPortName,QSerialPort &serial)
{
    serial.setPortName(currentPortName);

    /*Enable baud settings*/
    serial.setBaudRate(comSetting.Baud);
    serial.setDataBits(comSetting.Data);
    serial.setParity(comSetting.Parity);
    serial.setStopBits(comSetting.StopBit);
    serial.setFlowControl(comSetting.FlowCtrl);
    if (!serial.open(QIODevice::ReadWrite))
    {
        fprintf(stderr,"Fail to open the port\n");
        emit error(tr("Can't open %1, error code %2")
                   .arg(portName).arg(serial.error()));
        this->terminate();
        return false;
    }
    fprintf(stderr,"Opened the port %s \n",(char *)currentPortName.data());
    return true;
}

0 个答案:

没有答案