Serial data through COM port and Realterm confusion.

Hello! I am trying to send a hex string through a com port using arduino, I have made a program that works well with RealTerm, it generates a string that I can enter in to RealTerm and it displays the numbers just the way I want it to. But I want the numbers to be sent automatically, without the use of Realterm and I feel like I'm almost at that point but I am very confused right now.

I have gotten to the point where it looks like it's working, it sends hex bytes, but not quite correctly, one byte is missing, and all the values are incorrect, when I try sending "0xA5" it sends "D2" instead of "A5", and I can't find the pattern, why it does that, I've included the code in hopes that some of you guys could help me!

it's not a big program so I included all of it.

the string I send with RealTerm using "Send Number" is this and I want to send it with arduino, I don't know how RealTerm encodes these.
"0xA5 0x68 0x32 0x01 0x7B 0x01 0x10 0x00 0x00 0x00 0x02 0x00 0x00 0x01 0x00 0x00 0x03 0x12 0x00 0x32 0x12 0x00 0x32 0x12 0x00 0x32 0xF9 0x01 0xAE"

in the code I try this

byte sends[29] = {0xA5,0x68,0x32,0x01,0x7B,0x01,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAE};

  Serial.write(sends, 29);

sketch_may15a.ino (1.97 KB)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(0));
}

void loop() {

  delay(10000);
 
    
    
  int fix = 0x12D;                        //Fixed value to calculate the checksum that is implemented.
  int color = 0x12;                       //Color code in the hex string
  int i1value = random() % 10 + 0x30;     //Random numbers to display on the screen
  int i2value = random() % 10 + 0x30;     //I am generating these random numbers in place of a reader
  int i3value = random() % 10 + 0x30;     //that I will implement later when I get this function to work
  
  
  
  

  int i1 = (color + i1value);             //adding these numbers together to calculate the checksum
  int i2 = (color + i2value);             //which all work properly when using realterm
  int i3 = (color + i3value);             //none of this should be a problem
  int sum = (i1 + i2 + i3 + fix);         //as far as I can tell
  int sum2;  
  
  if (sum > 0x300){                       //a not so elegant solution for the checksum variable
      
      sum2 = 0x03;
      
  }else if (sum > 0x200){
      
      sum2 = 0x02;
      
  }else 
  
      sum2 = 0x01;

  sum = (sum&0xFF);                        //this removes the high byte that would appear if the number exceeds FF


  byte sends[29] = {0xA5,0x68,0x32,0x01,0x7B,0x01,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAE};


 
  sends[17] = color;                       //adding the values that are not constant to the array
  sends[19] = i1value;
  sends[20] = color;
  sends[22] = i2value;
  sends[23] = color;
  sends[25] = i3value;
  sends[26] = sum;
  sends[27] = sum2;
  

  
    
  Serial.write(sends, 29);                //Printing the array to the monitor.
      


}

the string I send with RealTerm using "Send Number" is this

So, why are you having the Arduino send binary values, then? If you want the Arduino to send the same string, then make it do that.

char uselessData[] = "0xA5 0x68 0x32 0x01 0x7B 0x01 0x10 0x00 0x00 0x00 0x02 0x00 0x00 0x01 0x00 0x00 0x03 0x12 0x00 0x32 0x12 0x00 0x32 0x12 0x00 0x32 0xF9 0x01 0xAE";

.
.
.

   Serial.print(uselessData);

If you want to convert an array of bytes to a string, and send that string, that's not that hard, either. sprintf() is one way.

good question, I tried implementing exactly what you said, changing the bytes to char and serial.write to serial.print, but what I'm picking up from that seems to read "2D 69 D6 09 FD DF 00 2" instead of "A5 68..." etc

I'll try sprintf() and get back

EDIT

I tried this and nothing happens, it doesn't send anything at all, only the initiation setup comes up but then it seems to ignore the data I want to send

char sends[29];
sprintf(sends, "0xA5 0x68 0x32 0x01 0x7B 0x01 0x10 0x00 0x00 0x00 0x02 0x00 0x00 0x01 0x00 0x00 0x03 0x12 0x00 0x32 0x12 0x00 0x32 0x12 0x00 0x32 0xF9 0x02 0xAE");


Serial.write("%s", sends);

EDIT 2: I tried putting every single value into another byte and sending from that but that didn't change anything, what I believe is happening is RealTerm somehow encodes the message in a way that I can't figure out, I don't think it sends the string as is, I saw someone else ask the question once but I couldn't find an answer to "how does RealTerm send numbers?"

byte a = 0xA5;
byte b = 0x68;

EDIT 3: I changed the code to the VERY basics to see if it might be easier to find a solution, and I still have the same problems, here is my entire program now

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(0));
}

void loop() {

  delay(5000);
  char sends[29] ={0xA5, 0x68, 0x32, 0x01, 0x7B, 0x01, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x12, 0x00, 0x32, 0x12, 0x00, 0x32, 0x12, 0x00, 0x32, 0xF9, 0x02, 0xAE};
  
  Serial.print(sends);
}
char sends[29];
sprintf(sends, "0xA5 0x68 0x32 0x01 0x7B 0x01 0x10 0x00 0x00 0x00 0x02 0x00 0x00 0x01 0x00 0x00 0x03 0x12 0x00 0x32 0x12 0x00 0x32 0x12 0x00 0x32 0xF9 0x02 0xAE");

Your string is WAY longer than 29 characters...

You REALLY need to decide if you need to send a string, a bunch of characters, or a bunch of bytes.

you're absolutely right, this is a mess of a problem, I did change that from arrays to bytes to ints everything I could think of, the 29 of the array was the number of bytes I had in it, that's why that's still in there, I see how that will be a problem when I send it but that isn't the issue right now, I believe.

I just want to send "A5" in hex and see that I've sent "A5" in RealTerm. I'm going to ask on the realterm forum as well because I realize now that this is probably the wrong forum to ask this, but any other insight could be helpful

I just want to send "A5" in hex and see that I've sent "A5" in RealTerm. I'm going to ask on the realterm forum as well because I realize now that this is probably the wrong forum to ask this, but any other insight could be helpful

"A5" is a string NOT a HEX value.

0xA5 is a hex value.

"0xA5" is a string.

If your mysterious device expects strings (or string representations of numeric values in base 16), then you must send strings.

I think you are going to continue to struggle until you understand what your device wants (numeric values in base 16 or strings) AND until you understand the difference between a numeric value and a string.

Oh sorry for the confusion there, I didn't mean "A5" as code, I meant A5 the hexadecimal value 0xA5