Reading values from Excel via PLX-DAQ

I've been using PLX-DAQ as a datalogging/graphing system with my Mega 1280. Things work great when it comes to reading multiple sensor inputs on the arduino logging and plotting them in real time.

The thing is I want to be able to customize constants and sensor properties using the excel sheet and not having to modify them in the sketch and reupload. I want to be able to have the arduino with the datalogging sketch on it read sensor properties and measurement times etc from the Excel sheet.

PLX-DAQ has the functionality to send information over serial from the excel sheet. If I send CELL,GET,CELLNUM over serial, PLX-DAQ is supposed to send back the value in CELLNUM over serial.

I don't know how to properly read this data.

I'm trying the basic, putting a number in cell Z2 and then having my loop send CELL,GET,CELLNUM and check if there is data available and read in into an integer. Then send that integer out again to PLX daq to get written in the current cell.

Here's my code:

void setup() {
  Serial.begin(128000);                 // 128000 is the highest supported by both the mega 1280 and plx-daq
  Serial.println("CLEARDATA");          // every time i reset the plx-daq connection clear all data thats been place in already
}

void loop() {
  Serial.println("CELL,GET,Z2");        // send the call to get the data from cell z2
  if (Serial.available() > 0) {         // if there is serial data
    int value = Serial.read();          // read the serial data into value
    Serial.print("DATA,");              // start a data output line
    Serial.println(value);              // output vale on that line then end it
  }
  delay(1000);                          // wait 1s before repeating
}

The unfortunate thing is I can't use another serial monitor while im using PLX-DAQ so all I can see is that the numbers reading out are not right. When I put 10 into cell Z2 they read 49, 48, -1, 13, 50 etc...

I'm just totally a scrub to serial communication, can anyone help?

49 is the ascii code for "1"
48 is the ascii code for "0"
13 is the ascii code for a "\n"
-1 is probably a read error

so you are closer than you think. the only thing you need to do is to read chars until you read 13.

in short

  1. flush serial in
  2. request cell
  3. read and combine until a CR

code snippet

c = Serial.read();
if (isdigit(c)) value = value * 10 + c - '0';

robtillaart:
49 is the ascii code for "1"
48 is the ascii code for "0"
13 is the ascii code for a "\n"
-1 is probably a read error

so you are closer than you think. the only thing you need to do is to read chars until you read 13.

in short

  1. flush serial in
  2. request cell
  3. read and combine until a CR

Could you give me some advice on how I flush the serial in and continuously reading?

My final objective is to in setup read a bunch of parameters from the excel sheet and store them in variable s to be used when reading the sensor.

flush:
while (Serial.available() > 0) Serial.read();

something like this - not tested

//
//    FILE: .ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: 
//    DATE: 
//     URL:
//
// Released to the public domain
//


void setup() 
{
  Serial.begin(128000);                 // 128000 is the highest supported by both the mega 1280 and plx-daq
  Serial.println("CLEARDATA");          // every time i reset the plx-daq connection clear all data thats been place in already
}

void loop() 
{
  // FLUSH
  while (Serial.available() > 0) 
  {
    Serial.read();
    delay(1);
  }
  
  // REQUEST VALUE
  Serial.println("CELL,GET,Z2");        // send the call to get the data from cell z2
  delay(100); // give some time to response
  
  // READ RESULT
  int value = 0;
  while(Serial.available() > 0) 
  {
    c = Serial.read();
    if (isdigit(c) && c != '\n') 
    {
      value = value * 10 + c - '\0';
    }
    delay(1);
  }
  Serial.print("DATA,"); 
  Serial.println(value);
  delay(1000);
}

Thanks a lot, that makes a lot of sense.

The only issue is that I'm not sure what datatype to give "c" in this case. I tried it with both int and char and i now get a return of 538 which means I'm Getting the 49, multiplying by 10, then adding the 48, then reading out.

I need to obtain the actual value of the digit coming in and then do that add.

This is where I'm at but im still getting the 538. I thought hey, if im gonna be reading a digit in and all digits count up in ascii from 48, just take the ascii value and subtract 48 to get the digit value. But that isn't working and I'm not sure why.

EDIT: Figured it out, was still adding readValue not charValue. Now to figure out non-integer values

