Platform: iOS with TCP/IP comms.

The following is an example of my incoming data stream.

$PLTIT,HV,0.20,M,0.00,D,-58.70,D,0.30,M*71
$PLTIT,HV,1.90,M,0.00,D,-8.30,D,1.90,M*41

A valid message frame looks like this:

$PLTIT,HV,1.90,M,0.00,D,-8.30,D,1.90,M*41

But there could be moments where I get this:

[time n+0]  $PLTIT,HV,0.20,M,0.00,D,-58.70,D,0.30,M*71
[time n+1]  $PLTIT,HV,1.90,M,
[time n+2]  0.00,D,-8.30,D,1.90
[time n+3]  ,M*41
[time n+4]  $PLTIT,HV,0.20,M,0.00,D,-58.70,D,0.30,M*71
[time n+5]  $PLTIT,HV,0.20,M,
[time n+6]  0.00,D,-58.70,D,0.30,M*71
[time n+7]  $PLTIT,HV,0.20,M,0.00,D,-58.70,D,0.30,M*71
[time n+8]  $PLTIT,HV,0.20,M,0.00,D,-58.70,D,0.30,M*71

The result is a broken up message frame in the middle.

What objects/containers can I use under iOS to enable me to collect all the data bytes correctly for parsing and not lose any bits due to some intermittent comms or equipment?

有帮助吗?

解决方案

A simple buffer of any variety should do the trick. If your data is always textual then you could even just append to an NSMutableString.

All you'd have to do is append the data, do a check to see if the buffer contains more than a single instance of the $ character (which I assume is your delimiter between entries), and if it does then process each entry except the very last, leaving the last one in the buffer until the next chunk of data arrives.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top