trouble using Serial.parseInt

Status
Not open for further replies.

gony

Well-known member
hello !
i have some issue using Serial.parseInt . i tried to track the problem myself and noticed that if i write a simple code that has Serial.parseInt in it than everything works fine . problem arise when i use my specified code :

Code:
#define SIMPLEFIFO_LARGE

#include <SimpleFIFO.h>

// PTA (12 13) = [3 4] // ready signal and XS2
// PTB (0 1 2 3) = [16 17 19 18] // For address bits
// PTC (0 1 2 3 4 5 6 7 8 9 10 11) = [15 22 23 9 10 13 11 12 35 36 37 38] // For digital inputs from NI cards
// PTD (0 1 2 3 4 5 6 7 15) = [2 14 7 8 6 20 21 5 54] //for digital output to plexon and one strobe signal
// PTE (25) = [34] // manual reward

byte inputPins[] = {3, 4, 16, 17, 19, 18, 15, 22, 23, 9, 10, 13, 11, 12, 35, 36, 37, 38};
byte outputPins[] = {2, 14, 7, 8, 6, 20, 21, 5, 54, 34};
int Dready = 3 ;// data ready pin number
int isCharOn = 0; //set to 1 when fifo bit was sent , to 0 when port cleared
int rewardON = 25;
int er = 0;
elapsedMicros charTime;
elapsedMillis rewardTime;
unsigned int onDur = 25;
unsigned long tPeriod = 20000;
unsigned long timeOut = 100;
IntervalTimer myTimerOne;
IntervalTimer myTimerTwo;
IntervalTimer myTimerThree;

SimpleFIFO <long, 255> sFIFO;

void strobe(void) {
  if (!digitalReadFast(Dready)&& GPIOB_PDIR == 7) {

    sFIFO.enqueue(GPIOC_PDIR & 0xFF);
  }
}

void charOut() {
  if (sFIFO.count() > 0){
    if (GPIOD_PDOR == 0x00) {
      GPIOD_PDOR = sFIFO.dequeue()|0x8000;
      isCharOn=1;
      charTime = 0;
    }
  }
  else {
    sFIFO.flush();
  }
}

void ManualReward(void) {
  if (Serial.available()) {
    er = Serial.parseInt();
    Serial.println(er);
    //rewardON = Serial.parseInt();
    //int rewardON = Serial.parseInt();
    //Serial.println(rewardON);
    GPIOE_PDOR= 0x2000000;
    rewardTime = 0;//start counting until stop
  }
}

void setup() {
  Serial.begin(9600);
  while(!Serial);
  for (int pinnum = 0; pinnum < 18; pinnum++) {
     //pinMode(inputPins[pinnum], INPUT_PULLDOWN);
     pinMode(inputPins[pinnum], INPUT);
  }
  for (int pinnum = 0; pinnum < 10; pinnum++) {
     pinMode(outputPins[pinnum], OUTPUT);
  }
  myTimerOne.priority(0);
  myTimerOne.begin(strobe,1);
  myTimerTwo.priority(1);
  myTimerTwo.begin(charOut,100000);
  myTimerThree.priority(16);
  myTimerThree.begin(ManualReward,10000);
  sFIFO.flush();
  
}

void loop() {
  if (isCharOn == 1 && charTime > onDur) {
    //Serial.print(char(GPIOD_PDOR & 0xFF));
    GPIOD_PDOR = 0x00;
    isCharOn = 0;
    charOut();
  }
  if ( GPIOE_PDOR>>25 && rewardTime > rewardON) {//if the reward output is being sent for the input time
    //digitalWriteFast(34,LOW);
    GPIOE_PDOR = 0x0;//stop reward
      }

}

look specifically atthe function Manualreward **
i upload the program and everything is working . than i send something through the Serial and it responds as i wish - but - than after a few seconds delay , the computer gets stuck and the monitor shows offline. than i receive this message on the error screen" Error while setting serial port parameters: 9,600 N 8 1 " it prints this message repeatedly until i reboot.
 
Last edited:
Maybe it would help to see what the parseint is trying to parse. That is if your host program only sends a number like "15" so that the code only sees the 1 and the 5, it is going to wait for a nun numeric value to be sent. Since I don't see any timeouts specified, it will hang in the parse function for the default timeout which is 1 second... Which then you call this function again...

But again only guessing
 
Status
Not open for further replies.
Back
Top