Serial comm problems.

Status
Not open for further replies.

barhirsh

New member
Hello everyone.
I'm doing a certain project working with TEENSY 4.0.
I make a current pulse. And with the help of the controller I control the current with a simple pid.
At the end of each pulse. I send the samples made by the controller to the computer via serial communication (Serial.println ()).
During the pulse I save the current samples (25 k samples) in an array of floats. And at the end of the teensy pulse sends the whole array in one blow.
Every time I try to send the entries I stop getting information from teensy (can still send information to him and he responds to the information (not in serial back))
To "fix" the problem I disconnect and connect the USB cable from the computer and everything works out until the next pulse.
Yesterday, after an hour of disconnections and connections. The teensy stopped communicating completely with the computer. The computer no longer recognizes it as a serial device and also any attempt I tried with the program button did not help.

The code snippet of the serial sending.
Code:
 /////////////////////////////////////////////////////////////END OF PULSE///////////////////////////////////////////
  digitalWrite(outgate1, LOW);
  analogWrite(outgate2, LOW);
  arraypulsestop=arrsize;
  trigertime = millis() - starttime;
  while (trigertime < pulsewitdhval+5) {
      trigertime = millis() - starttime;
      RunAvg ( analogCurrentIn, SizeOfRead, totalCur, readIndexCur,averageCur, inputCurARR);
      realcurrent=(((((float)(averageCur)*(3.3/4096.0))/(Ivoltdiv)))-Idcfix);
      TimeSand[arrsize]=trigertime;
      CurrentSand[arrsize]=realcurrent;
      arrsize++;
      //Serial.println(realcurrent);
      
  }
  if(arrsize!=0){
    /*Serial.println('t');
    for(int y=0;y<arrsize;y++){
       Serial.println(TimeSand[y]);
    }
    Serial.println('T');
    delay(100);
    Serial.println('c');*/
    for(int y=0;y<arrsize;y++){
       if(maxcur<=CurrentSand[y]){
        maxcur=CurrentSand[y];
       }
       if(y>arraypulsestart && y<arraypulsestop){
        if(mincur>=CurrentSand[y]){
          mincur=CurrentSand[y];
        }
        curavg+=double(CurrentSand[y]/(arraypulsestop-arraypulsestart));
       }
       Serial.println(CurrentSand[y]);
      }
   // Serial.println('C');
      Serial.print("Max current = ");
      Serial.println(maxcur);
      Serial.print("Min current = ");
      Serial.println(mincur);
      Serial.print("Average current= ");
      Serial.println(curavg);
      Serial.print("Ripple = ");
      Serial.println(maxcur-mincur);
      Serial.print("Ripple % = ");
      Serial.println(float((maxcur-mincur)*100/currentval));
      Serial.print("Cap Voltage = ");
      Serial.println(capacitorVoltCalcF);
      Serial.print("Resistance");
      Serial.println((double)capacitorVoltCalcF/maxcur,4);
  }
  delay(500);
  TIK=millis();
  stopwatch.Reset();
 
T_4.0 can send data faster than the computer USB can keep up with and handle when unrestrained.

... code presented just is a snippet - assuming TimeSand[] is 4 byte variable? and this "arrsize" is the 25 K sample count?

try:
Code:
    for(int y=0;y<arrsize;y++){
       Serial.println(TimeSand[y]);
       if ( 0==y%128 ) {
         Serial.flush()
         delay(5);
      }
    }
 
I have a similar problem.
I use a Mac with MacOS BigSur 11.2.2.
I am running Teensyduino 1.5.3 and the Teensy Loader 1.5.3.
I am using Teensy 4.1.
I am using Wireshark to monitor the USB.
The serial data is sent and received from CoolTerm 1.7.0
I am using an Apple original USB-C dongle to hock up the Teensy 4.1.

The sketch I am using looks as below. The Teensy receives a text string with a terminating "#", for example "abcdef#". No CR or LF at the end preferable.
The Teensy returns some text and the string and changes the status of the LED on the board.

My finding are:
1 - Using the LED on the Teensy I can see that the string is always received by the Teensy. With Wireshark you can also see that the text is sent.
2 - Using Wireshark I can see that the Mac always is receiving the reply.
3 - Sometimes the CoolTerm does not receive the reply.
4 - I can repeat this error by compiling the sketch and download it (automatically by the Teensy Loader).
5 - I can fix it by
a - closing the Teensy Loader
b - disconnect the usb
c - reconnect the usb

I have also removed all modems in SystemPreferences - Network. That made a short difference, but did not solve the problem.

My "conclusion" is that possible the Teensy Loader plays a role here.



Code:
#define USBSERIAL Serial      // Arduino Leonardo, Teensy, Fubarino
//#define USBSERIAL SerialUSB   // Arduino Due, Maple

int led = 13;  //Teensy 4.1
byte pinstate = LOW;

void readSerialCommand() {
  //Read from the USB line to see if there is any commands to read


  int numberOfCharToRead;
  if (USBSERIAL.available() >> 1) {
    //There are bytes to read
    USBSERIAL.println(USBSERIAL.available());
    String incomingString =  USBSERIAL.readStringUntil('#');//USBSERIAL.readBytes();
    USBSERIAL.print("The incoming string is: ");
    USBSERIAL.println(incomingString);
    USBSERIAL.send_now();
    //delay(10);


    // toggle pin 2, so the frequency is kbytes/sec
    if (pinstate == LOW) {
      digitalWriteFast(led, HIGH);
      pinstate = HIGH;
    } else {
      digitalWriteFast(led, LOW);
      pinstate = LOW;
    }

  }// end USBSERIAL.available


}  //end readSerialCommand


void setup() {
  // put your setup code here, to run once:
  USBSERIAL.begin(9600);
  USBSERIAL.setTimeout(0);
  pinMode(led, OUTPUT); 
}  //end setup


void loop() {
  // put your main code here, to run repeatedly:
  readSerialCommand();

}  //end loop
 
Status
Not open for further replies.
Back
Top