kachofool
27th November 2009, 00:16
Hey all,
I'm trying to write to a serial device using QextSerialPort. Here are some things I should note:
*I can read from the device, but cannot write to it.
*I can read and write from the device using a serial communication tool (cutecom)
*Using a loopback serial cable, I can see that write is working
Based on this info, I know it seems like the problem is device related, and for what its worth, it seems to have a really flaky serial I/F. However, I've been able to use it with normal linux serial drivers, and I can communicate with the device with cutecom (a linux gui app that r/w to serial ports -- handy for testing) no problem. This leads me to believe I'm doing something wrong with QextSerialPort that I don't understand. I made a simple program similar to the "events" example included with the source.
I'd really appreciate any advice!
-KF
// 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(QextSerialP ort* 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();
}
I'm trying to write to a serial device using QextSerialPort. Here are some things I should note:
*I can read from the device, but cannot write to it.
*I can read and write from the device using a serial communication tool (cutecom)
*Using a loopback serial cable, I can see that write is working
Based on this info, I know it seems like the problem is device related, and for what its worth, it seems to have a really flaky serial I/F. However, I've been able to use it with normal linux serial drivers, and I can communicate with the device with cutecom (a linux gui app that r/w to serial ports -- handy for testing) no problem. This leads me to believe I'm doing something wrong with QextSerialPort that I don't understand. I made a simple program similar to the "events" example included with the source.
I'd really appreciate any advice!
-KF
// 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(QextSerialP ort* 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();
}