source: pacpusframework/branches/2.0-beta1/src/PacpusTools/src/PacpusSerialPort.cpp@ 120

Last change on this file since 120 was 120, checked in by morasjul, 11 years ago
  • add PacpusSerialport (QT 5.1 required)
  • add part of QT4 / QT5 cmake script
  • fix CMake (link)
  • add ComponentBase virtual function addInput() & addOutput() call before component configuration
File size: 6.4 KB
Line 
1/*********************************************************************
2// created: 2013/07/10
3// filename: PacpusSerialPort.cpp
4//
5// author: Julien Moras
6//
7// version: $Id: $
8//
9// purpose: This class defines a Qt 5.1 driver for serial port
10 *********************************************************************/
11
12#include <Pacpus/PacpusTools/PacpusSerialPort.h>
13
14#include <Pacpus/kernel/ComponentFactory.h>
15#include <Pacpus/kernel/Log.h>
16#include <Pacpus/kernel/inputOutputInterface.h>
17
18using namespace pacpus;
19
20DECLARE_STATIC_LOGGER("pacpus.core.serialport");
21
22static ComponentFactory<PacpusSerialPort> sFactory("PacpusSerialPort");
23
24PacpusSerialPort::PacpusSerialPort(QString name):ComponentBase(name) {
25
26 serialPort = new QSerialPort(this);
27 // Default settings
28#if(Win32)
29 portName = "COM1";
30#else
31 portName = "/dev/tty0";
32#endif
33 direction = QSerialPort::AllDirections;
34 baudRate = QSerialPort::Baud9600;
35 dataBits = QSerialPort::Data8;
36 parity = QSerialPort::NoParity;
37 stopBits = QSerialPort::OneStop;
38 flowControl = QSerialPort::NoFlowControl;
39 errorPolicy = QSerialPort::SkipPolicy;
40
41 dataMode = ASCII;
42 sepChar = "\n";
43
44 // Or threaded
45 connect(serialPort,SIGNAL(readyRead()),this,SLOT(readData()));
46
47}
48
49PacpusSerialPort::~PacpusSerialPort() {
50
51 if(serialPort->isOpen())
52 serialPort->close();
53 delete serialPort;
54}
55
56void PacpusSerialPort:: addInput()
57{
58 ADD_INPUT("inStream",PacpusSerialPort,QByteArray,processInputFrame);
59}
60
61void PacpusSerialPort:: addOutput()
62{
63 ADD_OUTPUT("outStream",PacpusSerialPort,QByteArray);
64 ADD_OUTPUT("pinOutSignal",PacpusSerialPort,quint16);
65}
66
67
68ComponentBase::COMPONENT_CONFIGURATION PacpusSerialPort::configureComponent(XmlComponentConfig config)
69{
70
71 /// TODO get parameter from xml
72
73 if(param.hasProperty("portName"))
74 portName = param.getProperty("portName");
75
76 if(param.hasProperty("baudRate"))
77 baudRate = (QSerialPort::BaudRate) param.getProperty("baudRate").toInt();
78
79 if(param.hasProperty("direction"))
80 direction = (QSerialPort::Direction) param.getProperty("direction").toInt();
81
82 if(param.hasProperty("dataBits"))
83 dataBits = (QSerialPort::DataBits) param.getProperty("dataBits").toInt();
84
85 if(param.hasProperty("parity"))
86 parity = (QSerialPort::Parity) param.getProperty("parity").toInt();
87
88 if(param.hasProperty("stopBits"))
89 stopBits = (QSerialPort::StopBits) param.getProperty("stopBits").toInt();
90
91 if(param.hasProperty("flowControl"))
92 flowControl = (QSerialPort::FlowControl) param.getProperty("flowControl").toInt();
93
94 if(param.hasProperty("errorPolicy"))
95 errorPolicy = (QSerialPort::DataErrorPolicy) param.getProperty("errorPolicy").toInt();
96
97 if(param.hasProperty("dataMode"))
98 dataMode = (DataMode) param.getProperty("dataMode").toInt();
99
100 if(param.hasProperty("sepChar"))
101 sepChar = param.getProperty("sepChar");
102
103 serialPort->setPortName(portName);
104
105 if(!serialPort->setBaudRate(baudRate,direction))
106 LOG_ERROR("n° "<< serialPort->error());
107 if(!serialPort->setDataBits(dataBits))
108 LOG_ERROR("n° "<< serialPort->error());
109 if(!serialPort->setParity(parity))
110 LOG_ERROR("n° "<< serialPort->error());
111 if(!serialPort->setStopBits(stopBits))
112 LOG_ERROR("n° "<< serialPort->error());
113 if(!serialPort->setFlowControl(flowControl))
114 LOG_ERROR("n° "<< serialPort->error());
115 if(!serialPort->setDataErrorPolicy(errorPolicy))
116 LOG_ERROR("n° "<< serialPort->error());
117
118 // For futher developpements
119// serialPort->setBreakEnabled();
120// serialPort->setDataTerminalReady();
121// serialPort->setRequestToSend();
122 return ComponentBase::CONFIGURED_OK;
123}
124
125void PacpusSerialPort::startActivity()
126{
127
128 if(!serialPort->open((QSerialPort::OpenModeFlag)direction))
129 { LOG_FATAL("device "<< serialPort->portName().toStdString() <<" failed to open: " << serialPort->errorString()); }
130 else
131 { LOG_INFO("listening for data on " << serialPort->portName()); }
132
133 moveToThread(&thread_);
134 thread_.start();
135
136 //TODO Remove QThread inheritance if this works fine
137 //THREAD_ALIVE = true;
138 //start();
139 setState(ComponentBase::MONITOR_OK);
140}
141
142void PacpusSerialPort::stopActivity()
143{
144 QMetaObject::invokeMethod(&thread_, "quit");
145
146 //THREAD_ALIVE = false;
147 //while(!isFinished()); // TODO timeout
148
149 serialPort->close();
150
151
152}
153
154void PacpusSerialPort::processInputFrame(const QByteArray & inputFrame)
155{
156 LOG_TRACE("Input frame " << inputFrame.data());
157
158 // Write input to serial port
159 if(serialPort->write(inputFrame)>=0)
160 {
161 setState(MONITOR_OK);
162 LOG_DEBUG("Serial write performed")
163 }
164 else
165 {
166 setState(MONITOR_NOK);
167 LOG_WARN("Serial write error")
168 }
169}
170
171void PacpusSerialPort::readData()
172{
173 int n;
174
175 buffer.append(serialPort->readAll());
176
177 while(true) // loop until no data to send
178 {
179 switch(dataMode)
180 {
181 case ASCII : // Split the first line to send
182
183 n = buffer.indexOf(sepChar,1) + 1;
184 if(n<=0)
185 return; // end line char not found
186 break;
187
188 case BINARY : // Get all data
189
190 n = buffer.size();//serialPort->bytesAvailable();
191 if(n<=0)
192 return;
193 break;
194
195 default:
196
197 LOG_WARN("unknown dataMode")
198 setState(MONITOR_NOK);
199 return;
200
201 }
202
203 QByteArray data(buffer.data(),n);
204 buffer.remove(0,n);
205
206 quint16 pinout = (quint16)serialPort->pinoutSignals();
207
208 GET_OUTPUT("outStream",PacpusSerialPort,QByteArray)->send(data);
209 GET_OUTPUT("pinOutSignal",PacpusSerialPort,quint16)->send(pinout);
210 setState(MONITOR_OK);
211 LOG_INFO("Signal Read " << n << " bytes, PinOut : " << pinout);
212 LOG_INFO(QString(data).toStdString());
213 }
214 //LOG_INFO("Buffer " << buffer.data());
215}
216
217/*void PacpusSerialPort::run()
218{
219 while (THREAD_ALIVE)
220 {
221 if (serialPort->waitForReadyRead(timeOut))
222 {
223 QByteArray data = serialPort->readAll();
224 GET_OUTPUT("outStream",PacpusSerialPort,QByteArray)->send(data);
225 GET_OUTPUT("pinOutSignal",PacpusSerialPort,quint16)->send((quint16)serialPort->pinoutSignals());
226 setState(MONITOR_OK);
227 LOG_INFO("Run Read");
228 LOG_INFO(data.data());
229 } else {
230 setState(MONITOR_NOK);
231 }
232 }
233}*/
Note: See TracBrowser for help on using the repository browser.