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


Qt Code:
  1. // System Includes
  2. #include "main.h"
  3.  
  4. // RS232 Helper Thread
  5. RS232Thread::RS232Thread(QextSerialPort* port, QObject* parent)
  6. :QThread(parent)
  7. { serialPort = port; }
  8.  
  9. RS232Thread::~RS232Thread()
  10. {}
  11.  
  12. void RS232Thread::run()
  13. {
  14. // constantly get user input
  15. std::string inpSt;
  16.  
  17. std::cerr << "Send MVP a command string: " << std::endl;
  18.  
  19. while (inpSt.compare("quit") != 0)
  20. {
  21. std::getline(std::cin, inpSt);
  22. std::cerr << serialPort->write(inpSt.c_str());
  23. std::cerr << "Sent command: " << inpSt << std::endl;
  24. }
  25.  
  26. serialPort->close();
  27. }
  28.  
  29.  
  30. // RS232 Signal Handler
  31. RS232SignalHandler::RS232SignalHandler(QextSerialPort* port, RS232Thread* thread, QObject* parent):
  32. QObject(parent)
  33. { serialPort = port;
  34. sTime = thread->startTime; }
  35.  
  36. RS232SignalHandler::~RS232SignalHandler()
  37. {}
  38.  
  39. void RS232SignalHandler::RS232ReceivedData()
  40. {
  41.  
  42. char data[512];
  43. int numBytesRead = serialPort->read(data, 512);
  44.  
  45. std::cerr << "DATA READ IN: " << std::endl;
  46.  
  47. // print out what we read in as chars
  48. for (int i=0; i < numBytesRead; i++)
  49. { std::cerr << data[i]; }
  50. std::cerr << std::endl;
  51.  
  52. // print out what we read in as ints
  53. for (int i=0; i < numBytesRead; i++)
  54. { std::cerr << int(data[i]); }
  55. std::cerr << std::endl;
  56. }
  57.  
  58.  
  59. // Start Main
  60. int main(int argc, char *argv[])
  61. {
  62. QCoreApplication a(argc, argv);
  63.  
  64. // set up serial port
  65. QextSerialPort *port = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven);
  66. port->setBaudRate(BAUD9600);
  67. port->setFlowControl(FLOW_OFF);
  68. port->setParity(PAR_NONE);
  69. port->setDataBits(DATA_8);
  70. port->setStopBits(STOP_1);
  71.  
  72. // setup helper thread
  73. RS232Thread* hThread = new RS232Thread(port);
  74. a.connect(hThread, SIGNAL(finished()), &a, SLOT(quit()));
  75. hThread->start();
  76.  
  77. // setup data received signal handler
  78. RS232SignalHandler* handler = new RS232SignalHandler(port, hThread);
  79. handler->connect(port, SIGNAL(readyRead()), handler, SLOT(RS232ReceivedData()));
  80.  
  81. // event loop
  82. return a.exec();
  83. }
To copy to clipboard, switch view to plain text mode