Reading UART data on Serial Monitor

I am new to arduino and am trying to communicate with a peripheral device using UART, but having trouble with the serial monitor displaying it

Essentially, I need to send an init packet to the device, to which it will respond with its own packet.

Example:

The first 4 bytes are ascii characters: INIT
The next 4 are defined as uint32: 1000
The final (variable length) is binary data: 0

I would recieve:
RESP10000

All of this is done with hexadecimal

So i run my code and the serial monitor starts displaying strange characters. My best guess is that it's being mixed up as to what to display

For example, 'R' as an ascii character has hex code 0x52, and an asterisk '*' has an octal code of 52 (Which is what is being displayed instead of an R

My code is below and my question is: Why is the serial monitor displaying this information and is there a way I can read the data in a different format? (I am aware of the println() function buut that would send the data back down the UART and confuse the peripheral)

My code is below for help. Very Thanks

//simply
//Begin thee Uart at 115200 by sending INIT

//All the while recieve any data

byte data;

void setup() {

  Serial.begin(115200);
  delay(3000);
  sendINIT();
  delay(500);
}

void loop() {
  void handleRESP();
  delay(300);
  if (Serial.available()){
    Serial.read();
    delay(25);
  }
  delay(15000);
}

void handleRESP(){
  //handle the error code provided by RESP
  if (Serial.available()){
      char Headerbit1 = Serial.read();
      delay(10);
      char Headerbit2 = Serial.read();
      delay(10);
      char Headerbit3 = Serial.read();
      delay(10);
      char Headerbit4 = Serial.read();
      delay(10);
      uint32_t SizeBit1 = Serial.read();
      delay(10);
      uint32_t SizeBit2 = Serial.read();
      delay(10);
      uint32_t SizeBit3 = Serial.read();
      delay(10);
      uint32_t SizeBit4 = Serial.read();
      delay(10);
      uint8_t errorCode = Serial.read();
  }
}

void sendINIT(){
  Serial.write(0x49);
  Serial.write(0x4E);
  Serial.write(0x49);
  Serial.write(0x54);
  Serial.write(0x01);
  Serial.write((byte)0x00);
  Serial.write((byte)0x00);
  Serial.write((byte)0x00);
  Serial.write((byte)0x00);//value zero - 115200 BAUD
}

And here is the serial monitor output:
�*� then 5 blank boxes after that I can't type here

Do you have the device attached to the serial port? If so it won't work because the serial port is already in use by the serial monitor. You need to attach the device to a different serial port. Depending on which "Arduino" you have there may be additional hardware serial ports, otherwise use a software defined serial port.

1 Like

Explain more about the format expected by your device. Is it expecting four bytes of a uint32_t? Is it expecting the 4 ascii characters '1' '0' '0 '0'.

This is not either and is unusual

Serial.write(0x01);
Serial.write((byte)0x00);
Serial.write((byte)0x00);
 Serial.write((byte)0x00);
1 Like

Yes sorry it is expecting 4 bytes of uint32 with values of one, zero, zero, zero and zero

How would I write it correctly? I did it this way because when using tera term i types this and it worked

If this is what you did with terminal app, the I think it is correct.

Can you share the datasheet / user manual / protocol specification of that device?

What did you exactly type in teraterm; which settings did you have in teraterm? Terminal programs usually use ascii and if you typed something, I suspect that you typed INIT1000 (not sure how you got the binary zero after that).

1 Like

Indeed - Teraterm certainly does.

RealTerm is one that can display & send binary data:
image

https://realterm.sourceforge.io/

I've used realterm so I know it a bit :wink: Question was what OP did to achieve it.

Hi all I post an update

Sorry for confusion at times i try my best with english but I confuse you sometimes!

I have been able to see my RESP packet at times. sometimes i see "RESP " then 5 blank boxes (so it would mean that it works and the serial monitor is not displaying the 0x10 and 0x00's)

But other times it will show question marks in diamonds, and sometimes even both (i set the loop function to iterate through the recieved data buffer, and it will at first show rubbish then it will show "RESP....."!)

So my question is now, how can i get the arduino to read the data bytes after the first 4 characters (R,E,S and P) as numbers rather than anything else (I think it is displayiing boxes because 0x01 is new header in ascii)

Like terminal programs (see Post #6), the Serial Monitor is just intended for printable text (ASCII characters) - not binary data - so your binary data will just show up as "weird characters"

You need to use something that lets you enter binary data, and can display binary data.
Maybe you've found a way to get TeraTerm to do that, but I never have.

I would use RealTerm for this (see post #7) - others are available.

EDIT

i seem to have overlapped with your latest post - which has the same issue of dealing with non-printable (binary) data in a serial terminal ...

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