Unable to remove error message

Hi Arduino community!
I need to send the sensor data to python using UNO board and serial port, based on the following code:

unsigned long timeOfLastDAQ; 
const int daqDelay;
typedef struct Data {
unsigned long time;
float voltage;
};
  
 
void setup() {
  
Serial.begin(115200);
delay(3000); 
}


void loop() {
  Data data;
  if (Serial.available() > 0) {
  if (micros() - timeOfLastDAQ >= daqDelay) {
  int sensorValue = analogRead(voltagePin);
unsigned long timeMicroSec = micros();
data.voltage = sensorValue * (5.0 / 1023.0);
data.time = timeMicroSec;
  if (Serial.availableForWrite()) {
    //(byte*)&Data, sizeof(Data)
    Serial.write(&Data, sizeof(Data));
        timeOfLastDAQ = data.time;
    }
  }
  
 }
}

But I keep getting the following error.
In function 'void loop()':
C:\Users\Zohreh\Documents\Arduino\AnalogReadSerialByteVoltTime_1\AnalogReadSerialByteVoltTime_1.ino:28:23: error: expected primary-expression before ',' token
Serial.write(&Data, sizeof(Data));

Even this way:

const int voltagePin = A0;
unsigned long timeOfLastDAQ; 
const int daqDelay;
typedef struct Data {
unsigned long time;
float voltage;
};
 
Data readVoltage() {
Data data;
int sensorValue = analogRead(voltagePin);
unsigned long timeMicroSec = micros();
data.voltage = sensorValue * (5.0 / 1023.0);
data.time = timeMicroSec;
  // Write the result, volt and time
  if (Serial.availableForWrite()) {
    Serial.write((byte*)&Data, sizeof(Data));
  
  }
   return data;
}

void setup() {
  
Serial.begin(115200);
delay(3000); 
}


void loop() {
  if (Serial.available() > 0) {
  if (micros() - timeOfLastDAQ >= daqDelay) {
  Data  p = readVoltage();
        timeOfLastDAQ = p.time;
    }
  
  }
}

I need to stream data just as byte.
May I have your point?

Hi @ocien. It will not be possible for us to help you with that problem based on such a vague description. Please provide a detailed description of what you mean by "unable to remove error message" in a reply on this forum thread, including:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?

Make sure to include the full and exact text of any error or warning messages you might have encountered.

Thanks for using code tags in your first post. It did unfortunately not quite work out because const int voltagePin = A0 disappeared. Code tags are three back ticks (```) before and after the code; they need to be on their own line.

Hi @ptillisch As I edited the post, It is necessary to stream byte data to python from UNO Arduino using serial port.

@sterretje Many thanks.

const int voltagePin = A0;
unsigned long timeOfLastDAQ; 
const int daqDelay;
typedef struct Data {
unsigned long time;
float voltage;
};
  
 
void setup() {
  
Serial.begin(115200);
delay(3000); 
}


void loop() {
  Data data;
  if (Serial.available() > 0) {
  if (micros() - timeOfLastDAQ >= daqDelay) {
  int sensorValue = analogRead(voltagePin);
unsigned long timeMicroSec = micros();
data.voltage = sensorValue * (5.0 / 1023.0);
data.time = timeMicroSec;
  if (Serial.availableForWrite()) {
    //(byte*)&Data, sizeof(Data)
    Serial.write(&Data, sizeof(Data));
        timeOfLastDAQ = data.time;
    }
  }
  
 }
}

The correction:
const int voltagePin = A0
is added.

Oh I found the problem:
In line 18 the following should be exchanged :
Serial.write((byte*)&Data, sizeof(Data));
By:
Serial.write((byte*)&data, sizeof(data));

seems to be a too quick copy & paste. There is no difference between the to lines of code

ah ! as ec2021 posted lower-case "d" instead of capital "D"

Hi @StefanL38 ,

there is a difference:

Data versus data

Obviously you had switched off your "case sensitivity" :wink:

ec2021

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.