Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QespTest.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* QespTest.cpp
  2. **************************************/
  3. #include "QespTest.h"
  4. #include "qextserialport.h"
  5. #include <QLayout>
  6. #include <QLineEdit>
  7. #include <QTextEdit>
  8. #include <QPushButton>
  9. #include <QSpinBox>
  10. QespTest::QespTest(QWidget *parent)
  11. : QWidget(parent)
  12. {
  13. //modify the port settings on your own
  14. #ifdef Q_OS_UNIX
  15. port = new QextSerialPort(QLatin1String("/dev/ttyS0"), QextSerialPort::Polling);
  16. #else
  17. port = new QextSerialPort(QLatin1String("COM1"), QextSerialPort::Polling);
  18. #endif /*Q_OS_UNIX*/
  19. port->setBaudRate(BAUD19200);
  20. port->setFlowControl(FLOW_OFF);
  21. port->setParity(PAR_NONE);
  22. port->setDataBits(DATA_8);
  23. port->setStopBits(STOP_2);
  24. //set timeouts to 500 ms
  25. port->setTimeout(500);
  26. message = new QLineEdit(this);
  27. // transmit receive
  28. QPushButton *transmitButton = new QPushButton(tr("Transmit"));
  29. connect(transmitButton, SIGNAL(clicked()), SLOT(transmitMsg()));
  30. QPushButton *receiveButton = new QPushButton(tr("Receive"));
  31. connect(receiveButton, SIGNAL(clicked()), SLOT(receiveMsg()));
  32. QHBoxLayout *trLayout = new QHBoxLayout;
  33. trLayout->addWidget(transmitButton);
  34. trLayout->addWidget(receiveButton);
  35. //CR LF
  36. QPushButton *CRButton = new QPushButton(tr("CR"));
  37. connect(CRButton, SIGNAL(clicked()), SLOT(appendCR()));
  38. QPushButton *LFButton = new QPushButton(tr("LF"));
  39. connect(LFButton, SIGNAL(clicked()), SLOT(appendLF()));
  40. QHBoxLayout *crlfLayout = new QHBoxLayout;
  41. crlfLayout->addWidget(CRButton);
  42. crlfLayout->addWidget(LFButton);
  43. //open close
  44. QPushButton *openButton = new QPushButton(tr("Open"));
  45. connect(openButton, SIGNAL(clicked()), SLOT(openPort()));
  46. QPushButton *closeButton = new QPushButton(tr("Close"));
  47. connect(closeButton, SIGNAL(clicked()), SLOT(closePort()));
  48. QHBoxLayout *ocLayout = new QHBoxLayout;
  49. ocLayout->addWidget(openButton);
  50. ocLayout->addWidget(closeButton);
  51. received_msg = new QTextEdit();
  52. QVBoxLayout *myVBox = new QVBoxLayout;
  53. myVBox->addWidget(message);
  54. myVBox->addLayout(crlfLayout);
  55. myVBox->addLayout(trLayout);
  56. myVBox->addLayout(ocLayout);
  57. myVBox->addWidget(received_msg);
  58. setLayout(myVBox);
  59. qDebug("isOpen : %d", port->isOpen());
  60. }
  61. QespTest::~QespTest()
  62. {
  63. delete port;
  64. port = NULL;
  65. }
  66. void QespTest::transmitMsg()
  67. {
  68. int i = port->write(message->text().toLatin1());
  69. qDebug("trasmitted : %d", i);
  70. }
  71. void QespTest::receiveMsg()
  72. {
  73. char buff[1024];
  74. int numBytes;
  75. numBytes = port->bytesAvailable();
  76. if(numBytes > 1024)
  77. numBytes = 1024;
  78. int i = port->read(buff, numBytes);
  79. if (i != -1)
  80. buff[i] = '\0';
  81. else
  82. buff[0] = '\0';
  83. QString msg = QLatin1String(buff);
  84. received_msg->append(msg);
  85. received_msg->ensureCursorVisible();
  86. qDebug("bytes available: %d", numBytes);
  87. qDebug("received: %d", i);
  88. }
  89. void QespTest::appendCR()
  90. {
  91. message->insert(QLatin1String("\x0D"));
  92. }
  93. void QespTest::appendLF()
  94. {
  95. message->insert(QLatin1String("\x0A"));
  96. }
  97. void QespTest::closePort()
  98. {
  99. port->close();
  100. qDebug("is open: %d", port->isOpen());
  101. }
  102. void QespTest::openPort()
  103. {
  104. port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
  105. qDebug("is open: %d", port->isOpen());
  106. }