Skip to main content
Fix string termination.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

It seems the arduino's wrappers for serial API's are very slow

They aren't. There are some functions that depend on timeouts: these can indeed be very slow. However, if you read the ports in a non blocking fashion, everything should be fast. Non blocking basically means never waiting, and never trying to read more bytes than what is currently available(). For example:

const int buffer_size = 64;  // tune to taste

char buffer1[buffer_size];
int size1 = 0;
// same for buffer2, size2, etc.

void loop() {
    while (Serial1.available()) {
        char c = Serial1.read();
        if (size1 < buffer_size - 1)
            buffer1[size1++] = c;  // buffer the incoming character
        if (c == '\n') {  // found end-of-message
            buffer1[size1] = '\n';'\0';  // terminate the string
            Serial.print(buffer1);  // send through USB
            size1 = 0;  // get ready for next message
        }
    }
    // then the same for Serial2, ...
}

If you know a bit about object oriented programming, you can easily rewrite this in a more elegant way, with no repeated code.

It seems the arduino's wrappers for serial API's are very slow

They aren't. There are some functions that depend on timeouts: these can indeed be very slow. However, if you read the ports in a non blocking fashion, everything should be fast. Non blocking basically means never waiting, and never trying to read more bytes than what is currently available(). For example:

const int buffer_size = 64;  // tune to taste

char buffer1[buffer_size];
int size1 = 0;
// same for buffer2, size2, etc.

void loop() {
    while (Serial1.available()) {
        char c = Serial1.read();
        if (size1 < buffer_size - 1)
            buffer1[size1++] = c;  // buffer the incoming character
        if (c == '\n') {  // found end-of-message
            buffer1[size1] = '\n';  // terminate the string
            Serial.print(buffer1);  // send through USB
            size1 = 0;  // get ready for next message
        }
    }
    // then the same for Serial2, ...
}

If you know a bit about object oriented programming, you can easily rewrite this in a more elegant way, with no repeated code.

It seems the arduino's wrappers for serial API's are very slow

They aren't. There are some functions that depend on timeouts: these can indeed be very slow. However, if you read the ports in a non blocking fashion, everything should be fast. Non blocking basically means never waiting, and never trying to read more bytes than what is currently available(). For example:

const int buffer_size = 64;  // tune to taste

char buffer1[buffer_size];
int size1 = 0;
// same for buffer2, size2, etc.

void loop() {
    while (Serial1.available()) {
        char c = Serial1.read();
        if (size1 < buffer_size - 1)
            buffer1[size1++] = c;  // buffer the incoming character
        if (c == '\n') {  // found end-of-message
            buffer1[size1] = '\0';  // terminate the string
            Serial.print(buffer1);  // send through USB
            size1 = 0;  // get ready for next message
        }
    }
    // then the same for Serial2, ...
}

If you know a bit about object oriented programming, you can easily rewrite this in a more elegant way, with no repeated code.

Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

It seems the arduino's wrappers for serial API's are very slow

They aren't. There are some functions that depend on timeouts: these can indeed be very slow. However, if you read the ports in a non blocking fashion, everything should be fast. Non blocking basically means never waiting, and never trying to read more bytes than what is currently available(). For example:

const int buffer_size = 64;  // tune to taste

char buffer1[buffer_size];
int size1 = 0;
// same for buffer2, size2, etc.

void loop() {
    while (Serial1.available()) {
        char c = Serial1.read();
        if (size1 < buffer_size - 1)
            buffer1[size1++] = c;  // buffer the incoming character
        if (c == '\n') {  // found end-of-message
            buffer1[size1] = '\n';  // terminate the string
            Serial.print(buffer1);  // send through USB
            size1 = 0;  // get ready for next message
        }
    }
    // then the same for Serial2, ...
}

If you know a bit about object oriented programming, you can easily rewrite this in a more elegant way, with no repeated code.