-1

I have a RS232 device that I am able to communicate with using RealTerm on a windows PC.

The device is expecting a hex string like AA BB 03 01 03 EE

enter image description here


How would I send the equivalent string from an arduino? (I feel confident the arduino is wired correctly since I can see incoming uart data)

Things I have tried

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
      Serial.println('AA BB 03 01 01 EE', "OCT");
      delay(3000);
      Serial.println('0xAA 0xBB 0x03 0x01 0x01 0xEE', "DEC");
      delay(3000);
      Serial.println('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.println('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.println('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.print('0xAA 0xBB 0x03 0x01 0x01 0xEE', "DEC");
      delay(3000);
      Serial.print('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.print('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.print('AA BB 03 01 01 EE', "DEC");
      delay(3000);
      Serial.println('AA BB 03 01 01 EE', HEX);
      delay(3000);
      Serial.println('0xAA 0xBB 0x03 0x01 0x01 0xEE', HEX);
      delay(3000);
      Serial.println('0xAA 0xBB 0x03 0x01 0x01 0xEE');
      delay(3000);
      Serial.println('0xAA 0xBB 0x03 0x01 0x01 0xEE', HEX);
      delay(3000);
      Serial.write('AA BB 03 01 01 EE');
      delay(3000);
      Serial.write('0xAA 0xBB 0x03 0x01 0x01 0xEE');
      delay(3000);
}

I've also tried using double quotes as mentioned in the comments

     Serial.write("AA BB 03 01 01 EE");
     delay(3000);
     
     Serial.write("0xAA 0xBB 0x03 0x01 0x01 0xEE");
     delay(3000);

     Serial.write("101010101011101100000011000000010000000111101110");
     delay(2000);

Additionally I've tried writing the serial one line at a time

  Serial.write(0xaa); // AA
  Serial.write(0xbb); // BB
  Serial.write(0x03); // 03
  Serial.write(0x01); // 01
  Serial.write(0x01); // 01
  Serial.write(0xee); // EE
  Serial.write(0x0d); // \r
  Serial.write(0x0a); // \n
  delay(3000);

Additional Information

6
  • 'AA BB 03 01 01 EE' This is a multi-character literal (a different thing from a string literal) and won't play a role in your answer. I don't know whether or not it's relevant to the question. If not, you probably intended to have double quotes here.
    – timemage
    Jul 16, 2022 at 17:36
  • 1
    The device is expecting a hex string ... the first screenshot shows that binary values are being sent, not strings ... hexadecimal numbers are human readable representation of binary values
    – jsotola
    Jul 16, 2022 at 18:05
  • what problem are you seeing?
    – jsotola
    Jul 16, 2022 at 18:08
  • There are 2 problems. 1 is how to write out with Serial.write(). 2. Is how to understand what RealTerm is doing that arduino isn't doing. I'll capture that in another question (This question here is very similar to the second problem superuser.com/questions/245926/how-does-realterm-send-numbers)
    – spuder
    Jul 17, 2022 at 21:41
  • Futhermore, I'm finding that the expected values I'm sending over serial aren't matching the received results. Almost exactly identical to this form forum.arduino.cc/t/…
    – spuder
    Jul 18, 2022 at 0:55

2 Answers 2

3

As noticed by jsotola in a comment, your device doesn't seem to be expecting HEX at all, but rather plain binary. Serial.print() is not appropriate, as it is designed for sending ASCII text. For binary data, you should prefer Serial.write(). More specifically, for sending arbitrary binary data, the proper method is

size_t Print::write(const uint8_t *buffer, size_t size)

Applying this to your example gives:

const size_t packet_length = 6;
uint8_t packet[packet_length] = {0xaa, 0xbb, 0x03, 0x01, 0x03, 0xee};
Serial.write(packet, packet_length);
7
  • Thank you! This gets me closer, unfortunately the device still isn't responding. When I spy on the uart cable I see what is being transmitted is "~ (which converted from ascii is 7E 22
    – spuder
    Jul 16, 2022 at 19:47
  • 1
    @spuder: There is something wrong either with your setup or your serial sniffer. I just tested my code with hd /dev/ttyACM0, and it does send the intended sequence. Jul 16, 2022 at 19:59
  • I've created a simulator here: wokwi.com/projects/337371635280511572 When casting to a char the output is ª»î which matches hex AABB030103EE. So your code does appear to be logically correct, but there is something I'm not understanding in my situation.
    – spuder
    Jul 16, 2022 at 20:15
  • 1
    @spuder: Your simulation prints “170 187 3 1 3 238” (without the spaces), which does match 0xaa, 0xbb, 0x03, 0x01, 0x03, 0xee. Jul 16, 2022 at 20:24
  • I now understand ª»î is unicode, 170 187 3 1 3 238 is binary decimal, AABB030103EE is hexadecimal. I was confused because different terminals default to different display formats (even though it is all the same value under the hood).
    – spuder
    Jul 17, 2022 at 21:39
0

if device expects actual numbers, then it does not receive them in hex format or decimal, but in binary. in which case you would need an array of numbers and then go with them using for loop (or purpouse made function.

byte sequence[]={0xAA, 0xBB, 0x03, 0x01, 0x01, 0xEE};
for(byte i=0; i<sizeof(sequence);i++) Serial.write(sequence[i]);

if you have to send it as a HEX, then its most likely a case of hex representation and you can use printf for that

Serial.printf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X", 0xAA, 0xBB, 0x03, 0x01, 0x01, 0xEE);

Another think is that you should be careful using println when communicating with other devices. println ads extra character (number) for new line, after the data you wish to send. if you only want to send data you wish to send, then use print instead

Edit: Thanks for the note from Edgar Bonet, I was warned that printf is still not a standard instruction outside of the ESP core. Its well worth adding it up by yourself. Look for a guide on arduino playground.

7
  • 1
    Your Serial.printf() call can be simplified as Serial.print("0xAA 0xBB 0x03 0x01 0x01 0xEE");. Jul 16, 2022 at 22:00
  • yes, that is correct, but I have wanted to stress the 0x%02X formatting so that it always outputs two digit hex numbers and let the person latter change his variables after format string.
    – Tomas
    Jul 16, 2022 at 22:11
  • 1
    Good point! You may want to add a note, though, warning that Serial.printf() is not a standard Arduino method. It is available on ESP, but not on standard Arduino cores. Jul 17, 2022 at 7:57
  • To quote you: Good Point! I have got too used to esp core, that I have assumed that core-Arduino coughed-up with its hobby core in those years.
    – Tomas
    Jul 17, 2022 at 15:45
  • 1
    Alas, it would seem the core Arduino devs do not want the official cores to support printf(). 😞 Jul 17, 2022 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.