Serial.write VS EEPROM.write

Hi,

I want to send this message to a device (PIC18F2525) through a serial communication (using digital pins 16 and 17) from Arduino Mega.

The message is this one:

uint8_t sendByte[8];
void setup() {
  Serial.begin (9600);
}
void loop() {
sendByte[0] = (uint8_t)ETX;
    sendByte[1] = (uint8_t)STX;
    sendByte[2] = (uint8_t)1; //sendByte[2]= (uint8_t)1;
    sendByte[3] = (uint8_t)DLE;
    sendByte[4] = (uint8_t)5; //sendByte[4]= (uint8_t)5;
    sendByte[5] = (uint8_t)(256 - 5); //sendByte[5]= (uint8_t)(256-5);
    sendByte[6] = (uint8_t)ETX;
    sendByte[7] = (uint8_t)ETX;    
    Serial.write(sendByte, sizeof(sendByte));
}

Unfortunately, the device does not return anything (checking if(Serial.available()>0)). When I run that code, at the Serial Monitor appears this ⸮

Should I use another function to send the data (like EEPROM.Write) instead of Serial.write()? Which function to read should I use (Currently using Serial.read())?

Thanks

Pins 16 & 17 are the Serial2 pins.

Which function to read should I use (Currently using Serial.read())?

How are the serial lines connected to the receiving device? Does the receiving device have more than 1 hardware serial port?

Should I use another function to send the data (like EEPROM.Write)

Why would you use EEPROM.write to send serial data?

const int RX_RF = 16; // RX from the Micro RF
const int TX_RF = 17; // TX from the Micro RF

 pinMode(RX_RF, OUTPUT); digitalWrite(RX_RF, HIGH);
 pinMode(TX_RF, INPUT_PULLUP);

You do not need that code to set up Serial2 (it may be detrimental). Serial2.begin() does the setup of the port. What is the receiver sending back?

Post the receiver code.

I removed those lines and at least I receive data from Serial2 now. However, I cannot print that data correctly to the Serial Monitor.

I cannot print that data correctly to the Serial Monitor.

What does that mean? No data shows up? Data garbled? Post a sample of the received data.

Most of those characters are non-printing so you aren't going to see much. But if you copy and paste what you posted in your first message:

at the Serial Monitor appears this ⸮

Note that it shows all but one of the characters you sent. The DLE is missing because you defined it to be 10 (linefeed) which the monitor seems to be ignoring.
The true DLE character is not decimal 10, it is hex 10 (0x10 or decimal 16).

Pete

I have to collect the data in a struct. This is the C version of the struct.

typedef struct 
{
   int8 Model;
   int8 MayorRelease;
   int8 Release;
   int8 Address1;
   int8 Address2;
   int8 Address3;
   int8 AddressRis1;
   int8 AddressRis2;
   int8 AddressRis3;
   int8 Band;
   int8 FrequencyChannel;
}TCfgRxVer;
TCfgRxVer CfgRxVer;

And this my Arduino code

//typedef struct
{
  uint8_t Model;
  uint8_t MayorRelease;
  uint8_t Release;
  uint8_t Address1;
  uint8_t Address2;
  uint8_t Address3;
  uint8_t AddressRis1;
  uint8_t AddressRis2;
  uint8_t AddressRis3;
  uint8_t Band;
  uint8_t FrequencyChannel;
} TCfgRxVer;
TCfgRxVer CfgRxVer;


typedef union {
  TCfgRxVer a;
  uint16_t b;
} u_structTable;

byte val1, val2;
  if(Serial2.available() > 0)
  {    
    val1= Serial2.read();
    val2= Serial2.read();
  }  
  u_structTable x;
  x.b = val1  | (val2<<8);
  Serial.println((byte)x.a.Model);

The output is

1
0
0
0
0
0
0
0
0
0
0
0
0
....

How can I collect all the struct data from the serial device?

  if(Serial2.available() > 0)

This only guarantees you that there is at least ONE character in the buffer, but you read two of them.

Change it to:

  if(Serial2.available() > 1)

Pete