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

Thread: Teensy v3.5, USB serial, transferring data between PC and teensy: problem in writing.

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    9

    Teensy v3.5, USB serial, transferring data between PC and teensy: problem in writing.

    Hi,

    My laptop is connected to Teensy v.3.5 through the only USB port it has. This is the cable: https://www.pjrc.com/store/cable_usb_micro_b.html

    I've written a small application that does an echo: The PC writes a text that the Teensy must read and send back to the PC.

    I open the port as follows:

    Code:
      Serial.begin(SERIAL_BAUD_RATE);
      delay(50);
      while (!Serial) { /* NOOP */  }
      delay(100);
    I have noticed that for the Teensy to transfer data to the PC, I have to add the following code:

    Code:
        size_t bytes_transferred = Serial.print(buffer);
        if(usb_serial_write(buffer, bytes_read) == -1) {
          digitalWrite(LED_PIN, HIGH);
          delay(1000);
          digitalWrite(LED_PIN, LOW);
          delay(200);
        }
    I don't understand why I have to invoke these two writing functions. How should I transfer data from the Teensy to the PC?

    Cheers

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,118
    How should I transfer data from the Teensy to the PC
    Actually this is quite simple. Here an example
    Code:
    void setup()
    {
        while(!Serial){}
    
        // writing text
        Serial.println("Some Text\n");
    
        // writing binary data
        byte buf[] = // some binary data
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec convallis ipsum,\n"
        "at imperdiet orci. Aliquam mollis mauris vitae tristique bibendum. Praesent sapien odio,\n"
        "lacinia vitae pellentesque eget, euismod ac metus. Nam sollicitudin vel nisl a consequat.\n"
        "Nunc nec elit nisi. Morbi quis justo vel ex tincidunt fermentum eget id metus.\n"
        "Suspendisse scelerisque egestas orci, id vulputate purus. Suspendisse gravida metus ut lacus dictum ultricies.\n"
        "Mauris massa nisl, vestibulum in tincidunt at.\n";
    
        constexpr unsigned bufSize = sizeof(buf) / sizeof(byte); 
        Serial.write(buf, bufSize);
    }
    
    void loop()
    {
        // echoing received data to the PC
        while(Serial.available() > 0) // while we have data in the receiving buffer
        {
            byte b = Serial.read();
            Serial.write(b);
        }
    }
    You can find more info about using Serial to communicate with a PC here: https://github.com/TeensyUser/doc/wiki/Serial

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,750
    Quote Originally Posted by sntnjrg View Post

    I have noticed that for the Teensy to transfer data to the PC, I have to add the following code:

    Code:
        size_t bytes_transferred = Serial.print(buffer);
        if(usb_serial_write(buffer, bytes_read) == -1) {
          digitalWrite(LED_PIN, HIGH);
          delay(1000);
          digitalWrite(LED_PIN, LOW);
          delay(200);
        }
    I don't understand why I have to invoke these two writing functions. How should I transfer data from the Teensy to the PC?

    Cheers
    there is no must, but this seems only to indicate if serial_write is failing
    As @luni indicates there are better ways to echo to PC

  4. #4
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    The problem I am observing is that I need to carriage out two write operations in a row for the PC to receive the information.

    I have the following cable: TTL-232R-5V

    I'm not sure, the teensy works at 3.3V. Can I use that cable with the serial port 1?

    Update: Using that cable I have the same problem. I have to invoke the write function twice.

    Code:
        size_t bytes_transferred = SERIAL_PORT.print(buffer);
        bytes_transferred = SERIAL_PORT.print(buffer);
    where SERIAL is, in this case, Serial3.
    Last edited by sntnjrg; 08-07-2020 at 12:07 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Hi,

    I'm sorry, I had a glitch in the code on the PC side.

    Thanks for the help.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,118
    I'm not sure, the teensy works at 3.3V. Can I use that cable with the serial port 1?
    You can use it for a T3.5 which has 5V tolerant pins. A T3.6 or a T4.X will not like the 5V signal on its RX pin.

Posting Permissions

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