# Serial.write truncating output

**URL:** <https://forum.arduino.cc/t/serial-write-truncating-output/1409153>\
**Category:** Programming\
**Created:** [October 11, 2025, 12:55pm UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153 "2025-10-11T12:55:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldgeezer2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldgeezer2/32/1126688_2.png) [@oldgeezer2](https://forum.arduino.cc/u/oldgeezer2)\
**Post date:** [October 11, 2025, 12:55pm UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/1 "2025-10-11T12:55:58Z")

</div>

I have some Dynamixels and up to this point I have been using a laptop running PUREBASIC to experiment with them and defined over 30 command strings. I am now trying to duplicate the functions using an R4. Some like:

LEDOn:  
Data.a $FF, $FF, $05, $04, $03, $19, $01, $D9

would be easy to program using the shield libraries, However some like:

Swish1:  
Data.a $FF, $FF, $FE, $22, $83, $1E, $04, $02, $31, $04, $50, $00, $03, $0C, $06, $50, $00, $04, $0C, $06, $50, $00, $0A, $0C, $0A, $50, $00, $0B, $0C, $06, $50, $00, $0C, $0C, $06, $50, $00, $9D

would be very time consuming to decode and implement (I’m almost 70 and I don’t know if I would last that long).

The PUREBASIC program reads the array and writes the output to the serial port connected to the motors.

I tried to convert the LEDOn function but I couldn’t get it to work.

```arduino

```

#include \<DynamixelShield.h\>

#define DEBUG\_SERIAL Serial  
// Dynamixel is Serial1

const uint8\_t DXL\_ID = 1;  
const float DXL\_PROTOCOL\_VERSION = 1.0;

DynamixelShield dxl;

byte LEDOn[] = { 0xff, 0x80, 0x7f, 0x04, 0x03, 0x19, 0x01, 0xD9 };

int x;

using namespace ControlTableItem;

void setup() {

DEBUG\_SERIAL.begin(9600);  
dxl.begin(57600);  
dxl.setPortProtocolVersion(DXL\_PROTOCOL\_VERSION);  
}

void loop() {

DEBUG\_SERIAL.println("LEDOn");

for (byte i = 0; i \< 8; i = i + 1) {

```
DEBUG_SERIAL.print("print i = ");
DEBUG_SERIAL.println(i);

x = LEDOn[i];
DEBUG_SERIAL.print("print x = ");
DEBUG_SERIAL.println(x);

DEBUG_SERIAL.println(x, BIN);
 DEBUG_SERIAL.println();
DEBUG_SERIAL.println();

```

}

delay(5000000);  
}

```arduino

```

The issue, I think, is that the output of any value lower than 0x80 is truncated to significant digits. The LEDOn string in this example was modified to show this behavior. Here is the output:

print i = 0  
print x = 255  
11111111 \<--- 8 bits

print i = 1  
print x = 128  
10000000 \<--- 8 bits

print i = 2  
print x = 127  
1111111 \<--- 7 bits

A couple of questions:

Is what I am seeing on line 3 of each output actually was is being written to the port?

If so is there a “reasonable” solution?

Thanks for any guidance.

---

<div class="post-metadata">

**Author:** ![oldgeezer2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldgeezer2/32/1126688_2.png) [@oldgeezer2](https://forum.arduino.cc/u/oldgeezer2)\
**Post date:** [October 11, 2025, 1:02pm UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/2 "2025-10-11T13:02:43Z")

</div>

Update to try to post sketch correctly.

```arduino
#include <DynamixelShield.h>

#define DEBUG_SERIAL Serial
// Dynamixel is Serial1

const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 1.0;

DynamixelShield dxl;

byte LEDOn[] = { 0xff, 0x80, 0x7f, 0x04, 0x03, 0x19, 0x01, 0xD9 };

int x;

using namespace ControlTableItem;

void setup() {

DEBUG_SERIAL.begin(9600);
dxl.begin(57600);
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
}

void loop() {

DEBUG_SERIAL.println("LEDOn");

for (byte i = 0; i < 8; i = i + 1) {

```

DEBUG\_SERIAL.print("print i = ");  
DEBUG\_SERIAL.println(i);

x = LEDOn[i];  
DEBUG\_SERIAL.print("print x = ");  
DEBUG\_SERIAL.println(x);

DEBUG\_SERIAL.println(x, BIN);  
DEBUG\_SERIAL.println();  
DEBUG\_SERIAL.println();

```arduino
}

delay(5000000);
}

```

---

<div class="post-metadata">

**Author:** ![maxgerhardt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maxgerhardt/32/1127032_2.png) [@maxgerhardt](https://forum.arduino.cc/u/maxgerhardt)\
**Post date:** [October 11, 2025, 1:04pm UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/3 "2025-10-11T13:04:01Z")

</div>

It is a known behavior that when printing a number via

```cpp
DEBUG_SERIAL.println(x, BIN);

```

then all leading zeroes are **not** printed. Hence `1111111` is the expected output for the number 0x7f / 127.

The solution is to print all bits manually:

> [@How can I; Serial.println() a binary output that will give all the leading zero's of a variable?](https://forum.arduino.cc/t/how-can-i-serial-println-a-binary-output-that-will-give-all-the-leading-zeros-of-a-variable/962247/2):
>
> try this void printBin(byte aByte) { for (int8\_t aBit = 7; aBit \>= 0; aBit--) Serial.write(bitRead(aByte, aBit) ? '1' : '0'); } void setup() { Serial.begin(115200); Serial.println(); byte x = 0b00001100; printBin(x); } void loop() {}

---

<div class="post-metadata">

**Author:** ![BitSeeker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bitseeker/32/1241632_2.png) [@BitSeeker](https://forum.arduino.cc/u/BitSeeker)\
**Post date:** [October 11, 2025, 10:51pm UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/4 "2025-10-11T22:51:39Z")

</div>

> [@oldgeezer2](#):
>
> I tried to convert the LEDOn function but I couldn’t get it to work.

I am not sure whether you are just testing the code logic at the moment, but there seem to be no statements actually sending bytes to the Dynamixel port as yet?

---

<div class="post-metadata">

**Author:** ![oldgeezer2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldgeezer2/32/1126688_2.png) [@oldgeezer2](https://forum.arduino.cc/u/oldgeezer2)\
**Post date:** [October 12, 2025, 12:16am UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/5 "2025-10-12T00:16:16Z")

</div>

BitSeeker,

Thank you for the response. Yes, I had skinnied the sketch down to only what I thought would show me what was going on. In the full sketch I had:

Serial1.write(LEDOn[i]); within the loop. When it didn't work i stripped it out for testing.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 10, 2026, 12:16am UTC](https://forum.arduino.cc/t/serial-write-truncating-output/1409153/6 "2026-04-10T00:16:51Z")

</div>

Closed on April 10, 2026, 12:16am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
