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

Thread: Loop between two serial

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    3

    Loop between two serial

    I guys,
    i made a project with Teensy 3.2 and display Nextion (connected to Serial1).
    In my previous project with Arduino mega i used the onboad serial to transfer program to display Nextion with this skecth and it work fine:


    while (Serial.available()> 0) {
    Serial1.write(Serial.read());
    }

    while (Serial1.available()> 0) {
    Serial.write(Serial1.read());
    }

    Now, i upgrade my project using teensy3.2 but this part of program don't work anymore.
    The comunication with Nextion programmer is running, the program is trasferred but after it the display show the message "Update Failed:check Error!"

    I'm asking if someone can have some idea about this problem.
    Thanks in advance.
    Antonio

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,206
    Always hard to know without a lot of additional information. Could be simply either overrunning the display with data or...

    Could be the Serial buffer size, and you have more data on Serial than will fit in the data buffer for Serial1, so it sits there until it can send the data and as such your second loop never runs...

    When I have things like this, unless the two parts are in lock step with each other. That is first you need to send a complete package from Serial to Serial1 and then wait for it to complete, before reading back data from Serial1, I prefer to not have the sketch have to wait on buffer space to be free before checking the other side...

    You might want to look at the example sketch Teensy->USB_Serial->USBtoSerial
    It shows a way to make sure that the second have of the connection is not starved.

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,552
    USB is much faster than Hardwareserial, so it receives much faster than Serial1 can send it.
    You want something like

    Code:
    while (Serial1.availableForWrite() > 0 && Serial.available()> 0)  Serial1.write(Serial.read());
    and vice versa.
    It perhaps makes sense to use a common lop for both.
    Code:
    void loop() {
     if (Serial1.availableForWrite() > 0 && Serial.available()> 0)  Serial1.write(Serial.read());
     if (Serial.availableForWrite() > 0 && Serial1.available()> 0)  Serial.write(Serial1.read());
    }
    (untested)
    If that still not works, you might to have to increase the buffers.

  4. #4
    Junior Member
    Join Date
    Mar 2021
    Posts
    3
    Thanks a lot for your support.
    I try all your right advise but nothing changed.
    Maybe i find problem: in the arduino ide, "Tools > USB Type" i enable the option "Triple" an i used SerialUSB1 instead of Serial and now it work.
    I think that using Serial, the same used for bootloader, some data on flow, can be enable something in the bootloader.
    I don't have another explanation, but i not undestand why it don't work il i select "Double" instead of "Triple" serial.
    Thanks again
    Last edited by antgue; 03-05-2021 at 12:58 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2021
    Posts
    3
    Sorry, was a mistake.
    Work well also with "Double" serial!
    Before don't work because name of PC com was swapped

Posting Permissions

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