0

I have not been able to find this information anywhere. How long can a string be send with the TTL version of the redpark cable?

The following delegate method is called twice when I print something thorough serial from my Arduino, an example of a string is this: 144;480,42;532,40;20e

- (void) readBytesAvailable:(UInt32)length{

When I use the new function methods of retrieving available data [getStringFromBytesAvailable] I will only get 144;480,42;532,40; and then the whole function is called again and the string now contains the rest of the string: 20e

The following method is working for appending the two strings, but only if the rate of data transmission is 'slow' (1 time a second, I would prefer minimum 10 times a second).

-

 (void) readBytesAvailable:(UInt32)length{

    if(string && [string rangeOfString:@"e"].location == NSNotFound){
        string = [string stringByAppendingString:[rscMgr getStringFromBytesAvailable]];
        NSLog(string);
        finishedReading = YES;
    }
    else{
        string = [rscMgr getStringFromBytesAvailable];

    }


    if (finishedReading == YES) 
     {

        //do stuff

     }
        finishedReading = NO;
        string = nil;
    }

}

But can you tell my why the methods is called twice if I write a "long" string, and how to avoid this issue?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
moryde
  • 61
  • 5

3 Answers3

0

That is just how serial ports work. You can't and don't need to avoid those issues. There is no attempt at any level of the SW/HW to keep your serial data stream intact, so making any assumptions about that in your code is just wrong. Serial data is just a stream of bytes, with no concept of packetization. So you have to deal with the fact that you might have to read partial data and read the rest later.

TJD
  • 11,800
  • 1
  • 26
  • 34
0

Since your program fragment runs faster then the time it takes to send a string, you need to capture the bytes and append them to a string.

If the serial data is terminated with a carriage return you can test for it to know when you have received the entire string.

Then you can allow your Arduino to send 10 times a second.

Jeff
  • 1,364
  • 1
  • 8
  • 17
0

The serialPortConfig within the redparkSerial header file provided by RedPark does, in fact, give you more configuration control than you may realize. The readBytesAvailable:length method is abstracted, and is only called when one of two conditions is met: rxForwardingTimeout value is exceeded with data in the primary buffer (default set to 100 ms) or rxForwardCount is reached (default set to 16 characters).

So, in your case it looks like you've still got data in your buffer after your initial read, which means that the readBytesAvailable:length method will be called again (from the main run loop) to retrieve the remaining data. I would propose playing around with the rxForwardingTimeout and rxForwardCount until it performs as you'd expect.

As already mentioned, though, I'd recommend adding a flag (doesn't have to be carriage return) to at least the end of your packet, for identification.

Also, some good advice here: How do you design a serial command protocol for an embedded system?

Good luck!

Community
  • 1
  • 1