serial communicaton truncated, while also communicating over I2c with mlx90641



I have a problem with my serial communication, in combination with I2C communication.
Let me first explain my setup.
I have a 3D-printer main board, a SKR-v2 running Reprap firmware 3.3. RRF3 is configured to read a MAX6675-thermocouple board for bed temperature. Then I have a Teensy 3.6 that emulates the max6675 to send the measure temperature. It gets this temperature from a MLX90641 IR array sensor connected via I2C, and using the Seeed_Arduino_MLX9064x-master library. It then filters out the pixels of 8 area’s I am interested in, and it calculates an average bed temperature to be send over SPI to the SKR-2.
Then the 8 area temperatures are used to control 8 heaters with a PID control loop, that calculates the right PWM value for all 8 heaters. To do that it needs a Set-temperature.
It get that from a serial connection with the SKR-2.
A gcode command is send over serial 3: M409 K"heat.heaters[].active"
And RRF3 replies with: {"key":"heat.heaters[].active","flags":"","result":[35.0],"next":0}
That is 67 characters plus a “\n” newline.
Then I filter out the “35.0” and I have my set-temperatur for the heaters…. So far so good.
Except when I also enable the loop that reads the MLX90641 over I2C my serial communication seems truncated, and I receive:
Notice that the last “:0} \n” skipped.
And if the settemp has three digits, let say 100 then “t:0} \n” is skipped.
So the string is always 63 characters long.

So my question is: Is there some link between I2c and Serial communication that could cause this?
Or any other reason why a serial communication might get truncated at 63 characters?


  • All_shape_SLS_heater_system.ino
    15.8 KB · Views: 24
  • Read_IR_sensor.ino
    755 bytes · Views: 24
  • Serial_com.ino
    4.9 KB · Views: 30
The code is taking longer to return to read the incoming serial characters than it takes to fill the buffer.

The default buffer size is set at 64 - giving 63 usable characters stored.

Either find a way to read the characters more often, perhaps to a local buffer is needed.

Or, use this to allocate more buffer space at runtime in setup(): serial_add_memory_for_read(void *buffer, size_t length);

You can set that up to length of 190 and that will allow over 250 incoming bytes to be stored before they are lost.
It sound like that is exectly what I need, but I could not find any examply code, so now I am struggeling to get it to work.
I have added this to the declarations:

unsigned char serial1buffer[190];
int sizeOfSerial1buffer=190;

And I have added this to my void setup() {
Serial_add_memory_for_read(serial1buffer, sizeOfSerial1buffer);

but that gives me an error:
'Serial_add_memory_for_read' was not declared in this scope
Serial_add_memory_for_read(serial1buffer, sizeOfSerial1buffer);

I also found this function on the teensy website that looks the same:

Serial1.addMemoryForRead(serial1buffer, sizeOfSerial1buffer);

but that gives an other error :
'class HardwareSerial' has no member named 'addMemoryForRead'
It sound like that is exectly what I need, but I could not find any examply code, so now I am struggeling to get it to work.

My bad in a rush to post ... it seems that was only added to Serial1? What Serial# is in use? posted snippet suggest Serial1?

What version of TeensyDuino is installed? That was added not to many releases before current TD 1.56.

If that code is removed, and you make an edit to a CORE file : {local install}\hardware\teensy\avr\cores\teensy3\serial2.c

That file is for Serial2 as indicated, but the same for others with diff#, in that file edit this line: #define SERIAL2_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer

Change the 64 to 128 or larger number and it will allocate that buffer for incoming characters.

Also note: On T_3.x Using Serial1 or Serial2 is best with larger FIFO support to minimize interrupts transferring data to that buffer.
Yes!!! that solved it!

I am using AdruinoIDE 1.8.13
And serial 1 was reading truncated strings.
but after editing file serial1.c and changing the 64 to 128, all my troubles went away!

Thank you for your time
While the above solution did work, since I upgraded Arduino IDE from 1.8.13 to 2.2.1 it stopped working.

Is there an other way to enlage the serial buffer on a teensy 4.1?

i used to edit the file Hardwareserial3.cpp and edit the RX_buffersize value to 1024. But since the IDE upgrade my teensy receives just 40 bits over serial and then truncates the rest.