arduino serial send block of bytes instead of byte by byte

Hi Guys,

I am trying to get a block of multiple HEX in one block, and send them in one go through softwareserial.

If i monitor the serial port, i can see that every HEX part is send seperate. See my attachment.

Instead of

0xCC
0x02
0x3C
0x55
0x5F

It should be 0xCC 0x02 0x3C 0x55 0x5F

My test sketch is:

#include <SoftwareSerial.h>

int i = 0;

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

uint8_t enableTerminal[] = {0xCC, 0x02, 0x3C, 0x55, 0x5F};

void setup() {
   pinMode(14, OUTPUT);
   pinMode(10, INPUT);
   

  Serial.begin(57600);
  while (!Serial) {
    ; // 
  }

  mySerial.begin(57600);


 
}

void loop() { 


if (mySerial.available() > 0) 
{
    Serial.write(mySerial.read());

}
   if(i>30000) 
  {
    mySerial.write(enableTerminal, sizeof(enableTerminal));
    i=0;
  }
  i++;

}

If i monitor the serial port

Using what application? The Serial Monitor application has no idea what to do with the binary data you are sending to the serial port.

Your assumption that the Arduino is doing something wrong is incorrect. It is shoving bytes out the serial port one after the other, with nothing between them. Whatever application you are using to see the data is adding the carriage returns and line feeds.

Hi,

First of all thank you for your interest and looking to my thread.

Here i made a capture of the original application ( see red marking). As you can see it sends the byte string in one block. The first screenshot is from the arduino, where it sends all data in seperate blocks.

The fact it says "cc 02 3c 55 5f" over there says absolutly NOTHING about how it's send... I agree with Paul, the Arduino code does not add newlines...

Try and run

#include <SoftwareSerial.h>

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

void setup() {
  Serial.begin(57600);
  while (!Serial) //if you use a Uno(-ish) this line is useless

  mySerial.begin(57600);
}

void loop() {
  if (mySerial.available() > 0){
    byte in = mySerial.read();
    if(in == '\n'){
      Serial.print(F("I knew it, a new line!");
    }
    else if(in == '\r'){
      Serial.print(F("I knew it, a carriage return!");
    }
    else{
      Serial.write(mySerial.read());
    }
  }
}

Hi septillion, i monitor the output of the arduino. Not the input
So i miss the purpose of your sketch ....

Let me clarify it better. I want to send with an arduino mega and a max3232 to an byte array to a device. That is not working. I can see on the computer while i monitor the serial port incoming part that my code is sending the byte array in chunks instead of one block.

I thought mySerial was coming from some application and you monitored (in serial monitor) the return via the real hardware serial?

It would be nice that the ide has such rich feature serial monitoring part :slight_smile: !!!

I thought that was the sending program....

If it's some sort of receiving / monitoring program, what makes you say it sends it separate? I just see 0xcc 0x02 0x3c 0x55 0x5f with nothing between it...

But there doesn't seem to be any or in your data capture.
And in the first image you posted the data arrives all in the same second (probably much smaller than one second).

What does it matter - if the data comes in with no delimiters - you still get the data correctly in the application? Are there any spurious characters in that data stream?

It looks as though you are monitoring all conceivable activity on the serial port, not just the data arrival.

Dan95:
But there doesn't seem to be any or in your data capture.
And in the first image you posted the data arrives all in the same second (probably much smaller than one second).

What does it matter - if the data comes in with no delimiters - you still get the data correctly in the application? Are there any spurious characters in that data stream?

It looks as though you are monitoring all conceivable activity on the serial port, not just the data arrival.

The device that has to act on it matters that it are 5 different messages instead of one.

Are there any spurious characters in that data stream? - NO

It looks as though you are monitoring all conceivable activity on the serial port, not just the data arrival. - Yes, thats how reverse engineering starts :slight_smile:

OF COURSE there are 5 separate messages! Serial can only send a byte at a time. But there is NOTHING separating the data other then being it 5 packages... They are just 5 received bytes, in order, without ANYTHING between them.

septillion:
OF COURSE there are 5 separate messages! Serial can only send a byte at a time. But there is NOTHING separating the data other then being it 5 packages... They are just 5 received bytes, in order, without ANYTHING between them.

As you can see that the capture of the original application it is able to pack the 5 bytes in one go, instead of 5 times 1

Keep in mind that one day, your send data may contain 0x0D or other ASCII control characters.
Serial monitor has no way of understanding if these are binary data or ASCII chars.
Your sending code is only half the issue, your receiving code needs to understand 'what' is being sent in what context (binary data or text).

Nonsens, it's impossible... Serial is 8 bit aka 1 byte at a time (okay, 7 and 9 bit also exists but rarely used).

And I don't see it in the capture... Just see the data one, in the red circle...

septillion:
Nonsens, it's impossible... Serial is 8 bit aka 1 byte at a time (okay, 7 and 9 bit also exists but rarely used).

And I don't see it in the capture... Just see the data one, in the red circle...

I am glad you dont see the difference. I can tell you the data is different. The device doesnt response on the data coming out of the arduino. So keep saying nonsens, thank you :)!

lastchancename:
Keep in mind that one day, your send data may contain 0x0D or other ASCII control characters.
Serial monitor has no way of understanding if these are binary data or ASCII chars.
Your sending code is only half the issue, your receiving code needs to understand 'what' is being sent in what context (binary data or text).

I have to tell it again. I dont use the serial monitor on the arduino side. There is nothing ever going on there. I only have to send the command to activate. Also, i put a link in previous comment, that i am not the only one that is seeing this behavior on the serial port part.

mikeynl:
I am glad you dont see the difference. I can tell you the data is different.

Then show it! All you posted what that single image in which I can see the data being receive once. As a nice block of 5 bytes with nothing in between them...

mikeynl:
The device doesnt response on the data coming out of the arduino.

Still like a million other possibilities. For example connection error or it doesn't like the timing of SoftareSerial... (which is not as good as the real serial)

mikeynl:
So keep saying nonsens, thank you :)!

I'm certainly not. :wink: Do you even know how serial (or to be more precise here, TTL serial) works? Have a look and see for yourself it's always going to be blocks of 8-bits aka single bytes. O, and don't forget SoftwareSerial.write() which points to the HardwareSerial.write()

I know how it works, but i see a difference in both. One is making 5 part, one is putting it in one go. If anyone can tell me why, that i can look for an solution. And as long the device that i want to get a live doesnt respond, it means something is wrong at the arduino part. And as you can see it on the screenshots, there is a difference.

Why dont you respond on the other thread i mentioned ? I am not the only one who see this behavior. Also switching from software to hardware serial make no differences. Same behavior.

But please let stop this discussion, i am looking forward for someone that has an idea what is going on and push me in the right direction. I prefer that instead of an e-size measuring comparisation how good the other knows about something.....

Why dont you respond on the other thread i mentioned ?

What other thread?

cattledog:
What other thread?

i think my reply got stuck in the 5 minutes wait before post again

http://forum.arduino.cc/index.php?topic=173123.0

mikeynl:
Hi Guys,

I am trying to get a block of multiple HEX in one block, and send them in one go through softwareserial.

If i monitor the serial port, i can see that every HEX part is send seperate. See my attachment.

Instead of

0xCC
0x02
0x3C
0x55
0x5F

It should be 0xCC 0x02 0x3C 0x55 0x5F

My test sketch is:

Don't worry about it, its an artifact of the serial monitor program which is doing more than doing blocking reads.