How to write command from Arduino to another device via RS232?

Hi All,

I try write HEX sequence from Arduino Mega to another device via RS232.

Serial1.print('02H 46H 43H 03H BCC',HEX)

I convert HEX sequence to DEC and write to device again

Serial1.print('2 70 67 3 3020',HEX)

but device not recognise HEX sequence as command.

How to correctly write HEX sequnce as command to device?

Lot of thanks Regards Taras

What device?

HEX is just a representation of a binary number. You most likely need to send the device a particular series of 8-bit binary values, which can be conveniently represented to humans using HEX notation. If you will take the time to explain exactly what you want to do and why, perhaps someone will be able to help.

jremington: What device?

HEX is just a representation of a binary number. You most likely need to send the device a particular series of 8-bit binary values, which can be conveniently represented to humans using HEX notation. If you will take the time to explain exactly what you want to do and why, perhaps someone will be able to help.

The device is card dispenser CRT-531 from Creator Technology (China) How to send a particular series of 8-bit binary values? Need convert HEX sequence to BIN before send to device?

Thanks

Serial1.print('02H 46H 43H 03H BCC'

This code is not sending Hex. It is sending a series of 19 characters - the spaces are also characters. It is up to the program that receives the 19 characters to figure out what they represent.

Serial1.print('2 70 67 3 3020'

And this just sends 15 characters.

Post a link to the datasheet for the device you are trying to use.

...R

Hi Robin, I uploaded specification into http://www.park-master.org/CRT-531-V3-Specification.pdf

Thanks Kind regards

Single quotes are for single characters. Which ONE key on your keyboard did you press to get this one character?

Serial1.print('02H 46H 43H 03H BCC',HEX)

PaulS:
Single quotes are for single characters. Which ONE key on your keyboard did you press to get this one character?

Serial1.print('02H 46H 43H 03H BCC',HEX)

I want write HEX sequence. I am trying to write a sequence of character by character, but the same result

The full code is:

const int ledPin = 9;
const int keyPin = 2;
int keyCount = 0;

void setup(){
  pinMode(keyPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (digitalRead(keyPin) == HIGH){
    delay(10);
    if(keyCount == 0) {
    digitalWrite(ledPin,HIGH);
    Serial.print('02',HEX);
    Serial.print('46',HEX);
    Serial.print('43',HEX);
    Serial.print('03',HEX);
    Serial.print('BCC',HEX);
        
    keyCount++;
    }
    } else {
    digitalWrite(ledPin,LOW);
    keyCount = 0;
    }
}

P.S LED used for visualization only
I rewrote code at Arduino Ethernet for testing

    Serial.print('02',HEX);

Single quotes are for single characters.

Someday, you'll get smart and start using double quotes for strings. But, I'm not holding my breath.

I just had a look at the documentation for the card dispenser, and it is truly awful. The communications protocol is unusually bizarre and given how poorly the documentation is written, I think it would take quite a bit of experimentation to get anything to work. For example, some of the commands appear to require 12 or 16 bit values.

You might ask the manufacturer for an example program, demonstrating basic operation.

PaulS:     Serial.print('02',HEX);

Single quotes are for single characters.

Someday, you'll get smart and start using double quotes for strings. But, I'm not holding my breath.

Ok.. I rewrote the code but did not get a successful result What is wrong?

const int ledPin = 9;
const int keyPin = 2;
int keyCount = 0;

void setup(){
  pinMode(keyPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (digitalRead(keyPin) == HIGH){
    delay(10);
    if(keyCount == 0) {
    digitalWrite(ledPin,HIGH);

    Serial.print('02',HEX);
    Serial.print('46',HEX);
    Serial.print('43',HEX);
    Serial.print('30',HEX);
    Serial.print('03',HEX);    
    Serial.print('34',HEX);
    
    keyCount++;
    }
    } else {
    digitalWrite(ledPin,LOW);
    keyCount = 0;
    }
}

What is wrong?

Basically, everything that you are doing, which includes not carefully reading and understanding the responses to your questions.

I rewrote code as...

const int ledPin = 9;
const int keyPin = 2;
int keyCount = 0;
uint8_t my_serial_bytes[6]={0x02, 0x46, 0x43, 0x30, 0x03, 0x34};

void setup(){
  pinMode(keyPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (digitalRead(keyPin) == HIGH){
    delay(10);
    if(keyCount == 0) {
    digitalWrite(ledPin,HIGH);
    Serial.write(my_serial_bytes,sizeof(my_serial_bytes));
    
    keyCount++;
    }
    } else {
    digitalWrite(ledPin,LOW);
    keyCount = 0;
    }
}

All HEX sequence writing in dispenser via RS232 but result is unsuccesfull The manufacturer of dispenser send this sequence for me as commands

Much better, but how are you connecting the Arduino to the card dispenser? It may require an RS232 level shifter, but the documentation does not seem to address this.

Ok.. I rewrote the code but did not get a successful result

Still using single quotes for strings...

I connecting the Arduino to the card dispenser as
Card dispenser(RS232) <–>(RS232<->TTL)<–>TTL(Arduino) // pin 0 and 1
I gathered converter (RS232<->TTL) on a chip MAX232N

PaulS:

Ok.. I rewrote the code but did not get a successful result

Still using single quotes for strings...

PaulS, Please write sample code for example

Card dispenser(RS232) <–>(RS232<->TTL)<–>TTL(Arduino) // pin 0 and 1

Ground too? Also, do you have RX on the dispenser connected to TX on the arduino? It’s a common mistake to connect RX to RX.

Please write sample code for example

Serial1.print("02H 46H 43H 03H BCC") Though I have serious doubts that the device actually expects strings. Or spaces.

wildbill:

Card dispenser(RS232) <–>(RS232<->TTL)<–>TTL(Arduino) // pin 0 and 1

Ground too? Also, do you have RX on the dispenser connected to TX on the arduino? It’s a common mistake to connect RX to RX.

I checked connection before start topic :slight_smile:
I connect the Arduino to PC via USB2Serial converter and I view data in terminal window (eg. Hyper Terminal)

The OP most recently posted the following, which is in principle correct.

Serial.write(my_serial_bytes,sizeof(my_serial_bytes));

So, the following response is very misleading:

Quote Ok.. I rewrote the code but did not get a successful result End Quote Still using single quotes for strings...