Arduino Forum

Using Arduino => Programming Questions => Topic started by: bemin on Feb 15, 2018, 09:17 pm

Title: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 15, 2018, 09:17 pm
I am trying to use Serial.write() to write 1003 bytes on the serial port. It works fine but the issue is that since 1003 not a multiple of 64 bytes then serial port type the first 960 (15*64)  no problems but then for the remaining 43 bytes, it waits until they complete the 64 bytes then it send it out to the serialport.

how can I make sure it doesn't wait to do that, in another words how can i enable partial write

thanks
Title: Re: Writing more than 64 bytes to the serial port
Post by: Juraj on Feb 15, 2018, 09:22 pm
use Serial.flush() (https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/)

Title: Re: Writing more than 64 bytes to the serial port
Post by: knut_ny on Feb 15, 2018, 09:27 pm
Problem is in your receiver.  Does it wait until 64 bytes available ?
Title: Re: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 16, 2018, 02:17 am
Code: [Select]
[quote author=Juraj link=msg=3609142 date=1518726156]
use [url=https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/]Serial.flush()[/url]


[/quote]

[code]


Serial.write(pointer tobuffer,buffer_len)
Seral.flush()





yes I use serial flush after

Title: Re: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 16, 2018, 02:24 am
Problem is in your receiver.  Does it wait until 64 bytes available ?
my receiver application is the
Serial monitor
Title: Re: Writing more than 64 bytes to the serial port
Post by: -dev on Feb 16, 2018, 04:33 am
Quote
how can i enable partial write
There is no "problem" with the receiver.

Calling Serial.flush() will force it to wait for all characters to be sent, so don't call that if you don't want to wait.

You can check Serial.availableForWrite() (https://www.arduino.cc/en/Serial/AvailableForWrite) to see how much room is left in the transmit buffer.  Only write that many characters, and do other things until there is room for the rest of your message.  At 9600, it will take about 1000ms to send 1003 characters, so there will lots of time to do other things.

BTW, your Arduino "blocks" after the first 66 characters of the 1003 characters (64 in TX buffer, plus 2 in the UART hardware).  It has nothing to do with "multiples" of 64.
Title: Re: Writing more than 64 bytes to the serial port
Post by: sterretje on Feb 16, 2018, 04:50 am
Is this on your 32U4 based board with modified usb code?

If so, are you using the usb port or the serial port with ftdi cable?
Title: Re: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 16, 2018, 01:53 pm
There is no "problem" with the receiver.

Calling Serial.flush() will force it to wait for all characters to be sent, so don't call that if you don't want to wait.

BTW, your Arduino "blocks" after the first 66 characters of the 1003 characters (64 in TX buffer, plus 2 in the UART hardware).  It has nothing to do with "multiples" of 64.
I agree, I removed Serial.Flush. That is exactly what I am doing
Code: [Select]


size_t TotalBytes ;
    if(Serial.availableForWrite()){       
        TotalBytes = Serial.write(CalReturn,CalDataSize);
        Serial.println(TotalBytes);
       // Serial.flush();   
        delay(10);       
    }



Title: Re: Writing more than 64 bytes to the serial port
Post by: sterretje on Feb 16, 2018, 02:20 pm
If there is still space for one byte, write a whole lot?

Better to check if there is space for CallDataSize bytes before writing.

And take into account the printing of TotalBytes including cr/lf.
Title: Re: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 16, 2018, 04:11 pm
Is this on your 32U4 based board with modified usb code?

If so, are you using the usb port or the serial port with ftdi cable?
Yes it is
and not it isn't

I have a solution but I'm not sure if it ideal

I can just easy send 1024 and in my receiver application just read 1003
or i can increase the buffer size to 1003 ,  now that will still give me problem since I need that length to be variable not a constant
Title: Re: Writing more than 64 bytes to the serial port
Post by: bemin on Feb 16, 2018, 05:01 pm
Better to check if there is space for CallDataSize bytes before writing.


How can I check that ?
Title: Re: Writing more than 64 bytes to the serial port
Post by: sterretje on Feb 16, 2018, 06:26 pm
availableForWrite returns a number, not a true or false; under normal circumstances 0..64 (for the 32U4, the USB_EP_SIZE - FifoByteCount()).

Not sure if it will help though. I'm running a test on a Leonardo (as you might remember, I do not have a Micro) with serial monitor; measuring the time that it takes to send 1003 bytes. the results are (till now) consistent; between 7 and 10 milliseconds per write.
Code: [Select]
byte buffer[1003];

void setup()
{
  Serial.begin(115200);
  while(!Serial);
  Serial.println("Ready ..................");
  memset(buffer, '\r', sizeof(buffer));

  buffer[999]='1';
  buffer[1000]='0';
  buffer[1001]='0';
  buffer[1002]='3';
}

void loop()
{
  unsigned long startTime = millis();
  Serial.write(buffer, sizeof(buffer));
  Serial.println();
  Serial.print("\t"); Serial.println(millis() - startTime);
}

I've used '\r' because it's a printable character but the serial monitor does not show it.

The code did run without slowdown for 15 minutes. So I'm not sure what your problem is.

I've now added the (print of) availableForWrite
Code: [Select]
byte buffer[1003];

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Ready ..................");
  memset(buffer, '\r', sizeof(buffer));

  buffer[999] = '1';
  buffer[1000] = '0';
  buffer[1001] = '0';
  buffer[1002] = '3';
}

void loop()
{
  unsigned long startTime = millis();
  Serial.write(buffer, sizeof(buffer));
  Serial.println();
  Serial.print(Serial.availableForWrite()); Serial.print("\t"); Serial.println(millis() - startTime);

}

The beginning of the output looks like below in the serial monitor
Code: [Select]
Ready ..................
1003
19 7
1003
16 8
1003
29 8
1003
42 9
1003
55 9
1003
4 7
1003
18 8
1003
13 8
1003
17 8
1003
30 9
1003
43 9
1003
56 8
1003
5 8
1003
19 10
1003
12 7
1003
16 8
1003
29 9
1003
42 8