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

Last change on this file since 126 was 122, checked in by morasjul, 11 years ago

add function to PacpusSerialPort

File size: 8.1 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"; // TODO remove
43
44 startChar = "";
45 stopChar = "\n";
46 minPacketSize=0;
47
48 log = false; // TODO remove when component ok
49
50 // Or threaded
51 connect(serialPort,SIGNAL(readyRead()),this,SLOT(readData()));
52
53}
54
55PacpusSerialPort::~PacpusSerialPort() {
56
57 if(serialPort->isOpen())
58 serialPort->close();
59 delete serialPort;
60}
61
62void PacpusSerialPort:: addInput()
63{
64 ADD_INPUT("inStream",PacpusSerialPort,QByteArray,processInputFrame);
65}
66
67void PacpusSerialPort:: addOutput()
68{
69 ADD_OUTPUT("outStream",PacpusSerialPort,QByteArray);
70 ADD_OUTPUT("pinOutSignal",PacpusSerialPort,quint16);
71}
72
73
74ComponentBase::COMPONENT_CONFIGURATION PacpusSerialPort::configureComponent(XmlComponentConfig config)
75{
76
77 /// TODO get parameter from xml
78
79 if(param.hasProperty("portName"))
80 portName = param.getProperty("portName");
81
82 if(param.hasProperty("baudRate"))
83 baudRate = (QSerialPort::BaudRate) param.getProperty("baudRate").toInt();
84
85 if(param.hasProperty("direction"))
86 direction = (QSerialPort::Direction) param.getProperty("direction").toInt();
87
88 if(param.hasProperty("dataBits"))
89 dataBits = (QSerialPort::DataBits) param.getProperty("dataBits").toInt();
90
91 if(param.hasProperty("parity"))
92 parity = (QSerialPort::Parity) param.getProperty("parity").toInt();
93
94 if(param.hasProperty("stopBits"))
95 stopBits = (QSerialPort::StopBits) param.getProperty("stopBits").toInt();
96
97 if(param.hasProperty("flowControl"))
98 flowControl = (QSerialPort::FlowControl) param.getProperty("flowControl").toInt();
99
100 if(param.hasProperty("errorPolicy"))
101 errorPolicy = (QSerialPort::DataErrorPolicy) param.getProperty("errorPolicy").toInt();
102
103 if(param.hasProperty("dataMode"))
104 dataMode = (DataMode) param.getProperty("dataMode").toInt();
105
106 if(param.hasProperty("sepChar"))
107 sepChar = param.getProperty("sepChar");
108
109 if(param.hasProperty("startChar"))
110 startChar = param.getProperty("startChar");
111
112 if(param.hasProperty("stopChar"))
113 stopChar = param.getProperty("stopChar");
114
115 if(param.hasProperty("minPacketSize"))
116 minPacketSize = (DataMode) param.getProperty("minPacketSize").toInt();
117
118 serialPort->setPortName(portName);
119
120 if(!serialPort->setBaudRate(baudRate,direction))
121 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
122 if(!serialPort->setDataBits(dataBits))
123 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
124 if(!serialPort->setParity(parity))
125 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
126 if(!serialPort->setStopBits(stopBits))
127 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
128 if(!serialPort->setFlowControl(flowControl))
129 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
130 if(!serialPort->setDataErrorPolicy(errorPolicy))
131 LOG_ERROR(getName() << "\t error n° "<< serialPort->error());
132
133 // For futher developpements
134// serialPort->setBreakEnabled();
135// serialPort->setDataTerminalReady();
136// serialPort->setRequestToSend();
137 return ComponentBase::CONFIGURED_OK;
138}
139
140void PacpusSerialPort::startActivity()
141{
142
143 buffer.clear();
144
145 if(!serialPort->open((QSerialPort::OpenModeFlag)direction))
146 { LOG_FATAL("device "<< serialPort->portName().toStdString() <<" failed to open: " << serialPort->errorString()); }
147 else
148 { LOG_INFO("listening for data on " << serialPort->portName()); }
149
150 moveToThread(&thread_);
151 thread_.start();
152
153 //TODO Remove QThread inheritance if this works fine
154 //THREAD_ALIVE = true;
155 //start();
156 setState(ComponentBase::MONITOR_OK);
157}
158
159void PacpusSerialPort::stopActivity()
160{
161 QMetaObject::invokeMethod(&thread_, "quit");
162
163 //THREAD_ALIVE = false;
164 //while(!isFinished()); // TODO timeout
165
166 serialPort->close();
167
168
169}
170
171void PacpusSerialPort::processInputFrame(const QByteArray & inputFrame)
172{
173 LOG_TRACE("Input frame " << inputFrame.data());
174
175 // Write input to serial port
176 if(serialPort->write(inputFrame)>=0)
177 {
178 setState(MONITOR_OK);
179 LOG_DEBUG(getName() << " Serial write performed")
180 }
181 else
182 {
183 setState(MONITOR_NOK);
184 LOG_WARN(getName() << " Serial write error")
185 }
186}
187
188void PacpusSerialPort::readData()
189{
190 int n;
191
192 if(dataMode == BINARY & minPacketSize >0)
193 if(serialPort->bytesAvailable() < minPacketSize)
194 return;
195
196 buffer += serialPort->readAll();
197
198 while(true) // loop until no data to send
199 {
200 switch(dataMode)
201 {
202 case ASCII : // Split the first line to send
203 {
204 if(startChar.size()>0)
205 {
206 int indexStart = buffer.indexOf(startChar,0);
207 if(indexStart < 0)
208 return;
209 else
210 buffer.remove(0,indexStart);
211 }
212
213 int indexStop = buffer.indexOf(stopChar,1);
214 if(indexStop < 1)
215 return;
216
217 n=indexStop;
218 break;
219
220 // This work but replace by previous code that need to be test
221// n = buffer.indexOf(sepChar,1) + 1;
222// if(n<=0)
223// return; // end line char not found
224// break;
225 }
226 case BINARY : // Get all data
227 n = buffer.size();//serialPort->bytesAvailable();
228 if(n<=0)
229 return;
230 break;
231
232 default:
233
234 LOG_WARN("unknown dataMode")
235 setState(MONITOR_NOK);
236 return;
237
238 }
239
240 QByteArray data(buffer.data(),n);
241 buffer.remove(0,n);
242
243 quint16 pinout = (quint16)serialPort->pinoutSignals();
244
245 GET_OUTPUT("outStream",PacpusSerialPort,QByteArray)->send(data);
246 GET_OUTPUT("pinOutSignal",PacpusSerialPort,quint16)->send(pinout);
247 setState(MONITOR_OK);
248 LOG_DEBUG("Signal Read " << n << " bytes, PinOut : " << pinout);
249 LOG_DEBUG(data.data());
250
251 if(log)
252 {
253 switch(dataMode)
254 {
255 case ASCII :
256 LOG_INFO("Buffer " << buffer.data());
257
258 case BINARY :
259 {
260 QByteArray str;
261 for(int i =0;i<22 ;i++)
262 str+= QByteArray::number((uchar)buffer.data()[i],16).toUpper() + " ";
263 LOG_INFO("Value " << str.data());
264 }
265 }
266 }
267 }
268}
269
270/*void PacpusSerialPort::run()
271{
272 while (THREAD_ALIVE)
273 {
274 if (serialPort->waitForReadyRead(timeOut))
275 {
276 QByteArray data = serialPort->readAll();
277 GET_OUTPUT("outStream",PacpusSerialPort,QByteArray)->send(data);
278 GET_OUTPUT("pinOutSignal",PacpusSerialPort,quint16)->send((quint16)serialPort->pinoutSignals());
279 setState(MONITOR_OK);
280 LOG_INFO("Run Read");
281 LOG_INFO(data.data());
282 } else {
283 setState(MONITOR_NOK);
284 }
285 }
286}*/
Note: See TracBrowser for help on using the repository browser.