0

Description:

I have a code version where i only send data from Arduino to PC where i read data with PC. I want to add the possibility to send a command start or to stop the sending of data.

This code without writing commands works:

Arduino:

struct data {
  int16_t rho;
  int16_t theta;
  uint16_t phi;
};

struct data readDatastrore;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  readDatastrore.theta = random(-100, 100);
  readDatastrore.phi = random(-100, 100);
  readDatastrore.rho = random(0, 100);
  // send to serial
  Serial.write('S');
  Serial.write((uint8_t *)&readDatastrore, sizeof(data));
  Serial.write('E');
  //Serial.print(rho); Serial.print(",");
  //Serial.print(theta); Serial.print(",");
  //Serial.println(phi);
}

Python:

import serial
import struct
import pandas as pd

ser = serial.Serial('COM10', 9600, timeout = 1) # ttyACM1 for Arduino board

print ("Starting up")

columns = ['rho','tetah','phi']
df = pd.DataFrame(columns=columns)
try:
    while True:
        myByte = ser.read(1).decode('ascii')
        if myByte == 'S':
            data = ser.read(6)
            myByte = ser.read(1).decode('ascii')
            if myByte == 'E':
                new_values = struct.unpack('<hhh', data)
                a = pd.Series(list(new_values), index=columns, name="sample")
                df = df.append(a, ignore_index=True)

except KeyboardInterrupt:
    ser.flush() #flush the buffer
    print(df)
    df.to_csv( 'data_rhotethaphi.csv', sep=',', index=False)
    # df.plot(kind='scatter')

This code with writing commands to start returns nothing in any python print:

Arduino:

bool readSensor = true;
char inByte;

struct data {
  int16_t rho;
  int16_t theta;
  uint16_t phi;
};
struct data readDatastrore;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    inByte = Serial.parseInt();
    Serial.flush();
    if (inByte == "1") {
      readSensor = true;
      Serial.println(readSensor);
    }
    else if (inByte == 2) {
      readSensor = false;
      Serial.println(readSensor);
    }
  }
  if (readSensor) {
    // put your main code here, to run repeatedly:
    readDatastrore.theta = random(-100, 100);
    readDatastrore.phi = random(-100, 100);
    readDatastrore.rho = random(0, 100);
    // send to serial
    Serial.write('S');
    Serial.write((uint8_t *)&readDatastrore, sizeof(data));
    Serial.write('E');
  }
}

Python:

import serial
import struct
import pandas as pd
import time

read_command = '1'
stop_command = '2'

ser = serial.Serial('COM10', 9600, timeout = 1) # ttyACM1 for Arduino board

print ("Starting up")

columns = ['rho','tetah','phi']
df = pd.DataFrame(columns=columns)
try:
    # ser.write(struct.pack('>H', read_command))
    ser.write(read_command.encode('ascii'))
    # ser.flush()
    print(myByte)
    while True:
        myByte = ser.read(1).decode('ascii')
        print(myByte)
        if myByte == 'S':
            data = ser.read(6)
            myByte = ser.read(1).decode('ascii')
            if myByte == 'E':
                new_values = struct.unpack('<hhh', data)
                a = pd.Series(list(new_values), index=columns, name="sample")
                df = df.append(a, ignore_index=True)

except KeyboardInterrupt:
    # ser.write(struct.pack('>B', stop_command))
    ser.write(stop_command.encode('ascii'))
    ser.flush() #flush the buffer
    ser.close()
    print(df)
    df.to_csv( 'data_rhotethaphi.csv', sep=',', index=False)
    # df.plot(kind='scatter')
  • 1
    parseInt converts text to number and returns long not byte. read returns one byte (as int to be able to return -1 in no data are available). – Juraj Jun 1 at 9:46
  • 1
    you send numbers as text. 'parseInt' will convert them but it is an overkill for one digit. inByte is a number. you compare it with a pointer to string "1" in inByte == "1" – Juraj Jun 1 at 9:49
  • Ok i replaced by read_command = 1 and ser.write(read_command) . I removed flush. It's working now. – jlSta Jun 1 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.