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,7 +92,6 @@ a signal to buffer data..
92 92
 #include "scale.h"
93 93
 #include <QStringList>
94 94
 
95
-
96 95
 SerialScale::SerialScale(const QString &port) :
97 96
 	QextSerialPort(port, QextSerialPort::EventDriven)
98 97
 {
@@ -113,10 +112,9 @@ void SerialScale::dataAvailable()
113 112
 	responseBuffer.append(readAll());
114 113
 	if(responseBuffer.contains("\x0D"))
115 114
 	{
116
-		if(responseBuffer.startsWith("!"))
115
+		if(responseBuffer.contains("!"))
117 116
 		{
118 117
 			responseBuffer.clear();
119
-			weigh();
120 118
 		}
121 119
 		else
122 120
 		{
@@ -126,12 +124,13 @@ void SerialScale::dataAvailable()
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 129
 space followed by characters indicating a unit. This may be preceeded and
131 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 135
 \medskip
137 136
 
@@ -149,6 +148,11 @@ measurement is in.
149 148
 
150 149
 @<Process weight measurement@>=
151 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 156
 double weight = responseParts[0].toDouble();
153 157
 Units::Unit unit = Units::Unitless;
154 158
 if(responseParts[1] == "lb")

Loading…
Cancel
Save