Software Serial (RS232) / Sending and receiving real HEX data / frustrated...

Dear forum members

I am trying to communicate with an electronic door lock over its rs232-interface. I figured out that the device works with HEX codes by sniffing the transfer from the original software with a serial monitor. Later I was able to communicate with it over Hyperterminal by entering HEX code. Now I am trying to communicate with it over the arduino board. As I am using the Serial port to see the output i am using the software serial for communication with the device. i tried first on digital ports 4/5, now the rx and tx is on 10/11.

After searching all day in the whole forum and trying out various things I need your help.

The device answers to the following HEX characters without a return/new line: 0x02 0x46 0x46 0x48 0x44 0x34 0x04

Here is my code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

byte GetData[] = {0x02,0x46,0x46,0x48,0x44,0x34,0x04};
int i = 0;

void setup() {

  Serial.begin(57600);
  mySerial.begin(57600);

}

void loop()
{
  
  char buffer[4];
  if(i>30000) {
  Serial.println("Sending...");
    for (int i=0; i<sizeof(GetData); i++){
      sprintf(buffer, "%02X ", GetData[i]);
      mySerial.write(buffer);
      Serial.print(buffer);
    }
    Serial.println("");
    i=0;
  }
  if (mySerial.available()) {
    Serial.println("data");
    int inByte = mySerial.read();
    Serial.write(inByte); 
  }
  if (Serial.available()) {
    int inByte = Serial.read();
    mySerial.write(inByte); 
  }
  i++;
}

I can trigger some output by using the transponder, letting the door lock print an event over rs232. But there also I couldn’t get the content right yet… (i did get some numbers, hex values… but not the code that I found when I was sniffing, so I definitely would know the correct content…)

BTW: I just connected RX, TX and GND - that’s enough right?
The device settings should also be right. (57600baud / 8 data bits / 1 stopbit / no parity) that’s how software serial is working as far as I know.

I really hope you can get my some further. Thanks a lot in advance.
Dave

  1. have you tried to get it to work at 9600 baud?
    57600 baud is pretty high for SWSer.

  2. SW serial is good in sending but for reading it is less useful as you must be reading when the data comes in.
    So I expect that the staement Serial.available() will not work as there is never a byte available....

You are pretty fast :slight_smile: thx rob

The device specification defined the 57600 baud. So I have to keep this i guess...

When I move a transponder card to the reader the device gives out a serial message, which appears on the serial monitor, but sadly not in readable content... :frowning: so i guess the answer to my HEX request should arrive also as it will be sent after i sent the last hex value... right?

I adjusted the code a bit to get the full stream when holding the transponder card to the reader of the device. Also I replaced the UNO with a MEGA to test it with the second serial pins. no change though...

Code adjustment where I read from the device:

