Simultaneously output to multiple serial ports

Sure, just write or print the same data to all 3, like this

Code:
Serial1.write(myArray, sizeof(myArray));
Serial2.write(myArray, sizeof(myArray));
Serial3.write(myArray, sizeof(myArray));

Each port has a transmit buffer in memory, so your writes go quickly into the buffer and then the hardware actually sends the data slowly as the port's baud rate allows.

If the default buffer size isn't large enough for all your outgoing data (which causes print() or write() to have to wait), you can increase it with addMemoryForWrite(). Documentation here:

 
I'm doing a lot of serial writes to USB and three other real ports and it seem to slug down my program. What are good settings for buffer, size? What are the min and max?
 
Ok i changed the buffer size and the prints are quite fast but the program is slow in the ADC function i guess...

Can you see anything that would be slowing it down to about 1 second for the function to run?

Code:
// Function to read pressures and temperature from the ADS1118
void readPressuresAndTemperature(float pressures[4], float &temperature) {
    float mV, mA;

    // Set ADS1118 configuration
    ads1118.setSamplingRate(ads1118.RATE_8SPS);
    ads1118.setFullScaleRange(ads1118.FSR_2048);

    // Read the temperature (in Celsius)
    float tempC = ads1118.getTemperature();
    temperature = tempC * 9.0 / 5.0 + 32.0; // Convert to Fahrenheit

    for (int i = 0; i < 4; i++) {
        // Map the loop index to the correct ADS1118 input channels
        int channel;
        switch (i) {
            case 0: channel = ads1118.AIN_0; break;
            case 1: channel = ads1118.AIN_1; break;
            case 2: channel = ads1118.AIN_2; break;
            case 3: channel = ads1118.AIN_3; break;
        }

        // Select the input channel and allow settling time
        ads1118.setInputSelected(channel);
        delayMicroseconds(50); // Allow settling time

        // Discard the first reading and take the second
        ads1118.getMilliVolts(); // Discard first reading
        mV = ads1118.getMilliVolts(); // Read stabilized value

        // Calculate current (mA) based on calibration values
        switch (i) {
            case 0: mA = mV * 0.011774056 - 0.0706443 + 0.07; break;
            case 1: mA = mV * 0.011816083 - 0.000708965; break;
            case 2: mA = mV * 0.011794713 - 0.002240995; break;
            case 3: mA = mV * 0.011810675 - 0.002244028; break;
        }

        // Calculate pressure
        pressures[i] = mA * 250 - 1000.0;
        if (i == 0) pressures[i] -= 0.32; // Adjustment for Pressure 1
        if (i == 1) pressures[i] -= 0.12; // Adjustment for Pressure 2
        if (pressures[i] < 0) pressures[i] = 0.0; // Prevent negative pressure
    }
}
 
I'm doing a lot of serial writes to USB and three other real ports and it seem to slug down my program. What are good settings for buffer, size? What are the min and max?
The library always adds 64 bytes of buffer as standard for all physical serial ports. I've not dug into the USB port but I believe it has a larger buffer as standard.
Max added memory is 65472 bytes (64k - 64 giving 64k of buffer total).
Min is 0 (giving 64 bytes of buffer).

What's a good size? It depends on the use case but if you have the memory then the length of largest message you plan to send or if sending multiple messages rapidly the total of all the messages you will be outputting in one update cycle.
 
Back
Top