increasing read serial speed

Hi all,

I'm trying to communicate over serial with the Arduino.
For my project I need to read a single integer over the serial, and then respond with 2 integers.
I've succeeded in doing this, but am a bit struggling with getting it up to the speed I require: > 200 msg/sec.
I am getting about 80 msg/sec with attached code in the serial monitor of the IDE by just holding "Enter"

Is there something I can do to speed up the code?

Thanks,

int temp=0;
int i=0;

void setup() {
  Serial.begin(250000); // set the baud rate
}
void loop() {

  temp=micros();
  if (Serial.available() > 0) { 
    String inByte = Serial.readStringUntil('\n'); 
    temp=micros()-temp;
    Serial.print(i); 
    Serial.print(','); 
    Serial.println(temp); 
    i=i+1;
  }
}

don't send it that way.

You'll find here some tutorials about using Serial communication.

KingBee:
Is there something I can do to speed up the code?

Your example is very unrealistic to the extent that I can't see any value in trying to optimize it.

If you describe the project you are trying to implement it will be much easier to help. Someone may see a very different solution to what you have in mind.

Some food for thought ...

The USB system is not efficient when small packets of data (< 64 bytes) are being sent.

readString() is a blocking function that prevents the Arduino from doing other things until it detects the end-marker or it times out.

Although I advocate sending data in human readable form as it assists debugging it is much better to send data in binary form (or at least in some compressed form) if speed is essential.

...R
Serial Input Basics - simple reliable ways to receive data.

You could switch to binary and SPI and send/receive at 8 Mb/s (1 MB/s or 500,000 integers per second).

Does the code know what two integers to respond with before it receives the integer? If so you can send in parallel with receiving and exchange close to 250,000 messages per second.

Holding down the Enter key will just tell you the key-repeat speed of the key. Your Serial code spends most of its time waiting for the key.

Try to send a file - use something other than Serial Monitor on the PC side. I like RealTerm.

Thanks for the replies all.

Wrote a python code to send the files, speed is indeed better.
Getting up to 400 msg/sec now, which is decent enough for my purposes.
Might need it to increase it at a later stage.

Thanks,