-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

spuder
  • 111
  • 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 '22 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 '22 at 18:05
  • what problem are you seeing? – jsotola Jul 16 '22 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 https://superuser.com/questions/245926/how-does-realterm-send-numbers) – spuder Jul 17 '22 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 https://forum.arduino.cc/t/how-to-send-hexadecimal-value-to-serial-devices/138834/15 – spuder Jul 18 '22 at 00:55
  • The problem was that I wasn't using a RS232 to TTL adapter. Once I added that in between the arduino and the device the correct values were received. – spuder Jul 18 '22 at 22:56

2 Answers2

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);
Edgar Bonet
  • 39,449
  • 4
  • 36
  • 72
  • 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 '22 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. – Edgar Bonet Jul 16 '22 at 19:59
  • I've created a simulator here: https://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 '22 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. – Edgar Bonet Jul 16 '22 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 '22 at 21:39
  • I setup a loopback debugging cable to see the output. When using RealTerm I get `AA BB 03 01 03 EE`, but when using the arduino I get `95 91 7E 7F 7E 04 00` – spuder Jul 17 '22 at 22:14
  • Once I used a proper RS232 to TTL adapter between the arduino and the device, the serial values started transmitting properly. Thank you https://www.amazon.com/dp/B07BJJ1T5G?ref=ppx_yo2ov_dt_b_product_details&th=1 – spuder Jul 18 '22 at 22:58
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.

Tomas
  • 324
  • 2
  • 9
  • 1
    Your `Serial.printf()` call can be simplified as `Serial.print("0xAA 0xBB 0x03 0x01 0x01 0xEE");`. – Edgar Bonet Jul 16 '22 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 '22 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. – Edgar Bonet Jul 17 '22 at 07: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 '22 at 15:45
  • 1
    Alas, it would seem [the core Arduino devs do not want the official cores to support `printf()`](https://github.com/arduino/ArduinoCore-API/pull/28). – Edgar Bonet Jul 17 '22 at 18:56
  • I would suggest you view a tutorial or two on HEX numbers and how they are represented. – Gil Jul 17 '22 at 20:27
  • @Gil What do you mean? if you see something incorrect state it. I take Feedback, but be specific so that I and the others can learn from it. I Personaly use HEX numbers in programming more then decimal. Mine communication library has all the instructions in HEX. So if you have some specific improvement of mine approach in mind, state it and I will edit mine answer quoting you in edit, like I did with the Edgar Bonet s useful point. – Tomas Jul 17 '22 at 21:29