Hi,
I have data coming in via soft serial. I want to extract text and float data from it. It is from an A&D FX-300i scale, via an RS232 to TTL module. I have worked out how to capture the data and send it to the serial monitor.
Here is the data format:
ST,+00016.64 GN
The relevant page from the manual is as follows. Note that I am receiving the data in units of GN (grains), whereas the example below is in grams, so the decimal point is in a different position.
Firstly, what approach should I use? Should I pick up each character as I read it, or read the whole data string first, then parse it (using something like sscanf(), which does not work for float variables in Arduino, so.....not sscanf)?
I could put the first to letters into a char variable, put the numbers into a char variable then use atof(), then put the last two characters into a char variable.
Here is my code so far:
/*
Author : David R
*/
#include <SoftwareSerial.h>
// Sets pins as software serial ports
const int rxPin = 5;
const int txPin = 6;
SoftwareSerial mySerial(rxPin, txPin); // RX, TX.
// Pin definition for motor driver
const int AIA = 9; // (pwm) pin A-IA on motor driver
const int AIB = 10; // (pwm) pin A-IB on motor driver
//Variables to control the motor turning on and off
int timeOn = 100; //amount of time the motor stays on
int timeOff = 100;
int motorState = LOW;
//Variable to store the incoming data from the A&D
const byte numChars = 17;
char dataAnD[numChars]; // an array to store the received data
boolean newData = false;
//Variables for weight sent by scale
float scaleReading;
char Steady[2];
char units[4];
//Variables for BlinkWithoutDelay
const int ledPin = LED_BUILTIN;// the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long blinkInterval = 1000; // interval at which to blink (milliseconds)
void setup()
{
Serial.begin(19200); // set the data rate for the serial monitor
mySerial.begin(19200); // set the data rate for the SoftwareSerial port
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(AIA, OUTPUT);
pinMode(AIB, OUTPUT);
// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
delay(200); //Stops a line of question marks appearing when it first starts reading data
}
void loop()
{
recvFixedLength();
vibrate(100);
}
void recvFixedLength() {
byte Count = 1;
if (mySerial.available() >= numChars) {
while (Count <= numChars) {
dataAnD[Count] = mySerial.read();
Serial.print(dataAnD[Count]); //Debugging step
Count++;
}
}
}
void vibrate(int speedVibe)
{
unsigned long currentMillis = millis();
// if the motor is off turn it on and vice-versa:
if (motorState == LOW) {
if (currentMillis - previousMillis >= timeOff) {
motorState = HIGH;
analogWrite(AIA, 0); // Tell motor to rotate
analogWrite(AIB, speedVibe);
previousMillis = millis();
}
}
else {
if (currentMillis - previousMillis >= timeOn) {
motorState = LOW;
analogWrite(AIA, 0); // Tell motor to stop
analogWrite(AIB, 0);
previousMillis = millis();
}
}