if (Serial1.available()) {
    Serial.println("data");
    while(Serial1.available()>0) {
      byte inByte = Serial1.read();
      Serial.print(inByte); 
      delay(10);
    }

Output with the request I send and the content I get when placing the transponder card to the reader to force a serial message from the device. the correct content would be:
02 36 46 43 54 33 30 33 33 46 30 45 34 37 41 33 44 04 (instead of that 6350469389102...)

...
Sending...
02464648443404
Sending...
02464648443404
data
63504693891026230646693251009510225103129999151610310210110231Sending...
02464648443404
Sending...
02464648443404
Sending...
02464648443404
...

The device should answer to the request 02 46 46 48 44 34 04 with 02 58 52 44 56 31 38 41 44 04

I just connected the rx from the laptop to the tx of the arduino and vica versa, also gnd is connected.

I created a connection with the same settings and also with 9600 bauds on both sides. I sent the following data from laptop to the arduino:

HEX (laptop) -> result on Arduino
0x01 -> 127
0x02 -> 63
0x03 -> 126
0x04 -> 31
0x05 -> 125
hallo -> 169 58 58 10 2

I compared ASCII, HEX, DEC values of those data, but do not have any clue what is going wrong! :frowning:

Changing baud rate or other settings do not impact it. I also moved the Serial to Serial2 of the MEGA, still same problem.

I tried to send the input coming to the arduino right back to the laptop and end up getting different values as the once that were sent, the same values each time at least.... I am pretty frustrated now and leave it as it is right now... I need a beer.

Try this:
Serial.print(inByte, HEX);

and for sending back:
Serial.write(the_data); // whatever is to go back

That’s actually how I started…

I tried to following methods:

Serial1.write(inByte);
Serial1.print(inByte);
Serial1.print(inByte, HEX);
Serial1.print((byte)inByte);
Serial1.print(inByte, DEC);

When I communicate arduino <-> laptop over serial, I get constant but weird outputs…
I send the numbers 1-5 and don’t even get some following numbers… At least every ‘1’ gives me the same output, when I send it several times.

It is like the arduino is talking a different language? baud rate, control bits and length is all correct…

cotwild:
I compared ASCII, HEX, DEC values of those data, but do not have any clue what is going wrong! :frowning:

So I thought I would compare the binary

0000 0001 -> 
0111 1111 = 127

0000 0010 -> 
0011 1111 = 63

0000 0011 ->
0111 1110 = 126

0000 0100 ->
0001 1111 = 31

0000 1000
0111 1101 = 125

Along with the 8 data bits, there will be a stop bit and the pattern starts to look like a flip and shift.

So I re-read your post more carefully

the rx from the laptop to the tx of the arduino and vica versa, also gnd is connected.

Your laptop uses RS232 voltage levels. Your Arduino uses TTL.
And there you have a mechanism for the bit flipping.

cotwild:
I just connected the rx from the laptop to the tx of the arduino and vica versa, also gnd is connected.

I created a connection with the same settings and also with 9600 bauds on both sides. I sent the following data from laptop to the arduino:

HEX (laptop) -> result on Arduino
0x01 -> 127
0x02 -> 63
0x03 -> 126
0x04 -> 31
0x05 -> 125
hallo -> 169 58 58 10 2

I compared ASCII, HEX, DEC values of those data, but do not have any clue what is going wrong! :frowning:

It looks like it is inverted and reversed
0x01 == 0000 0001 127 == 0111 1111
0x02 == 0000 0010 63 == 0011 1111 (OK almost...)

or inverted, reversed and missing a start-bit. ...

can you try the bytes
0x55 == 0101 0101
0x66 == 0110 0110
0x77 == 0111 0111

wow... what a great effort you guys put into my problem!! thank you very much Matt and Rob....
I'll check this out as soon as I am at home! I'll get back to you with some more examples, if needed.

Tomorrow I got time to work on it. I'll be back :slight_smile:

if (Serial2.available()) {
while(Serial2.available()>0) {
byte inByte = Serial2.read();
Serial.print(inByte); 
Serial.print("-");
Serial.println(inByte,BIN); 
}
}

Output: (Sent on PC -> Arduino output
0x55 -> 85-1010101
0x66 -> 38-100110
0x77 -> 68-1000100

can you wire it so that the device is connected to the hardware seriall?
and connect an LCD to show what you got?

It still looks to me as a timing problem with the SW Serial.

I already took a MEGA and tried out Serial1 and also Serial2… I get the same data.

Can I send the data stream to a LCD? I’ll try to figure out the code and check it…

Mystery continues....

I now try to send data from the Serial input from the arduino (monitor input over normal arduino usb connection) to the pc:
when i send 'A' i receive on the pc the two hex values '5f 79' constantely...

When i send like 20 'B' i receive mostly 'AF 1E', but also a few '2F 79' and '2F 1E'....

Now as I am using the MEGA i got the possibility to specify the settings... but it doesn't help. Same output.

  Serial2.begin(57600, SERIAL_8N1);

Ok, at least I could sort out the reason for this. When I put down the baud rate to 9600 i constantely get 'AF 1E' when sending 'B' on the arduino. The high transfer rate seems to be the problem for this. But it isn't solving the problem... :slight_smile:

I was also sending a Carriage return, when I just send the char 'A' i get '5F', for 'B' i get '2F', for 'C' i get '5E', 'D' gives me '17'...

byte val = 11111111;
Serial2.write((byte)val);

I am sending BIN values from the arduino to the pc now.

00000000 -> 00
00000001 -> 7F (1111111)
00000010 -> 3F (111111)
00000011 -> 7E (1111110)
00000100 -> 1F (11111)
00000101 -> 7D (1111101)

When i send '2' as byte (byte val = 2;) i also get the answer 3F, obviously the content seems to be really sent as byte.

I am going to try out this:
http://arduiniana.org/libraries/newsoftserial/

I states something from inverted signaling... Perhaps exactly what I need.

NewSoftSerial was integrated in SoftwareSerial.... But :slight_smile: this helped me. I do not understand the code yet, but I am able to receive ASCII values when sending chars... finally

http://forum.arduino.cc/index.php/topic,11955.0.html

Which obviously originated here:

Now I'll have to modify the code to send / receive HEX data with 57600 bauds....