Go Down

Topic: Writing more than 64 bytes to the serial port  (Read 674 times) previous topic - next topic

bemin

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


knut_ny

Problem is in your receiver.  Does it wait until 64 bytes available ?
Ny

bemin

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


bemin

Problem is in your receiver.  Does it wait until 64 bytes available ?
my receiver application is the
Serial monitor

-dev

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() 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.
Really, I used to be /dev.  :(

sterretje

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?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bemin

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);       
    }




sterretje

#8
Feb 16, 2018, 02:20 pm Last Edit: Feb 16, 2018, 02:22 pm by sterretje
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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bemin

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

bemin

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


How can I check that ?

sterretje

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
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up