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?
//
// 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);
}
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);
}
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?