#include <ctype.h>

void setup() {
  Serial.begin(128000);
  Serial.println("CLEARDATA");
}

void loop() 
{
  // FLUSH
  while (Serial.available() > 0) 
  {
    Serial.read();
    delay(1);
  }
  
  // REQUEST VALUE
  Serial.println("CELL,GET,Z2");  // send the call to get the data from cell z2
  delay(100);                     // give some time to respond
  
  // READ RESULT
  int cellValue = 0;
  int charValue = 0;
  while(Serial.available() > 0) 
  {
    int readValue = Serial.read();
    if (isdigit(readValue) && readValue != '\n') 
    {
      charValue = readValue - 48;
      cellValue = cellValue * 10 + readValue;
    }
    delay(1);
  }
  Serial.print("DATA,0,"); 
  Serial.println(cellValue);
  delay(1000);
}

readValue can be char

for your info char(48) == '0'

#include <ctype.h>

void setup() {
  Serial.begin(128000);
  Serial.println("CLEARDATA");
}

void loop() 
{
  // FLUSH
  while (Serial.available() > 0) 
  {
    Serial.read();
    delay(1);
  }
  
  // REQUEST VALUE
  Serial.println("CELL,GET,Z2");  // send the call to get the data from cell z2
  delay(100);                     // give some time to respond
  
  // READ RESULT
  int cellValue = 0;
  while(Serial.available() > 0) 
  {
    char readValue = Serial.read();
    if (isdigit(readValue) && readValue != '\n') 
    {
      cellValue = cellValue * 10 + readValue - '0';
    }
    delay(1);
  }
  Serial.print("DATA,0,"); 
  Serial.println(cellValue);
  delay(1000);
}

Alright, I've got integer numbers reading in and I can work with them no problem. The next thing I need to tackle is non-integer numbers (with decimals).

Here was my plan of attack: Look for the decimal point, then start a place counter.

The decimal point sets the place counter as 1. Then the next number read in gets multiplied by 0.1 before it is added to cellValue. Then the place counter moves up to 2, the next number read is multiplied by 0.1^2 and from there it continues. The nth number read after the decimal point is multiplied by 0.1^n before being added.

So that's what I'm doing in this code, adding the reads to the current value like before but now just diving by 10 each time rather than multiplying really.

void loop() {
  // FLUSH
  while (Serial.available() > 0) {
    Serial.read();
    delay(1);
  }
  
  // REQUEST VALUE
  Serial.println("CELL,GET,Z2");  // send the call to get the data from cell z2
  delay(500);                     // give some time to respond
  
  // READ RESULT
  float cellValue = 0;
  float charValue = 0;
  int placeCounter = 0;
  while(Serial.available() > 0) {
    char readValue = Serial.read();
    if (readValue = '.') {
      placeCounter = placeCounter +1;
    }
    else if (isdigit(readValue) && readValue != '\n') {
        if (placeCounter != 0) {
        charValue = readValue - '0';
        charValue = charValue * pow(0.1, placeCounter);
        cellValue = cellValue + charValue;
        placeCounter = placeCounter +1;
      }  
      else if (placeCounter == 0) {
        charValue = readValue - '0';
        cellValue = cellValue * 10 + charValue;
      }
    }
    delay(1);
  }
  Serial.print("DATA,0,"); 
  Serial.println(cellValue);
  delay(1000);
}

I'm now just returning 0's and I can't figure out why. I know its not a datatype issue as I can run the other code just fine working with the floats. Any ideas anyone?

    if (readValue = '.') {

WRONG!!!

    else if (isdigit(readValue) && readValue != '\n') {

If the character is a digit and is not a carriage return... How could a character possibly be a digit AND a carriage return.

It's really a good thing that the people why provide Serial.parseInt() and Serial.parseFloat() have more of a clue.

Well that was really constructive of you... wow.

handsupdb:
Well that was really constructive of you... wow.

Your welcome. I hope that you at least learned that there are already functions that do what you want, and do them correctly.

Yeah, well you coulda been a little less of a dick about it.

I searched and couldn't find that function so resorted to me own.

Yeah, well you coulda been a little less of a dick about it.

Not me.