Reading mixed serial input

I have the following problem. I would like to read in the following protocol via Serial 2 and then output it again via Serial 1 as a test. The protocol consists of a mix of HEX and ASCII characters.

I get the following output with this code.


#define RXD2 16
#define TXD2 17

byte variable[8];
int i = 0;

void setup() {
  Serial.begin(9600, SERIAL_8N1);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

void loop() {
 while (Serial2.available() && i<8) {
      Serial.println(, HEX);



What do I have to do, to receive the correct result.

Best regards,

I'm not at all clear what you want to produce.

The code for SOH is 0x1, for DC3 is 0x13 etc so it seems to me as if the numbers are correct.

Where did you get the chunk of text that you have posted as an image?


i think you’re trying to do the following

this compiles on an UNO if I map Serial2 to Serial.
it captures a string of characters. it re-transmits it when EOT is detected
It drops it if it exceeds the size of the buffer.
you may need to recognize an ESC if ESC, SOH or EOT can be valid data

#define NUL 0x00
#define SOH 0x01
#define EOT 0x04

#define N   80
char variable [N];
int i = 0;

#if 1
#define Serial2     Serial

void setup() {
    Serial.begin(9600, SERIAL_8N1);
    Serial2.begin(9600, SERIAL_8N1);

void loop() {
    if (Serial2.available())  {
        byte c =;
        variable [i++] = c;

        // drop frame if start of new frame or too long
        if (SOH == c || N-1 <= i)
            i = 0;

        if (EOT == c)
            Serial.write (variable, i);

I think the first numbers are read currently. But as soon as I want to read the ASCII characters, it’s unreadable. How can I get the ascii string correctly?

why would ascii characters, which are only 7-bits and include your SOH and EOT, not be readable?

    if (Serial2.available())  {
        byte c =;
        variable [i++] = c;

I think the first numbers are read currently. But as soon as I want to read the ASCII characters, it’s unreadable. How can I get the ascii string correctly?

You need to explain where the data is coming from and where you got the image that you posted in your Original Reply.

My guess is that your program is working properly but the data that is being sent is not what you expect.


Hello Struber,

I use some code for a similar purpose, you are welcome to have it, use it, adapt to your own needs:

// Receives data on serial port 1 and sends to the serial monitor with an index.
// Each character is on a new line.

uint16_t baudHeating = 57600;

void setup() {
  Serial.println("Serial monitor started");
  Serial.println("Serial 1 started");

void loop() {

void serialMonitor() {
  char RxTemp;
  static uint8_t charCount;
  while (Serial1.available() > 0) {
    RxTemp =;
    Serial.print(" byte ");
    Serial.print((byte)RxTemp, HEX);
    Serial.print(" char ");
    if ((uint8_t)RxTemp == 0) {
      charCount = 0;
      Serial.println("charCount = 0");

The output looks like this:

0 byte 24 char $
1 byte 54 char T
2 byte 49 char I
3 byte 4D char M
4 byte 2C char ,
5 byte 31 char 1
6 byte 2C char ,
7 byte 31 char 1
8 byte 32 char 2
9 byte 2C char ,
10 byte 37 char 7
11 byte 2C char ,
12 byte 31 char 1
13 byte 34 char 4
14 byte 2C char ,
15 byte 31 char 1
16 byte 2C char ,
17 byte 35 char 5
18 byte 39 char 9
19 byte 0 char
charCount = 0

Each character is on its own line with its hex value and ASCII character, if there is one. Note that as there are about 15 or so output characters to each received character the serial monitor needs to be set to go much faster then the other serial port.

++Karma; // For posting code correctly on your first post.

you may be interested in the CMRInet Protocol