read more than 255 bytes

i have use arduino mega 2560.
i have send serially char[255] and output to dmd p10 module done.
but i required more than 255 bytes data i.e. char array size more than 255 bytes
char[300] my question is how to send more than 255 bytes using arduino mega 2560

Simple answer is: by the same way as you have sent 255 bytes. There is no such limit on serial port.
However, you could be more specific and to post your code.

You may have defined your input buffer with a byte quantity for its size.

my code for read 255 bytes

/*read serial data

char serIn[255];

void setup()
{

Serial.begin(9600);

}

void loop()
{

if(Serial.available()) {
int chars_in = 0;
// void serialFlush(){
while (Serial.available()>0 && chars_in<=254){
serIn[chars_in] = Serial.read();
Serial.write( byte(serIn[chars_in]));
chars_in++;
}

serIn[chars_in+1] = '\0';

}

i required more than 255 bytes,what modify in program.

i required more than 255 bytes,what modify in program.

The size of the array and the limit value in the while loop

As all your code does with the data received is to write it out again then do you actually need to save it in an array ?

How many bytes are you aiming to read ?

Something like this:

const int SerialBufferSize = 300;
char serIn[SerialBufferSize];

void setup()
{
  Serial.begin(9600);
}

void loop()
{

  if (Serial.available())
  {
    int chars_in = 0;
    //  void serialFlush(){
    while (Serial.available() > 0 && chars_in < SerialBufferSize)
    {
      serIn[chars_in] = Serial.read();
      Serial.write( byte(serIn[chars_in]));
      chars_in++;
    }

    serIn[chars_in + 1] = '\0';
  }
}

at that BAUD rate you will not read all the bytes you sent before you find that the buffer is empty (unless you wait until all are sent, but then your input buffer will have overflowed) adddelayMicroseconds(1200); within your while loop.

Deva_Rishi:
at that BAUD rate you will not read all the bytes you sent before you find that the buffer is empty (unless you wait until all are sent, but then your input buffer will have overflowed) adddelayMicroseconds(1200); within your while loop.

Better still, don't add any kind of delay, and handle the serial properly.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The examples asume the max number of chars received will be 32 but that can easily be changed. If you want to receive more than 255 chars (or bytes) be sure to change the datatype from byte to int.

...R

AWOL:
Better still, don't add any kind of delay, and handle the serial properly.

it all depends what you consider properly, an aSync task is not always better (it depends what you want to do with the result and what part it takes within your program.) Noone ever asks how i get to 1200uS ... hmmm.

On a microcontroller asynchronous is always better than blocking...

Noone ever asks how i get to 1200uS .

Because no-one cares for your cycle-wasting "solution"?

AWOL:
Because no-one cares for your cycle-wasting "solution"?

Honestly i was just making sure that the code that was given in a previous post was actually going to work. But wasteful or not, there are circumstances where just coming back to see if more bytes are available might not work. definitely if you plan to send more bytes then the Serial buffer can hold (unless you increase that in the library of course) If the code that gets executed in between checks exceeds say 100 ms (quite a lot i do agree) the buffer gets overrun. If let's say you'd use swSerial (what other excuse does one have for using such a low BAUD-rate) and in the other part of the code interrupts get turned of (for instance for sending a bit-banged signal of some kind, WS2812 or something) and there are quite a few other reasons that reading the whole lot in one go is the preferred method. Not to mention the sheer simplicity of the resulting code, easy to understand for the beginner (though Robin's code is great don't get me wrong) Sometimes wasting cycles is all we do, but i prefer to waste my cycles gently...

at that BAUD rate you will not read all the bytes you sent before you find that the buffer is empty

That's OK, because johnwasser's code is also checking for the 300-byte buffer to be full before it stops filling it. It's neither necessary or desirable to wait for the (hidden) serial input buffer to collect more than 1 byte...
The OP's code has a bug or two:

void loop() {
if(Serial.available()) {  
   int chars_in = 0;    // RESETS char_in each time through loop() !
   while (Serial.available()>0 && chars_in<=254){  // while loop exits when available is 0.
       serIn[chars_in] = Serial.read();      
       Serial.write( byte(serIn[chars_in]));
       chars_in++;
   }  
   serIn[chars_in+1] = '\0';

There are a lot of times when you're reading serial input, and you really DON'T have anything to do until it has all been read. If I'm going to block reading Serial input, I tend to prefer to make that blocking really explicit. It makes the main code path much clearer (not "cluttered" with "available" calls), and makes it more parallel to the sort of code you'd see in a non-embedded program:

char readc_blocking() {
   while (Serial.available() < 1)
      ; // spin/block, waiting for data
   return Serial.read();
}
void loop() {
   for (count=0; count < BUFFER_SIZE; count++) {
      buffer[count] = readc_blocking();
   }
}

lectrotek:
serIn[chars_in+1] = '\0';

Oops.

johnwasser:
serIn[chars_in + 1] = '\0';

:o

according to johnwasser's code,i have implemented code for serial data to dmd display but on display only 255 character coming . my code is

#include <DMD.h>
#include <SPI.h>
#include <TimerOne.h>
#include "SystemFont5x7.h"
//#include "Arial_black_16.h"
#define DISPLAYS_ACROSS 25
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

const int SerialBufferSize = 300;
char serIn[SerialBufferSize];

void ScanDMD()
{
dmd.scanDisplayBySPI();
}
void setup()
{
Timer1.initialize( 5000 );
Timer1.attachInterrupt( ScanDMD );
dmd.clearScreen( true );

Serial.begin(9600);
}

void loop()
{

if (Serial.available())
{
int chars_in = 0;
// void serialFlush(){
while (Serial.available() > 0 && chars_in < SerialBufferSize)
{
serIn[chars_in] = Serial.read();
Serial.write( byte(serIn[chars_in]));
chars_in++;

}

serIn[chars_in + 1] = '\0';
}

dmd.selectFont(SystemFont5x7);
dmd.drawString(0, 0, serIn,150, GRAPHICS_NORMAL );
dmd.drawString(0, 8, serIn+150, 300, GRAPHICS_NORMAL );

delay(1000);
}

what modify in program to receive more than 255 bytes and output to dmd p10 module.

i think serial receive buffer size is 1 byte so, it receive 255 bytes at time

can i change serial receive buffer size .
please send modified program for this.

Please, use code tags.
Button [</>].

lectrotek:
what modify in program to receive more than 255 bytes and output to dmd p10 module.

i think serial receive buffer size is 1 byte so, it receive 255 bytes at time

can i change serial receive buffer size .

The Arduino serial input buffer is 64 bytes long and that should be plenty big enough to receive 500 (or 1000) bytes if you have space to store them all.

Have you carefully studied the code in the link I gave you in Reply #8 ?

...R

westfw:

char readc_blocking() {

while (Serial.available() < 1)
      ; // spin/block, waiting for data
  return Serial.read();
}
void loop() {
  for (count=0; count < BUFFER_SIZE; count++) {
      buffer[count] = readc_blocking();
  }
}

Now this is pretty and understandable, still a 'timing-out' in the blocking would be 'better' if not all 300 bytes are received (as one would hope and expect) we end up in an infinite loop. When sending data without terminating characters (which is the only way if it's "RAW") requires at least an 'Error-check' for it's size.

[quote author=Coding Badly date=1544753521 link=msg=3980910]
Oops.

:o [/quote]
Oops. I should have changed:

char serIn[SerialBufferSize];

to

char serIn[SerialBufferSize+1];  // Add room for a null terminator