-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
  • 1
  • 8

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
  • 45,094
  • 4
  • 42
  • 81
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
  • 338
  • 1
  • 2
  • 9