Best way to send string of data wirelessly? XBee is too slow!

Hello there,

I am working on a project that needs fast wireless communication from MATLAB to arduino (One way).

The data that needs to be sent is in the format of string of numbers, where the sending rate is 100Hz.

I am using the XBee 1mW Series 1 and Teensy 3.2.

Here is what I have got working. The data is sent from matlab in this format
<1234123412341234>

The program only looks for the start marker ‘<’ and then starts recording the other numbers until we reach the end marker ‘>’

I would like the main loop to run at 100Hz, but the problem is that the program main loop is only able to run at about 25Hz, which is way too slow for my application.

FYI - I am able to run up to 250hz loop without reading the data string.

Is there a way to optimise the code? Or is serial communication of XBee too slow? How can I improve this?

Thank you so much in advance.

void setup(){
    Serial3.begin(57600);
}

void loop(){

    recvWithStartEndMarkers();

    if(newData){
          // do something here
    }

    current_time = millis();
    time_diff = current_time - prev_time;
    prev_time = current_time;
    
    Serial.print("The loop is running at");
    Serial.print(1/time_diff);
    Serial.println("Hz");
}

byte newData = 0;
char receivedChars[18];

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial3.available() > 0 && newData == false) {
        rc = Serial3.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        if (rc == startMarker) {
            recvInProgress = true;
            ndx = 0;
        }
    }
}

Heinrich Hertz deserves a capital letter in the SI unit named after him.

How do you know that you're running 25Hz? That small program you showed doesn't report speed.

57600 baud is about 5760 bytes per second. To achieve 100Hz, you can only send 57 bytes per message. How long is the actual message that Matlab sends?

MorganS:
How do you know that you're running 25Hz? That small program you showed doesn't report speed.

57600 baud is about 5760 bytes per second. To achieve 100Hz, you can only send 57 bytes per message. How long is the actual message that Matlab sends?

I use the micros() function to check the time difference between each loop.

Matlab sends 18 characters per message. When I increase the baud rate, the data gets very corrupted, so I stick with 57600, which should be enough?

puedpued:
I would like the main loop to run at 100hz, but the problem is that the program main loop is only able to run at about 25hz, which is way too slow for my application.

FYI - I am able to run up to 250hz loop without reading the data string.

I don't understand what you mean by "loop" in this context.

Also I don't understand from your Original Post what you actually want to send 100 times per second.

I routinely use the code in recvWithStartEndMarkers() at 500,000 baud so the Arduino code is not the obstacle.

I don't have any experience with XBee modules. Have you tried communication using a USB cable rather than the XBee?

...R

Robin2:
I don't understand what you mean by "loop" in this context.

Also I don't understand from your Original Post what you actually want to send 100 times per second.

I routinely use the code in recvWithStartEndMarkers() at 500,000 baud so the Arduino code is not the obstacle.

I don't have any experience with XBee modules. Have you tried communication using a USB cable rather than the XBee?

...R

The data is being sent wirelessly to Xbee at 100hz, but the arduino cant keep up for some reasons. I need it to log all the data that is being sent.

I have a timer in the loop() function that counts the time difference between each time the loop() function is called. I need the time difference between each time loop to be at least 10ms.

I haven't try the USB cable, I might give it ago. But still, I need this to be done wirelessly.

Hope it makes sense, and thanks for helping!!

JohnLincoln:
Heinrich Hertz deserves a capital letter in the SI unit named after him.

Or write it “s-1”. 8)

puedpued:
The data is being sent wirelessly to Xbee at 100hz,

WHAT is being sent 100 times per second? Give some examples of typical messages.

I have a timer in the loop() function that counts the time difference between each time the loop() function is called. I need the time difference between each time loop to be at least 10ms.

Post the program with the timing code

I haven't try the USB cable, I might give it ago. But still, I need this to be done wirelessly.

The purpose of trying the USB cable is to see if the XBee is the cause of the problem.

...R

Give some examples of typical messages.

I did in the first post.

Post the program with the timing code

I modified the code in the first post as well. Please kindly have a look there.

Thanks, I appreciate it.

puedpued:
I modified the code in the first post as well. Please kindly have a look there.

NEVER do that. It makes a nonsense of all the comments that have followed your post.

In future when you have new code or new information put it in a new Reply so that the chronological order of the discussion is not upset.

The program in your Original Post is not complete. Please post a complete program in your next Reply.

…R

The code in the first post cannot print anything other than "The loop is running at0Hz"

Show us the actual code.