Serial Communication problem

Hello guys, I'm working on this project in which I need to communicate Arduino MEGA with a Unitronics HMI to send a 32-bit message.

I'm communicating though the Serial1 of Arduino with a Serial-RS232 cable, into the RS232 port the HMI.
The communication works, but the data has sort of a format problem. See, the Unitronics system works with the ASCII table (ASCII Table), and also Arduino, but when I send the character 'A' from Unitronics to Arduino, Arduino receives 95 (instead of 65 decimal), the same happens when I send 'A' from Arduino to Unitronics, Unitronics receives 95.

I would be glad if someone can help me with that.

I'm using the Serial1.print function, but I have also tried with Serial1.write and the same happens.

Please, post your codes.

joaovitorcpm:
I'm communicating though the Serial1 of Arduino with a Serial-RS232 cable, into the RS232 port the HMI.

Do you mean a cable that converts between TTL and RS232 signal levels?

You should NOT connect RS232 signal levels to an Arduino as they may damage the Arduino.

...R

Robin2:
Do you mean a cable that converts between TTL and RS232 signal levels?

You should NOT connect RS232 signal levels to an Arduino as they may damage the Arduino.

...R

yes, the cable converts RS232 to TTL level, I've checked with an oscilloscope

GolamMostafa:
Please, post your codes.

byte incomingByte = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);

delay(50);
Serial.print('B');
Serial1.print('B');
}

void loop() {
delay(50);
Serial.print('B');
Serial1.print('B');
// put your main code here, to run repeatedly:
if (Serial1.available() > 0) {
incomingByte = Serial1.read();
Serial1.print(incomingByte);
Serial.print(incomingByte);
Serial.print('\n');
}
}

1.png shows the received message in Unitronics when I send 'A' with Arduino

  1. png shows the sent message in Unitronics when I send 'A' from Unitronics TO Arduino

@OP

1. Upload the following sketch (your one with some adjustment) in your MEGA.
2. Bring in the Serial Monitor (SM) of MEGA at Bd 9600.
3. Put Unitronics in receive mode.
4. Place the cursor at the InputBox of the SM of MEGA.
5. Enter A from Keyboard of the PC.
6. Click on the Send button of the SM.
7. Observe that A has appeared on the OutputBox of SM.
8. What have you got on the screen of the Unitronics? Are you getting A or 65 or what?

//byte incomingByte = 0;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);

  //  delay(50);
  //Serial.print('B');
  //Serial1.print('B');
}

void loop() 
{
  //delay(50);
  //Serial.print('B');
  //Serial1.print('B');
  // put your main code here, to run repeatedly:
  //if (Serial1.available() > 0)
  if(Serial.available()>0)
  {
    char x = Serial.read();
    Serial1.print(x); 
    Serial.print(x);
  }
  /*
  {
    incomingByte = Serial1.read();
    Serial1.print(incomingByte);
    Serial.print(incomingByte);
    Serial.print('\n');
  }
 */
}

I had a similar problem with 232 yesterday. In my case the cause was that the "interface module" inverted the signal. So I had to put a transistor to invert it again...

Can;t say if it is the same in your case

GolamMostafa:
@OP

1. Upload the following sketch (your one with some adjustment) in your MEGA.
2. Bring in the Serial Monitor (SM) of MEGA at Bd 9600.
3. Put Unitronics in receive mode.
4. Place the cursor at the InputBox of the SM of MEGA.
5. Enter A from Keyboard of the PC.
6. Click on the Send button of the SM.
7. Observe that A has appeared on the OutputBox of SM.
8. What have you got on the screen of the Unitronics? Are you getting A or 65 or what?

//byte incomingByte = 0;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);

//  delay(50);
  //Serial.print('B');
  //Serial1.print('B');
}

void loop()
{
  //delay(50);
  //Serial.print('B');
  //Serial1.print('B');
  // put your main code here, to run repeatedly:
  //if (Serial1.available() > 0)
  if(Serial.available()>0)
  {
    char x = Serial.read();
    Serial1.print(x);
    Serial.print(x);
  }
  /*
  {
    incomingByte = Serial1.read();
    Serial1.print(incomingByte);
    Serial.print(incomingByte);
    Serial.print('\n');
  }
*/
}

Okay, i tried it.

Result: I dindn't work. When I disconnect RX and TX from Arduino, the A appears on SM. But with it connected, it appears that one communication affects the other or it makes Arduino bug, because it doesn't event show A on SM.

Post the the spec / datasheet (or link) for the serial interface on the "HMI".

joaovitorcpm:
Okay, i tried it.

Result: I dindn't work. When I disconnect RX and TX from Arduino, the A appears on SM. But with it connected, it appears that one communication affects the other or it makes Arduino bug, because it doesn't event show A on SM.

Now, you know where is the problem. It is a combined job of you (in particular) and Forum (in general) to figure out -- what is the problem.

There are no such things as RX and TX in MEGA. What we have in MEGA are: (RX0, TX0) for Serial0 PORT, and it has dedicated connection with Serial Monitor/IDE; (RX1, TX1) for Serial1 PORT; (RX2, TX2) for Serial2 Port; (RX3, TX3) for Serial3 Port.

Please, post an image of the DB9-pin connector of your Unitronics. Also, post the connection diagram that you have made among MEGA, MAX232 (TTL <--->RS232), and Unitronics.

Check again the pins of MEGA with which you have connected the TTL side of your TTL<--->RS232 converter. It looks like (from your utterance of RX, TX) that you have connected the TTL side with RX0, TX0 pins of MEGA.

OP, are you certain the Uni is setup for 9600/N/8/1?

That fact that it's happening at both ends suggests a bit rate or physical layer problem.