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

Thread: Teensy freeze when stream from Serial port [ ubuntu18.04 teensy3.6 teensyduino1.53]

  1. #1

    Teensy freeze when stream from Serial port [ ubuntu18.04 teensy3.6 teensyduino1.53]

    I use Teensy 3.6 on Arduino 1.8.13, Teensyduino 1.53, Ubuntu 18.04 when I stream data from Serial port Teensy freeze

    mycode.ino


    #include <DHT.h>


    #define DHTPIN 24

    #define DHTTYPE DHT22 // DHT 22 (AM2302)



    DHT dht(DHTPIN, DHTTYPE);


    void setup() {

    Serial.begin(9600);
    dht.begin();

    }



    void loop() {


    float h = dht.readHumidity();
    float t = dht.readTemperature();
    Serial.println(t);
    Serial.println(h);
    delay(1000);



    }

    output


    49-teensy.rules

    # UDEV Rules for Teensy boards, http://www.pjrc.com/teensy/
    #
    # The latest version of this file may be found at:
    # http://www.pjrc.com/teensy/49-teensy.rules
    #
    # This file must be placed at:
    #
    # /etc/udev/rules.d/49-teensy.rules (preferred location)
    # or
    # /lib/udev/rules.d/49-teensy.rules (req'd on some broken systems)
    #
    # To install, type this command in a terminal:
    # sudo cp 49-teensy.rules /etc/udev/rules.d/49-teensy.rules
    #
    # After this file is installed, physically unplug and reconnect Teensy.
    #
    ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789B]?", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
    ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789A]?", ENV{MTP_NO_PROBE}="1"
    SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789ABCD]?", MODE:="0666"
    KERNEL=="ttyACM*", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789B]?", MODE:="0666"
    #
    # If you share your linux system with other users, or just don't like the
    # idea of write permission for everybody, you can replace MODE:="0666" with
    # OWNER:="yourusername" to create the device owned by you, or with
    # GROUP:="somegroupname" and mange access using standard unix groups.
    #
    # ModemManager tends to interfere with USB Serial devices like Teensy.
    # Problems manifest as the Arduino Serial Monitor missing some incoming
    # data, and "Unable to open /dev/ttyACM0 for reboot request" when
    # uploading. If you experience these problems, disable or remove
    # ModemManager from your system. If you must use a modem, perhaps
    # try disabling the "MM_FILTER_RULE_TTY_ACM_INTERFACE" ModemManager
    # rule. Changing ModemManager's filter policy from "strict" to "default"
    # may also help. But if you don't use a modem, completely removing
    # the troublesome ModemManager is the most effective solution.
    hope somone help me , Thank you

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,406
    With delay(1000) that is not pushing much out USB so it shouldn't cause trouble there.

    In setup() add :: pinMode(LED_BUILTIN, OUTPUT);
    > Also add to make sure USB is online before the first print:: while (!Serial && millis() < 5000) ;

    and in loop() do : digitalToggleFast( LED_BUILTIN );

    That will blink the LED showing the Teensy still running or not with a blink on/off each second.

  3. #3
    #include <DHT.h>


    #define DHTPIN 24

    #define DHTTYPE DHT22 // DHT 22 (AM2302)



    DHT dht(DHTPIN, DHTTYPE);


    void setup() {

    Serial.begin(9600);
    dht.begin();
    pinMode(LED_BUILTIN, OUTPUT);
    while (!Serial && millis() < 5000) ;

    }



    void loop() {

    digitalToggleFast( LED_BUILTIN );


    float h = dht.readHumidity();
    float t = dht.readTemperature();
    Serial.println(t);
    Serial.println(h);
    delay(1000);



    }

    blink still running

  4. #4
    Quote Originally Posted by defragster View Post
    With delay(1000) that is not pushing much out USB so it shouldn't cause trouble there.

    In setup() add :: pinMode(LED_BUILTIN, OUTPUT);
    > Also add to make sure USB is online before the first print:: while (!Serial && millis() < 5000) ;

    and in loop() do : digitalToggleFast( LED_BUILTIN );

    That will blink the LED showing the Teensy still running or not with a blink on/off each second.
    but when i use Sonarmax MB1242 blink not working
    Code:
    // -------------------------------------------------------------------------------------------
    // Basic Master
    // -------------------------------------------------------------------------------------------
    //
    // This creates a simple I2C Master device which when triggered will send/receive a text 
    // string to/from a Slave device.  It is intended to pair with a Slave device running the 
    // basic_slave sketch.
    //
    // Pull pin12 input low to send.
    // Pull pin11 input low to receive.
    //
    // This example code is in the public domain.
    //
    // -------------------------------------------------------------------------------------------
    
    /* Code for Arduino Uno R3  
    Assumes the sensor is using the default address 
    Sensor Connections: 
    Pin 7 to GND 
    Pin 6 to 5V 
    Pin 5 to SCL 
    Pin 4 to SDA 
    Requires pull-ups for SCL and SDA connected to 5V to work reliably 
    */ 
    
    #include <i2c_t3.h>
    
    // Memory
    #define MEM_LEN 256
    //The Arduino Wire library uses the 7-bit version of the address, so the code example uses 0x70 instead of the 8-bit 0xE0  
    #define SensorAddress byte(0x71) 
    #define NewSensorAddress byte(0x73) 
    //The sensors ranging command has a value of 0x51 
    #define RangeCommand byte(0x51) 
    //These are the two commands that need to be sent in sequence to change the sensor address 
    #define ChangeAddressCommand1 byte(0xAA) 
    #define ChangeAddressCommand2 byte(0xA5)  
    
    char databuf[MEM_LEN];
    int count = 0;
    
    void setup()
    {
        pinMode(LED_BUILTIN,OUTPUT);    // LED
        digitalWrite(LED_BUILTIN,LOW);  // LED off   
        // Setup for Master mode, pins 37/38, external pullups, 100kHz, 200ms default timeout
        Wire1.begin(I2C_MASTER, 0x00, I2C_PINS_37_38, I2C_PULLUP_EXT, 100000);
        Wire1.setDefaultTimeout(200000); // 200ms
    
        // Data init
        memset(databuf, 0, sizeof(databuf));
        count = 0;
        Serial.begin(9600);
    }
    
    void loop()
    {digitalToggleFast( LED_BUILTIN );
          digitalWrite(LED_BUILTIN,HIGH);   // LED on
    
          if (count==0){
            changeAddress(SensorAddress, NewSensorAddress, true);     //if is the 7 bit version
            Serial.print("Change address done ");
            count=1;
            Serial.println(count);
             }
          else{
    
              takeRangeReading();               //Tell the sensor to perform a ranging cycle 
           // Check if error occured
            if(Wire1.getError())
                Serial.print("FAIL\n");
            else{   
                delay(100);                       //Wait for sensor to finish 
                word range = requestRange();       //Get the range from the sensor 
                Serial.print("Range: "); Serial.println(range);  //Print to the user 
                digitalWrite(LED_BUILTIN,LOW);   // LED off
            }
            
         }
         
            
          
    }
    //Commands the sensor to take a range reading 
    void takeRangeReading(){ 
      Wire1.beginTransmission(NewSensorAddress);    //Start addressing 
      Wire1.write(RangeCommand);       //send range command 
      Wire1.endTransmission();        //Stop and do something else now 
      
    } 
    //Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no communication. 
    word requestRange(){ 
      Wire1.requestFrom(NewSensorAddress, byte(2)); 
      
         if(Wire1.available()>= 2){ //Sensor responded with the two bytes 
                byte  HighByte = Wire1.read();       //Read the high byte back
                byte  LowByte = Wire1.read();       //Read the low byte back
                word  range = word(HighByte, LowByte);   //Make a 16-bit word out of the two bytes for the range 
                return range;                          
         }
         else{
                //range = 0x00;
                return 0;                               //Else nothing was received, return 0 
         }
       
      
      
    }
    /* Commands a sensor at oldAddress to change its address to newAddress 
    oldAddress must be the 7-bit form of the address that is used by Wire 
    7BitHuh determines whether newAddress is given as the new 7 bit version or the 8 bit version of the address 
    If true, if is the 7 bit version, if false, it is the 8 bit version 
    */ 
    void changeAddress(byte oldAddress, byte newAddress, boolean SevenBitHuh){ 
      Wire1.beginTransmission(oldAddress);      //Begin addressing 
      Wire1.write(ChangeAddressCommand1);    //Send first change address command 
      Wire1.write(ChangeAddressCommand2);    //Send second change address command 
     
      byte temp; 
      if(SevenBitHuh){ temp = newAddress << 1; }    //The new address must be written to the sensor 
      else             { temp = newAddress;         }    //in the 8bit form, so this handles automatic shifting 
       
      Wire1.write(temp);          //Send the new address to change to 
     
      Wire1.endTransmission(); 
    }
    Last edited by defragster; 09-12-2020 at 04:12 PM. Reason: change quote to code #

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,679
    Sorry, I have not fully studied your code. But again it might help to understand more of what the problem is.

    That is all we know so far is it freezes. The question is when? Does it run for awhile or freezes real soon?

    Also the changes for LED are not likely to much for you:

    Code:
    void loop()
    {
    digitalToggleFast( LED_BUILTIN );
    digitalWrite(LED_BUILTIN,HIGH); // LED on
    You run the togglefast and then right after set it high and set it low at the end. Should probably use one method (toggle only at start, or HIGH/LOW)...

    My guess is it has more to do with Device and I2C than the Serial output.

    Again may help to know which sensor you are using. My guess is it is hanging in the reading of it...

    I know at one point I was playing with an SHT-31 sensor and at times it would hang the program...

    Also would help to know how it is hooked up.... What wires do you have connected to it? What voltage are you giving it? Do you have Pull Up resistors?

    Again not much to go on yet.

    If it were me, and I did not have a logic analyzer (which I do), I would probably instrument the code some to get an idea of where it may be hanging???

    Maybe change loop to something like:
    Code:
    void loop()
    { 
      digitalToggleFast( LED_BUILTIN );
      Serial.print(count,DEC); Serial.flush();
      if (count == 0) {
        changeAddress(SensorAddress, NewSensorAddress, true); //if is the 7 bit version
        Serial.print("Change address done "); 
        count = 1;
        Serial.println(count);
        Serial.flush(); // make sure it goes out before anything else...
      }
      else {
        takeRangeReading(); //Tell the sensor to perform a ranging cycle
        Serial.print("("); Serial.flush();
        // Check if error occured
        if (Wire1.getError())
          Serial.print("FAIL\n"); Serial.flush();
        else {
          delay(100); //Wait for sensor to finish
          Serial.print(")"); Serial.flush();
          word range = requestRange(); //Get the range from the sensor
          Serial.print("Range: "); Serial.println(range); //Print to the user
        }
    
      }
    }
    So you should see Output Serial port like:
    Code:
    0(Ch...
    1()Range: <the range>
    ...
    The use of Serial.flush() in places tells system to send what is currently in the buffer now and not wait for timeout or buffer full. This helps debug as sometimes there will be pending stuff in the Serial buffer, system crashes and you don't see what it actually last output.

    Good Luck

Posting Permissions

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