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 | |
---|
11 | QespTest::QespTest(QWidget* parent) |
---|
12 | : QWidget(parent) |
---|
13 | |
---|
14 | { |
---|
15 | //modify the port settings on your own |
---|
16 | port = new QextSerialPort("COM1"); |
---|
17 | port->setBaudRate(BAUD19200); |
---|
18 | port->setFlowControl(FLOW_OFF); |
---|
19 | port->setParity(PAR_NONE); |
---|
20 | port->setDataBits(DATA_8); |
---|
21 | port->setStopBits(STOP_2); |
---|
22 | |
---|
23 | message = new QLineEdit(this); |
---|
24 | |
---|
25 | // transmit receive |
---|
26 | QPushButton *transmitButton = new QPushButton("Transmit"); |
---|
27 | connect(transmitButton, SIGNAL(clicked()), SLOT(transmitMsg())); |
---|
28 | QPushButton *receiveButton = new QPushButton("Receive"); |
---|
29 | connect(receiveButton, SIGNAL(clicked()), SLOT(receiveMsg())); |
---|
30 | QHBoxLayout* trLayout = new QHBoxLayout; |
---|
31 | trLayout->addWidget(transmitButton); |
---|
32 | trLayout->addWidget(receiveButton); |
---|
33 | |
---|
34 | //CR LF |
---|
35 | QPushButton *CRButton = new QPushButton("CR"); |
---|
36 | connect(CRButton, SIGNAL(clicked()), SLOT(appendCR())); |
---|
37 | QPushButton *LFButton = new QPushButton("LF"); |
---|
38 | connect(LFButton, SIGNAL(clicked()), SLOT(appendLF())); |
---|
39 | QHBoxLayout *crlfLayout = new QHBoxLayout; |
---|
40 | crlfLayout->addWidget(CRButton); |
---|
41 | crlfLayout->addWidget(LFButton); |
---|
42 | |
---|
43 | //open close |
---|
44 | QPushButton *openButton = new QPushButton("Open"); |
---|
45 | connect(openButton, SIGNAL(clicked()), SLOT(openPort())); |
---|
46 | QPushButton *closeButton = new QPushButton("Close"); |
---|
47 | connect(closeButton, SIGNAL(clicked()), SLOT(closePort())); |
---|
48 | QHBoxLayout *ocLayout = new QHBoxLayout; |
---|
49 | ocLayout->addWidget(openButton); |
---|
50 | ocLayout->addWidget(closeButton); |
---|
51 | |
---|
52 | received_msg = new QTextEdit(); |
---|
53 | |
---|
54 | QVBoxLayout *myVBox = new QVBoxLayout; |
---|
55 | myVBox->addWidget(message); |
---|
56 | myVBox->addLayout(crlfLayout); |
---|
57 | myVBox->addLayout(trLayout); |
---|
58 | myVBox->addLayout(ocLayout); |
---|
59 | myVBox->addWidget(received_msg); |
---|
60 | setLayout(myVBox); |
---|
61 | |
---|
62 | qDebug("isOpen : %d", port->isOpen()); |
---|
63 | } |
---|
64 | |
---|
65 | QespTest::~QespTest() |
---|
66 | { |
---|
67 | delete port; |
---|
68 | port = NULL; |
---|
69 | } |
---|
70 | |
---|
71 | void QespTest::transmitMsg() |
---|
72 | { |
---|
73 | int i = port->write((message->text()).toAscii(), |
---|
74 | (message->text()).length()); |
---|
75 | qDebug("trasmitted : %d", i); |
---|
76 | } |
---|
77 | |
---|
78 | void QespTest::receiveMsg() |
---|
79 | { |
---|
80 | char buff[1024]; |
---|
81 | int numBytes; |
---|
82 | |
---|
83 | numBytes = port->bytesAvailable(); |
---|
84 | if(numBytes > 0) |
---|
85 | { |
---|
86 | if(numBytes > 1024) numBytes = 1024; |
---|
87 | |
---|
88 | int i = port->read(buff, numBytes); |
---|
89 | buff[i] = '\0'; |
---|
90 | QString msg = buff; |
---|
91 | |
---|
92 | received_msg->append(msg); |
---|
93 | received_msg->ensureCursorVisible(); |
---|
94 | qDebug("bytes available: %d", numBytes); |
---|
95 | qDebug("received: %d", i); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void QespTest::appendCR() |
---|
101 | { |
---|
102 | message->insert("\x0D"); |
---|
103 | } |
---|
104 | |
---|
105 | void QespTest::appendLF() |
---|
106 | { |
---|
107 | message->insert("\x0A"); |
---|
108 | } |
---|
109 | |
---|
110 | void QespTest::closePort() |
---|
111 | { |
---|
112 | port->close(); |
---|
113 | qDebug("is open: %d", port->isOpen()); |
---|
114 | } |
---|
115 | |
---|
116 | void QespTest::openPort() |
---|
117 | { |
---|
118 | port->open(QIODevice::ReadWrite); |
---|
119 | qDebug("is open: %d", port->isOpen()); |
---|
120 | } |
---|
121 | |
---|