Problems with adding (byte) 0x00 to a byte array using Teensyduino

Hello,

I've been having issue with something that seems should be an easy fix, but can't get it to work. I'm attempting to send a message to a teensy 4.1 but can't send any 0x00's because it seems thatit is being interpreted as a termination character. All the solutions I've seen simply say add (byte) in front of the 0x00 but that just isn't working. I may be missing something simple but I've found that the main and obvious solution is not fixing this issue. Here's the sample code:

I try both (byte)0x00 and byte(0x00)

byte actuatorMessage [6];

void setup() {
  Serial.begin(9600); //not necessary for Teensy but attempting runs with and without it

  //Send command string
    actuatorMessage[0] = 0xFE; 
    actuatorMessage[1] = 0x02;
    actuatorMessage[2] = byte(0x00);
    actuatorMessage[3] = 0x01;
    actuatorMessage[4] = 0xBB;
    actuatorMessage[5] = (byte) 0x00;
}

void loop() {
  Serial.write(actuatorMessage, 6);
  Serial.println("Message Sent!");

  delay(2000);
}

However, what comes out on the serial monitors is the first two bytes on the array (as symbols but thats fine) that then continue to repeat again and again. When this happens the println message "Message Sent!" also doesn't get printed unless there's a delay of any value between the Serial.write and Serial.println.

Thanks for the help in advance!

Are you sure the serial monitor is also set to 9600?

The message you are sending to serial monitor is not ASCII text, so it's never going to make any sense in serial monitor if you send it like that.

When using Teensy 4.1 it seems I can't change the serial monitor baud rate. How would I know what the baud rate of the monitor is? I've also tried changing it to other standard baud rates or not include Serial.begin() at all and nothing changes.

Oh I just tried it on an Arduino and what I have works. I guess the baud rate is relevant to why its not working on the teensy 4.1. I'm looking more into it rn

If you can't see even the "Message Sent!" message, I think there's something wrong with the serial speed setting, like @jr6223 already told you. Check the Serial Monitor speed.
Please remember the Serial Monitor is not intended to show binary data.

I'll look more into it, thanks. Question though, if its a serial speed issue, how come the "Message Sent!" does send if I simply add a delay(1)?

How would I check the serial monitor speed? When using Teensy the option to change baud rate of serial monitor goes away. What I'm trying is connect to an Arduino Uno, where I have the option to change the baud rate of the Serial Monitor to 9600. Then unplug it and plug the Teensy without changing the code. But nothing changes. And if I close the serial monitor and open it up again once the Teensy is plugged in the option to change baud rate once again goes away

You need check the Arduino IDE config for Teensy boards - ie files platform.txt and board.txt regarding the predefined serial monitor baud

Ah, I guess that could be because Teensy 4.1 has native USB, so whatever baud rate you set is ignored because it always runs at full USB speed. (I think that means full USB 1.0/1.1 speed which is 12Mb/s). So I'm not sure why you don't see "Message Sent!". But the fact remains that your 6 byte message is not "printable" ASCII text and could be causing Serial Monitor to behave strangely.

I FOUND THE ISSUE! It seems under 'Tools' the USB Type defaulted to 'Raw HID' but when I change that to 'Serial' everything works as expected :slight_smile:

Thank you all for the help

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