Technology stack:
1. Teensy 4.0 with the FlexCAN_T4 library
2. Processing 4.1.2 (Java-like) language
Task:
Fix the corrupted data issue in a bidirectional serial-port (UART) communication. Both Teensy and Processing should receive unshifted data.
Envisioned solution:
– 2 threads on Teensy: one for Serial Tx in the main loop, another one for Serial Rx in a second loop, e.g. yield(), serialEvent(), or TeensyThread.
– synchronization between Teensy and Processing Tx buffers: either hardware (RTS/CTS pins of UART) or software (XON/XOF or a lightweight custom protocol with acknowlendgements)
Current use case:
Teensy and Processing exchange serial data asynchronously. Teensy constantly sends a burst of data to Processing. This burst of data is received from CAN on interrupts. Processing periodically sends data to Teensy.
Issue:
Teensy always receives correct data from Processing. However, when a collision between Tx on Teensy and Tx on Processing occurs, the data from Teensy gets shifted, and is received shifted by Processing. If no data is sent from Processing to Teensy, Processing always receives correct data.
Example code:
I attach a simplified (with no CAN data) Teensy code which recreates the issue.
Deliverables:
A Teensy code capable of receiving correct Serial data and transmitting correct Serial data via the same port. The incoming Serial data can appear randomly. The transmitted Serial data should be generated and sent out on interrupts.