#include <QCoreApplication>
#include <QtSerialPort/QtSerialPort>
#include<QDebug>
#include "windows.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int MessageLength=120;
char *MessageToSend= new char[MessageLength];
int MessageLengthToRead=MessageLength;
char *dataToReceive= new char [MessageLengthToRead];
//*****************************************************************************************************************
//Here I create two serial ports : the first one to emit and the second one to receive
QSerialPort *SerialEmitter = new QSerialPort;
QSerialPort *SerialReceiver = new QSerialPort;
//*****************************************************************************************************************
//Here we initialize serial ports
SerialEmitter->setPortName("COM9");
SerialEmitter->open(QSerialPort::OpenModeFlag::ReadWrite);
SerialEmitter->setBaudRate(QSerialPort::Baud115200);
SerialEmitter->setDataBits(QSerialPort::Data8);
SerialEmitter->setParity(QSerialPort::NoParity);
SerialEmitter->setStopBits(QSerialPort::OneStop);
//*****************************************************************************************************************
//Here I initialize the message i am going to send
SerialReceiver->setPortName("COM12");
SerialReceiver->open(QSerialPort::OpenModeFlag::ReadWrite);
SerialReceiver->setBaudRate(QSerialPort::Baud115200);
SerialReceiver->setDataBits(QSerialPort::Data8);
SerialReceiver->setParity(QSerialPort::NoParity);
SerialReceiver->setStopBits(QSerialPort::OneStop);
SerialReceiver->setFlowControl(QSerialPort::NoFlowControl);
SerialReceiver->setReadBufferSize(MessageLength);
SerialReceiver->clear();
SerialEmitter->clear();
//*****************************************************************************************************************
//Initialisation of the data to send
for (int i =0; i<MessageLength; i++)
{
MessageToSend[i]=(char)(i);
}
//Displaying Datas
for(int i=0;i<MessageLength;i++)
{
cout<<(int)((unsigned char)MessageToSend[i])<<" ";
}
cout<<endl;
cout<< "***************************************************************"<<endl;
//Here I write datas
cout<< "The value retrun of Serial emitter wtrite is "<< SerialEmitter->write(MessageToSend,MessageLength)<<endl;
//Here I wait loooong time!
SerialEmitter->waitForBytesWritten(-1);
SerialReceiver->waitForReadyRead(1000);
//Here I read datas
cout<< "Number of readed bytes "<<SerialReceiver->read(dataToReceive, MessageLength)<<endl;
for(int i=0;i<(MessageLengthToRead);i++)
{
cout<<(int)((unsigned char)dataToReceive[i])<< " ";
}
cout<<endl;
SerialEmitter->close();
SerialReceiver->close();
return a.exec();
}
Bookmarks