Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Sending 0xd byte, but receving 0xa

  1. #1
    Junior Member
    Join Date
    Oct 2018

    Sending 0xd byte, but receving 0xa

    I am having a strange problem that I haven't been able to find the cause of.

    On the teensy, I am writing the byte "0xd" to USB serial. My teensy code is as follows:

    void setup() {
    void loop() {

    On my computer running Ubuntu 16.04, I am reading the USB port using the following code and printing the output. I am expecting "d" to get printed, but instead "a" gets printed. I have tried sending other bytes from the teensy and they get printed correctly. It is only happening with 0xd. Does anyone know what could be causing this?

    The following was compiled with "g++ d_test.cpp -o d_test" and run with "./d_test"
    #include <stdint.h>
    #include <iostream>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    int main(int argc, char** argv){
      int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
      // If the device has been opened successfully, configure it
      if(fd != -1) {
        // set non blocking
        fcntl(fd, F_SETFL, FNDELAY);
        struct termios options;
        tcgetattr(fd, &options);
        // set baud rate
        cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);
        // ignore modem lines and enable receiver
        options.c_cflag |= (CLOCAL | CREAD);
        // set size of character to 8
        options.c_cflag |= CS8;
        // disable hardware flow control
        options.c_cflag &= ~CRTSCTS;
        // disable canonaical mode, echo, and signal generation
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        // apply the changes
        tcsetattr(fd, TCSANOW, &options);
        std::cout << "Failed to open teensy" << std::endl;
        return 1;
        // try reading a byte from the teensy
        uint8_t byte = 0;
        int ret = read(fd, &byte, sizeof(uint8_t));
        // if a byte was read, print it
        if(ret == 1){
          std::cout << std::hex << (unsigned int)byte << std::endl;
      return 0;

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Sounds like a unix line discipline is "helpfully" changing stuff.

    Use cfmakeraw(&options) to configure for raw mode, so you actually receive the same bytes Teensy transmits. For details, type "man termios".

  3. #3
    Junior Member
    Join Date
    Oct 2018
    That worked. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts