Loop between two serial

Status
Not open for further replies.

antgue

New member
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
 
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.
 
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.
 
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:
Sorry, was a mistake.
Work well also with "Double" serial!
Before don't work because name of PC com was swapped
 
Status
Not open for further replies.
Back
Top