Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: This formula blocks ONLY Serial 1

  1. #1

    This formula blocks ONLY Serial 1

    Hi,
    I have a one Teensy sending out two byte to another Teensy.
    After hours and hours of troubleshooting my project I found that Serial1 on the receiving Teensy seems to be blocked by the formula in the code below.
    I have tried with Serial4, Serial5 and Serial6, and they all receive the two bytes.

    On the receiving Teensy I have this code:

    Code:
    byte readValueFromSerial;
    bool transmissionStarted;
    byte byteToReceiveFromSerial_high;
    byte byteToReceiveFromSerial_low;
    float thisFormulaBlocksSerial1;
    float thisFormulaDoesNOTBlockSerial1;
    void setup(){
        Serial1.begin(500000); 
    }
    
    void loop(){  
      //thisFormulaDoesNOTBlockSerial1=pow(1,byteToReceiveFromSerial_high)-pow(1.06,byteToReceiveFromSerial_high);
      thisFormulaBlocksSerial1=pow(1,byteToReceiveFromSerial_high)-pow(1.06,byteToReceiveFromSerial_high);
      readSerial();
    }
    
    void readSerial(){
      if(Serial1.available()){
        readValueFromSerial=Serial1.read();
        if(transmissionStarted){
          if(!bitRead(readValueFromSerial,7)){
            byteToReceiveFromSerial_low=readValueFromSerial;
            transmissionStarted=false;
            Serial.println("Read Successful");
          }
        }else{
          if(bitRead(readValueFromSerial,7)){
            bitClear(readValueFromSerial,7);
            byteToReceiveFromSerial_high=readValueFromSerial;
            transmissionStarted=true;     
          }
        }
      }
    }
    I would be grateful for any advice on how to solve this issue.
    /Johan

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,715
    What Teensy?
    The blocking calc in loop() will be running on undefined values - and mismatched values as it runs when no values are received?

    Is loop still running? set pin 13 to OUTPUT and in loop do digitalToggle(13) to see - it may be faulting the processor with undefined calc values?

    Set a flag in readSerial to limit the formula from running when the values are unknown or only one of the two bytes has been received? That readSerial() will only read one at a time - even if two are present.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,569
    I don't see how there can be any effect. The two formulae are identical (other than the variable name on the LHS) and, in either case, the result of the calculation isn't used anywhere so the compiler is very likely to optimize the statement out.
    Is there some purpose behind the calculations?
    Code:
    pow(1,byteToReceiveFromSerial_high)
    This function call will always result in 1 no matter what the value of byteToReceiveFromSerial_high is (including zero) which reduces the expression to:
    Code:
    thisFormulaBlocksSerial1 = 1 - pow(1.06,byteToReceiveFromSerial_high);
    Pete

  4. #4
    El Supremo, I have modified my original formula that calculates MIDI note to frequency. The formula in my code example is only to demonstrate that if a float is used instead of an Int the read of Serial1 is blocked. Serial 4, 5 & 6 works perfect using either of the formulae.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,715
    Serial1 and Serial2 are backed by a FIFO and may have slightly different response behavior - but if only two bytes are involved that shouldn't matter.

    was blink added to loop() ? how is it known it blocks when it doesn't express anything outside in the sketch?

  6. #6
    Defragster, when I activate the nonblocking formula the Serial monitor displays "Read successful", if I activate the blocking formula the Serial monitor gets blank. If I switch Serial1 to either Serial4, 5 or 6, the Serial monitor displays "Read successful" no matter which of the two formulae I use. I discovered this when modulating two filter frequencies, one of left audio channel and one on right audio channel. The right filter modulated fine using input from serial4 but the left, using serial1, did not work. Both channels uses the same formula. I stripped down my code and deleted everything and it all came down to this issue.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,715
    Posted sketch as noted doesn't offer feedback to know if it is hung or other at all. No USB Serial output or LED blink. And not sure what is connecting to transmit the 'two bytes'?

    Can a self standing repro version be made and posted where Serial2.begin(500000); is added and the two test values sent where Serial2 would be cross wired to Serial1.

    If it really is not acting right then a better repro case to observe the behavior would be useful to get to a fix.

    <edit> : crosspost - The print in the readSerial() wasn't observed here for what it was.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,715
    However: Teensy starts fast and USB Serial may not be online before ejecting that message?

    In setup() add :: while(!Serial); Serial.print("Teensy Online - talk to me");

  9. #9
    Quote Originally Posted by defragster View Post
    Posted sketch as noted doesn't offer feedback to know if it is hung or other at all. No USB Serial output or LED blink. And not sure what is connecting to transmit the 'two bytes'?

    Can a self standing repro version be made and posted where Serial2.begin(500000); is added and the two test values sent where Serial2 would be cross wired to Serial1.

    If it really is not acting right then a better repro case to observe the behavior would be useful to get to a fix.

    <edit> : crosspost - The print in the readSerial() wasn't observed here for what it was.
    Hi, I posted some wrong code:

    This formula blocks Serial1:
    pow(1.06,byteToReceiveFromSerial_high)-pow(1.06,byteToReceiveFromSerial_high);

    This formula lets data through:
    pow(1,byteToReceiveFromSerial_high)-pow(1.06,byteToReceiveFromSerial_high);

    It seems to have something to do with the decimal value and the pow() function.

    I tested with sq(byteToReceiveFromSerial_high)-sq(byteToReceiveFromSerial_high).
    Worked fine.

    Click image for larger version. 

Name:	Good.jpg 
Views:	14 
Size:	142.8 KB 
ID:	22353
    Click image for larger version. 

Name:	Bad.jpg 
Views:	12 
Size:	81.4 KB 
ID:	22354
    Last edited by johanbilen; 11-07-2020 at 09:41 PM. Reason: Added images

  10. #10
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    845
    You should put a delay() after your Serial.println() statements.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •