Browse Source

Negative value handling and improved robustness

Neal Wilson 11 years ago
parent
commit
e096312b20
1 changed files with 11 additions and 7 deletions
  1. 11
    7
      src/scales.w

+ 11
- 7
src/scales.w View File

92
 #include "scale.h"
92
 #include "scale.h"
93
 #include <QStringList>
93
 #include <QStringList>
94
 
94
 
95
-
96
 SerialScale::SerialScale(const QString &port) :
95
 SerialScale::SerialScale(const QString &port) :
97
 	QextSerialPort(port, QextSerialPort::EventDriven)
96
 	QextSerialPort(port, QextSerialPort::EventDriven)
98
 {
97
 {
113
 	responseBuffer.append(readAll());
112
 	responseBuffer.append(readAll());
114
 	if(responseBuffer.contains("\x0D"))
113
 	if(responseBuffer.contains("\x0D"))
115
 	{
114
 	{
116
-		if(responseBuffer.startsWith("!"))
115
+		if(responseBuffer.contains("!"))
117
 		{
116
 		{
118
 			responseBuffer.clear();
117
 			responseBuffer.clear();
119
-			weigh();
120
 		}
118
 		}
121
 		else
119
 		else
122
 		{
120
 		{
126
 	}
124
 	}
127
 }
125
 }
128
 
126
 
129
-@ Each line of data consists of characters representing a number followed by a
127
+@ Each line of data consists of an optional sign character possibly followed
128
+by a space followed by characters representing a number followed by a
130
 space followed by characters indicating a unit. This may be preceeded and
129
 space followed by characters indicating a unit. This may be preceeded and
131
 followed by a variable amount of white space. To process a new measurement, we
130
 followed by a variable amount of white space. To process a new measurement, we
132
-must remove the excess white space, split the number from the unit, convert the
133
-string representing the number to a numeric type, and determine which unit the
134
-measurement is in.
131
+must remove the excess white space, split the number from the unit, prepend the
132
+sign to the number if it is present, convert the string representing the number
133
+to a numeric type, and determine which unit the measurement is in.
135
 
134
 
136
 \medskip
135
 \medskip
137
 
136
 
149
 
148
 
150
 @<Process weight measurement@>=
149
 @<Process weight measurement@>=
151
 QStringList responseParts = QString(responseBuffer.simplified()).split(' ');
150
 QStringList responseParts = QString(responseBuffer.simplified()).split(' ');
151
+if(responseParts.size() > 2)
152
+{
153
+	responseParts.removeFirst();
154
+	responseParts.replace(0, QString("-%1").arg(responseParts[0]));
155
+}
152
 double weight = responseParts[0].toDouble();
156
 double weight = responseParts[0].toDouble();
153
 Units::Unit unit = Units::Unitless;
157
 Units::Unit unit = Units::Unitless;
154
 if(responseParts[1] == "lb")
158
 if(responseParts[1] == "lb")

Loading…
Cancel
Save