// System Includes
#include "main.h"
// RS232 Helper Thread
RS232Thread
::RS232Thread(QextSerialPort
* port,
QObject* parent
){ serialPort = port; }
RS232Thread::~RS232Thread()
{}
void RS232Thread::run()
{
// constantly get user input
std::string inpSt;
std::cerr << "Send MVP a command string: " << std::endl;
while (inpSt.compare("quit") != 0)
{
std::getline(std::cin, inpSt);
std::cerr << serialPort->write(inpSt.c_str());
std::cerr << "Sent command: " << inpSt << std::endl;
}
serialPort->close();
}
// RS232 Signal Handler
RS232SignalHandler
::RS232SignalHandler(QextSerialPort
* port, RS232Thread
* thread,
QObject* parent
):{ serialPort = port;
sTime = thread->startTime; }
RS232SignalHandler::~RS232SignalHandler()
{}
void RS232SignalHandler::RS232ReceivedData()
{
char data[512];
int numBytesRead = serialPort->read(data, 512);
std::cerr << "DATA READ IN: " << std::endl;
// print out what we read in as chars
for (int i=0; i < numBytesRead; i++)
{ std::cerr << data[i]; }
std::cerr << std::endl;
// print out what we read in as ints
for (int i=0; i < numBytesRead; i++)
{ std::cerr << int(data[i]); }
std::cerr << std::endl;
}
// Start Main
int main(int argc, char *argv[])
{
// set up serial port
QextSerialPort *port = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
// setup helper thread
RS232Thread* hThread = new RS232Thread(port);
a.connect(hThread, SIGNAL(finished()), &a, SLOT(quit()));
hThread->start();
// setup data received signal handler
RS232SignalHandler* handler = new RS232SignalHandler(port, hThread);
handler->connect(port, SIGNAL(readyRead()), handler, SLOT(RS232ReceivedData()));
// event loop
return a.exec();
}
// System Includes
#include "main.h"
// RS232 Helper Thread
RS232Thread::RS232Thread(QextSerialPort* port, QObject* parent)
:QThread(parent)
{ serialPort = port; }
RS232Thread::~RS232Thread()
{}
void RS232Thread::run()
{
// constantly get user input
std::string inpSt;
std::cerr << "Send MVP a command string: " << std::endl;
while (inpSt.compare("quit") != 0)
{
std::getline(std::cin, inpSt);
std::cerr << serialPort->write(inpSt.c_str());
std::cerr << "Sent command: " << inpSt << std::endl;
}
serialPort->close();
}
// RS232 Signal Handler
RS232SignalHandler::RS232SignalHandler(QextSerialPort* port, RS232Thread* thread, QObject* parent):
QObject(parent)
{ serialPort = port;
sTime = thread->startTime; }
RS232SignalHandler::~RS232SignalHandler()
{}
void RS232SignalHandler::RS232ReceivedData()
{
char data[512];
int numBytesRead = serialPort->read(data, 512);
std::cerr << "DATA READ IN: " << std::endl;
// print out what we read in as chars
for (int i=0; i < numBytesRead; i++)
{ std::cerr << data[i]; }
std::cerr << std::endl;
// print out what we read in as ints
for (int i=0; i < numBytesRead; i++)
{ std::cerr << int(data[i]); }
std::cerr << std::endl;
}
// Start Main
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// set up serial port
QextSerialPort *port = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven);
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
// setup helper thread
RS232Thread* hThread = new RS232Thread(port);
a.connect(hThread, SIGNAL(finished()), &a, SLOT(quit()));
hThread->start();
// setup data received signal handler
RS232SignalHandler* handler = new RS232SignalHandler(port, hThread);
handler->connect(port, SIGNAL(readyRead()), handler, SLOT(RS232ReceivedData()));
// event loop
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks