[SOLVED] Serial communication problem.

Hello,

I apologize in advance for my English, I am French.

I need your help for a small problem I currently have. My Arduino Mega is connected in such a way as to recover an RS232 frame from a machine. The frame I get is in hexadecimal. However, some “0” do not seem to be read … Have you any advice?

I would like to be able to recover the entire frame without loss of “0” and be able to send it in hexadecimal in the same way.

I hope to have been clear and have posted in the good topic ^^

I thank you in advance !

Rixem: The frame I get is in hexadecimal. However, some "0" do not seem to be read

Your English is excellent.

As you have not posted your program I can only guess at the problem.

I wonder if, in fact, it is receiving the data correctly but leading zeroes are not being displayed when your print the data. For example with this code

byte testNum = 000123;
Serial,println(testNum);

you would not see the leading zeroes - and you would not expect to see them.

If that is not a correct guess then have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thank you for this quick response!

This is exactly the problem … But these “0s” are very important for my frame since the machine with which I want to communicate is sensitive to the position of the bytes. I am looking for a solution to solve this problem. Moreover, the solution of transmitting this data in ASCII is impossible. I need to send this data in hexadecimal.

Rixem:
Thank you for this quick response!

This is exactly the problem …

You still have not posted your program so how can I help?

…R

Ups, sorry… This is a very simple sketch. Just to send data!

/* Test envoi trame avance papier balise */


void setup() {

  Serial.begin(38400);
  Serial2.begin(38400);
  delay(100);

  Serial.println("Avance Papier");
  delay(100);

}

void loop() {
 

  Serial.print("FF 10 04 DA 00 02 04 00 00 00 01");
  delay(1000);
  Serial.print("\n");

}

Testenvoitramebalise.ino (288 Bytes)

Rixem: Ups, sorry.. This is a very simple sketch. Just to send data!

And what does that actually do and what do you want it to do that is different?

It seems very straightforward.

...R

  Serial.print("FF 10 04 DA 00 02 04 00 00 00 01");

Looks like ASCII to me.

void printHexLeadingZeros(byte x) {
  if(x<0x10) Serial.print("0");
  Serial.print(x, HEX);
}

MorganS: Looks like ASCII to me.

I suspect he has shown us the wrong code.

...R

Not related to the posted code but be aware that printing a number using Serial.print omits the leading zeroes.

sterretje: Not related to the posted code but be aware that printing a number using Serial.print omits the leading zeroes.

That's what I was trying to illustrate in Reply #1 :)

...R

Robin2:
That’s what I was trying to illustrate in Reply #1 :slight_smile:

…R

I was wondering why nobody had mentioned it :wink: I must stop using a cellphone and miss half the text :frowning: Karma to you :wink:

Rixem: Thank you for this quick response!

This is exactly the problem .. But these "0s" are very important for my frame since the machine with which I want to communicate is sensitive to the position of the bytes. I am looking for a solution to solve this problem. Moreover, the solution of transmitting this data in ASCII is impossible. I need to send this data in hexadecimal.

So there is no problem. The fact that Serial.print strips leading zeroes when it prints numbers is a non-issue. Serial.print anyway converts numeric variables to text (ASCII), so is the wrong choice if you don't want ASCII.

Use Serial.write instead.

byte frame[] = {0xFF, 0x10, 0x04, 0xDA, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x01};

void setup()
{
  Serial.begin(57600);
  Serial.write(frame, sizeof(frame));
}

void loop()
{
}

Use a decent terminal program that can display hex (I use RealTerm under Windows) instead of serial monitor if you want to display on a PC.

Hello,

Thank you all for your quick answers and thank you for taking the time to explain! It seems clear to me now!

However it is my fault, I should have looked at the difference between the "Serial.Write" and "Serial.print". I will try the solution of Sterretje and I come back to you if the solution has worked.

Thank you very much!

SOLVED!

Sending frame is OK. I needed a Non-Null Modem cable.

Thanks :)

@Rixem

  1. Your problem was to display the leading 0s of the upcoming numeric data on the Serial
    Monitor of Arduino IDE.

  2. You are saying that you have solved the problem, but you have not posted your codes which
    could enrich our experiences.

  3. You said that you would be testing the codes of sterretje, but you have not reported the
    outcome. Do these codes print leading 0s of the upcoming numeric data?

  4. I have worked on this issue; I have found a way of displaying leading 0s of the upcoming
    numeric data based on the following principle of the ASCII type Serial Monitor of Arduino IDE:

The Serial Monitor is designed to suppress the leading 0s of the upcoming numeric data (do we
know why?); however, it does not suppress the literal character, meaning that the Monitor will show
0 (zero) if the upcoming data byte is 0x30 (0011 0000 = ASCII code of character 0).

The implementation of this principle demands that the digits of the array of the hexadecimal data
byte (s) must be converted into their corresponding ASCII codes.

  1. Setup of my Test Bench:
    (a) When we gently press K1, the numeric data goes upward towards IBMPC.
    (b) Literate characters that we send from the Serial Monitor are displayed on the LCD.

  1. The Codes for the Setup of Step-5.
#include <LiquidCrystal.h>
#define K1 8

// APin           PC4 PC5 PC0 PC1 PC2 PC3      
LiquidCrystal lcd(A4,   A5,  A0, A1, A2, A3); //<--- APin
// LCD Signal     D-I/  E    D4  D5  D6  D7
// LCD Pin----->  4    6   11  12  13  14     R-W/(5) = tied to LL       

byte frame1[] = {0x00, 0x03, 0x34, 0xFF}; //numeric data
byte frame2[8];// ={0x30, 0x30, 0x30,0x33, 0x33,0x34, 0x46, 0x46};
byte x1;
void digitToAscii();

void setup() 
{
  Serial.begin (4800);                    //L1a:
  lcd.begin(16, 2);                       //L1b:
  lcd.setCursor (0, 0);
  pinMode(8, INPUT_PULLUP);           //L1c:
} 

void loop()
{
  Serial.flush();
  if(digitalRead(K1) == LOW)            //L2:
  {
    while (digitalRead(K1) != HIGH)     //key debouncing
      ;
    
    digitToAscii();  // convert a hex-digit into ASCII 
    Serial.write(frame2, sizeof(frame2));                 //L2A:
    Serial.print('\n');
  }
  if (Serial.available() !=0x00)         //L3:
    lcd.write(Serial.read());             //L4:
}           

void digitToAscii()
{
  for(int i = 0, j=0; i<=03; i++, j++)
  {
    x1 = frame1[i]>>4;
    if (x1 <= 0x09)
     frame2[j] = x1+0x30;
    else
     frame2[j] = x1+0x37;
  //----------------------------
   x1 = frame1[i] & 0x0F;
   if (x1 <= 0x09)
    frame2[j=j+1] = x1+0x30;
   else
    frame2[j=j+1] = x1+0x37;
   }
 }