Having serial monitor open keeps python from freezing


i've built program functions oscilloscope , function generator. receives waveform (string of 256 values between 0 , 255, delimited commas) python on serial, outputs on d0 d7. user can send new waveform clicking button on python end. @ same time, reads voltage values @ a0, stores them buffer, once per second transmits these values python on serial connection.

everything works when program starts, after sending few new waveforms python, gets stuck on readfromserial() function. however, if open serial monitor, clears , goes working normally. monitor open, user can send many new waveforms without problem.

when sendstuff() function removed, works without problems, assume there's conflict between sending , receiving of data on serial.

can explain me why , how fix it?

code: [select]

static byte table[256]; //this hold values transmitted python

int i;
byte value;
float n = 256;
float f_clk = 54000;


static float freq;
int amp;
double place;
double m;

int datalen;
char startword;

char bufflen[4]; //stores datalen information
char buff[1000]; //larger need can null term end it
char buff2[10]; //intermediate buffer

const unsigned int numvalues = 200;
byte readvalues[numvalues];
int counter;
int sendflag;


void setup() {
  serial.begin(115200);

  for(i=0;i<9;i++){
    pinmode(i, output);
  }
  pinmode(13, output);

  place = 0;
 
  freq = 100; //default values
 
  amp=2.0;
  m = (freq*n)/f_clk;
  datalen = 767; //default; length of square wave set
 
  counter = 0;
  sendflag = 0;
}


void readfromserial() { //reads identifier @ start of incoming data , directs right function

    startword = serial.read();
   // serial.print("startword: "); //for testing purposes
   // serial.println(startword);
   
    if(startword == 't') {
      gettable();
    }
   else {
    while(serial.available()) { //use incoming string can try again next time
      char t = serial.read();
    }
   }   
  }

void gettable() { //parses incoming dataset
  for(int i=0;i<256;i++) {
    table[i] = 0; //clear table make room new values
  }
  serial.println("getting table!"); 

  char bufflen[4];
  serial.readbytes(bufflen, 3); //first, find length of incoming data
  bufflen[3] = '\0'; //null terminate
  datalen = atoi(bufflen);
  serial.readbytes(buff, datalen); //read right number of bytes
  buff[datalen] = '\0'; //null terminate
  int k = 0;
  int i=0;
  while(i<datalen){
    int j = 0;
    while(buff[i]!=44 && buff[i] != '\0') { //while we're on numbers (incoming format 1,99,100,101,102, etc)
      buff2[j]=buff[i];
      i++;
      j++;
    } //now we're @ comma, want ignore
    buff2[j] = '\0'; //null term intermediate buff
    table[k] = atoi(buff2); //copy intermediate buff table
    k++;
    i++;
  }
}


void makewavebyplace() { //output 1 value table, advance placeholder, return
  = (int)place;
  portd = table[i];
  place = place + m;
  if(place > 255) {
    place = place - 255;
  }
}


void oscbyparts() { //this collects values we'll sending python later
  readvalues[counter] = analogread(a0)/4; //get value
  counter++;
  if(counter>=numvalues) {
    sendflag = 1; //stop collecting data
    counter = 0; //reset counter
  }
 
}

void sendstuff() { //this sends values on serial python
  portd = 0; //silence fungen while sending
  for(int i=0;i<numvalues;i++) {
    serial.println(readvalues[i]);
  }
  serial.println("..");
  serial.flush();
  //sendflag = 0;
}


void loop() {
  long starttime = millis();
  while((millis()-starttime)<1000) { //do second
    if(serial.available()) { //if we're getting new data in
      readfromserial();
    }
 
    makewavebyplace();
 
    if(sendflag == 0) {
      oscbyparts(); //gathering data sent
    }
  }
  if(serial.available()<=0) { //trying (unsuccessfully) force not interfere incoming data
   
    sendstuff(); //send data gathered python, once every second
  }   
}

code: [select]
serial.begin(115200);

  for(i=0;i<9;i++){
    pinmode(i, output);
  }
can see problem here?


Arduino Forum > Using Arduino > Interfacing w/ Software on the Computer > Having serial monitor open keeps python from freezing


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts