direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
interfaceAvr.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "interfaceAvr.h"
22 
24 {
25  // get the name of this class (this is for debugging messages)
26  className = this->staticMetaObject.className();
27 
28  // creating the serial port object
29  serialPort = new DirecsSerial();
30 
31  // let the error messages from the direcsSerial object be transferred to the GUI
32  // (connect the signal from the interface class to the signal from this class)
33  connect(serialPort, SIGNAL(message(QString)), this, SIGNAL(message(QString)));
34 }
35 
36 
38 {
39  delete serialPort;
40 }
41 
42 
43 bool InterfaceAvr::openComPort(QString comPort)
44 {
45  // for QString to char* conversion
46  QByteArray ba = comPort.toLatin1();
47 
48 
49  // check if file (serial port) exists
50  if (QFile::exists(comPort) == false)
51  {
52  emit message(QString("<font color=\"#FF0000\">ERROR: %1 not found!</font>").arg(comPort));
53  // this tells other classes that the robot is OFF!
54  emit robotState(false);
55 
56  return false;
57  }
58 
59 
60  // serial port config and flush also done in openAtmelPort!
61  if (serialPort->openPort( ba.data(), BAUDRATE ) == -1)
62  {
63  // this tells other classes that the robot is OFF!
64  emit robotState(false);
65  return false;
66  }
67 
68  return true;
69 }
70 
71 
73 {
74  // using direcsSerial
76 }
77 
78 
79 bool InterfaceAvr::sendChar(unsigned char character, QString callingClassName)
80 {
81  int result = 0;
82 // static int receiveErrorCounter = 0;
83 
84 
85  // send one byte to the serial port with direcsSerial
86  //emit emitMessage( QString("Sending '%1'.").arg(character) ); // this makes the program to slow and than to crash!!
87  result = serialPort->writeData(&character, callingClassName);
88 
89  if (result < 0)
90  {
91 // receiveErrorCounter++;
92  emit message( QString("<font color=\"#FF0000\">ERROR '%1' (InterfaceAvr::sendChar)!<font>").arg(strerror(result)) );
93 
94 // // MASSIVE COMMUNICATION ERROR!
95 // if (receiveErrorCounter >= 4)
96 // {
97 // receiveErrorCounter = 0;
98  emit tooMuchErrors();
99 // }
100  return false;
101  }
102 
103  return true;
104 }
105 
106 
107 bool InterfaceAvr::receiveChar(unsigned char *character, QString callingClassName)
108 {
109  int result = 0;
110 
111 
112  // reading one char with direcsSerial
113  // Must return 1 (1 character succussfull read)!
114  result = serialPort->readData(character, 1, callingClassName);
115 
116  if (result != 1)
117  {
118  // ERROR
119  // emit message( QString("<font color=\"#FF0000\">ERROR '%1' (InterfaceAvr::receiveChar)!<font>").arg(strerror(result)) ); < error message already emitted from readAtmelPort!
120  return false;
121  }
122 
123  // emit emitMessage( QString("Received '%1'.").arg(result) ); // this makes the program to slow and than to crash!!
124  return true;
125 }
126 
127 
128 bool InterfaceAvr::sendString(QString string, QString callingClassName)
129 {
130 // QString debugstring;
131 
132 
133  // send starter
134  if (sendChar(starter, callingClassName) == true)
135  {
136  // send 'content' of string
137 // debugstring = "*";
138  for (int i=0; i<string.length(); i++)
139  {
140  // char by char
141  if (sendChar(string.at(i).toAscii(), callingClassName) == false)
142  {
143  return false;
144  }
145 // debugstring.append(string.at(i));
146  }
147 
148  // send terminator
149  if (sendChar(terminator, callingClassName) == true)
150  {
151  // success
152 // debugstring.append("#");
153 // emit message(debugstring);
154  return true;
155  }
156  }
157 
158  return false;
159 }
160 
161 
162 bool InterfaceAvr::receiveString(QString &string, QString callingClassName)
163 {
164  int result = 0;
165  unsigned char character;
166  QByteArray ba;
167 
168 
169  do
170  {
171  // reading one char. Must return 1 (one character succussfull read).
172  result = serialPort->readData(&character, 1, callingClassName);
173 
174  if (result == 1)
175  {
176  // append received char to byte array
177  ba.append(character);
178  }
179 
180  } while ( (result == 1) && (character != '#') );
181 
182  if (result != 1)
183  {
184  // ERROR (error message already emitted from readAtmelPort!)
185  qDebug() << "error at receiveString called from" << callingClassName;
186  return false;
187  }
188 
189  // copy chars to QString to pointer to return the QString
190  string = QString::fromUtf8(ba.data(), ba.length());
191 
192  // check result!
193  if ((string.startsWith(starter)) && (string.endsWith(terminator)))
194  {
195  return true;
196  }
197 
198 
199  return false;
200 }
201 
202 
203 bool InterfaceAvr::receiveInt(int *value, QString callingClassName)
204 {
205 // static int receiveErrorCounter = 0;
206  unsigned char character = 0;
207  int intValue = 0;
208 
209 
210  //-----------------
211  // receive MS-Byte
212  //-----------------
213  if (receiveChar(&character, callingClassName) == false)
214  {
215 // receiveErrorCounter++;
216  // emit error message already in calling receiveChar!
217 
218  //
219  // MASSIVE COMMUNICATION ERROR!
220  //
221 // if (receiveErrorCounter >= 4)
222 // {
223 // receiveErrorCounter = 0;
224 // emit tooMuchErrors();
225 // }
226 
227  value = 0;
228  return false;
229  }
230 
231  // reset error counter
232 // receiveErrorCounter = 0;
233 
234 
235  // bit shifting
236  intValue = (character << 8);
237 
238 
239  //-----------------
240  // receive LS-Byte
241  //-----------------
242  if (receiveChar(&character, callingClassName) == false)
243  {
244  // emit error message already in calling receiveChar!
245  value = 0;
246  return false;
247  }
248 
249 
250  // build the int value
251  // (add the LS-Byte to the MS-Byte)
252  *value = (intValue + character);
253 
254  // emit emitMessage( QString("Received int '%1'.").arg(*value) ); // this makes the program to slow and than to crash!!
255  return true;
256 }
257 
258 
259 bool InterfaceAvr::convertStringToInt(QString string, int &value)
260 {
261  bool conversion = false;
262 
263 
264  // remove starter
265  string = string.remove(starter);
266  // remove terminator
267  string = string.remove(terminator);
268 
269  // convert to int
270  value = string.toInt(&conversion);
271 
272  if (conversion)
273  {
274  return true;
275  }
276 
277  value = 0;
278  return false;
279 }