Why i can't use Serialprint instead of serial write

Hi Experts,
Currently, i am trying to read data from the MODBUS sensor through Arduino mega, My problem is if i use serial.write function the Modbus(RS-485) sensor return data then if i use serial print function instead of serial write that sensor return nothing
i have shown my code here for your reference, i was searching in google regarding this but i don’t get the reason that’s why i ask question to this forum,

Serial.write send Binary data to serial that’s why i am used serial print(value, BIN) but i get the same problem,

kindly help to solve this problem.

#include <SoftwareSerial.h>

#define Pin13LED 13

byte byteSend;

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println(“MODBUS…”);
}
void loop()
{

while (Serial1.available())
{
byteSend = Serial1.read(); // Read the byte
Serial.print(“RDATA:”);
Serial.println(byteSend, HEX);
}
delay(1000);
byte request = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
int i=0;
for(i=0; i<sizeof(request); i++)
{
// Serial1.write(request*);*
_ Serial1.print(request*,BIN);_
_
//Serial1.println("");_
_
}_
_
}*_

Print sends the byte as text. Even if you use BIN, it will emit characters '1' and '0'.

thanks for the reply then how can i use serial print instead of serial write

You can’t. That’s why serial.write exists.

explain to us what is the reason for you to switch from serial.write to serial.print?

best regards Stefan

for a testing purpose, i am used Arduino but in the future, i am going to implement the steps on another microcontroller that controller must be not supported Arduino IDE that's why i want to know the exact reason for why we can't use serial print instead of serial write if i found that answer my next update will be easier that's the only reason

wildbill:
You can't. That's why serial.write exists.

Actually, I don't think this is true. You can print a string and in there you can place escape sequences to specify the bytes you want. Look: Escape sequences in C - Wikipedia it's C#, but they're the same.

kannannatesh:
for a testing purpose, i am used Arduino but in the future, i am going to implement the steps on another microcontroller that controller must be not supported Arduino IDE that's why i want to know the exact reason for why we can't use serial print instead of serial write if i found that answer my next update will be easier that's the only reason

But if the microcontroller is not supported by Arduino, then you can't use Serial.print on whatever platform you move to, right? That means it's moot to worry about using or not using Serial.write.

wildbill:
Actually, I don't think this is true. You can print a string and in there you can place escape sequences to specify the bytes you want. Look: Escape sequences in C - Wikipedia it's C#, but they're the same.

Would not work in the case of 0x00, which would be interpreted as the end of the string. Serial.write does take an optional parameter for the number of bytes to write, if eliminating the for loop is desired.

christop:
But if the microcontroller is not supported by Arduino, then you can't use Serial.print on whatever platform you move to, right? That means it's moot to worry about using or not using Serial.write.

yes but my microcontroller must-have a function like serial print to print string in serial that's why i am asking how to write the program for serial print what function executed by serial write

You have a misunderstanding of data formats and how they are represented. You need to understand the difference between Serial.write, which writes raw bytes to the serial port as a sequence of bits, and print which formats those bytes first as ASCII (in some human readable format) and prints those ASCII characters as a sequence of raw bytes (themselves sent as a sequence of bits).

Raw byte data going to the serial port is NOT the same stream of bytes as human readable ASCII generated by Serial.print(x, BIN).

This code…

byte request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3}; 
for(int i=0; i<sizeof(request); i++)
{
  Serial1.write(request[i]);
}

Or this code…

byte request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3};
Serial1.write(request, sizeof(request));

Sends the following bytes of raw data (in binary form) over the serial link…
0x01 0x03 0x00 0x75 0x00 0x04 0x55 0xD3

Meanwhile this code…

byte request[] = {0x01, 0x03, 0x00, 0x75, 0x00, 0x04, 0x55, 0xD3}; 
for(int i=0; i<sizeof(request); i++)
  Serial1.print(request,BIN);
}

Sends the following ASCII data over the serial link (spaces added for reading clarity, they aren’t actually present in the output)…
1 11 0 1110101 0 100 1010101 11010011

ASCII data is transferred as a sequence of bytes (‘0’=0x30, ‘1’=0x31), so representing the above string in the same format as the first example the bytes that are actually transferred are…
0x31 0x31 0x31 0x30 0x31 0x31 0x31 0x30 0x31 0x30 0x31 0x30 0x31 0x30 0x30 0x31 0x30 0x31 0x30 0x31 0x30 0x31 0x31 0x31 0x30 0x31 0x30 0x30 0x31

i have already checked the three steps what you have shown in your code, i have attached my code for your reference, i get a response from my Modbus meter when i am using the serial write function

Serial1.write(request, sizeof(request));

or

int i = 0;
for (i = 0; i < sizeof(request); i++)
{
Serial1.write(request*);*

  • }*
    but i can’t get a response when i use
    like
    void loop()
    {
    int i = 0;
    for (i = 0; i < sizeof(request); i++)
    {
    Serial1.print(request*,BIN);*
    * }*
    * {[/sup]*

Hi kanna,

do yuo read the answers that have been posted?
The explanation has already been given. as a anlog picture:

imagine you are asking an english speaking person who is only able to speak english and no other language .

You ask them "Quelle heure est-il?"

He will look at you and have a big question-mark in his face what`?

You used the exact same mouth as always to say the words "Quelle heure est-il"
But this only english-speaking person will understand nothing because it is french

It is the same principle with seril.write and serial.print

serial.print uses the exact same serial connection but with a language your device does not understand.

meilleures salutations
que tengas un buen día
そして、与えられた答えを注意深く読んでください

Hi StefanL38.
Kindly try to understand my replies, I just replied who is trying to give an answer to my question. i was concluded when i read this below link

if i send 0x01 data to serial write that write 00000001

but serial print gives 00000000, 00000001 that's why we can't use serial print function instead of serial write,

regards
Kannan

but serial print gives 00000000, 00000001

Is that really what it sends ?

i am not sure because i don’t have a DSO to check the bytes