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

Status
Not open for further replies.

sntnjrg

Member
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
 
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
 
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
 
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:
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.
 
Status
Not open for further replies.
Back
Top