objective c - NSString substring issue -
here code i'm using separate nsstring substrings. don't understand why being separated way are. i've had instances comes out right , messed up. method delegate protocol defined in project. feel way port receiving data , timing it. wanted make sure i'm not incorrectly using nsstring.
i'm trying x, y , z strings same array.
- (void)serialport:(orsserialport *)serial didreceivedata:(nsdata *)data { nsstring *temp = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nsarray *listitems = [temp componentsseparatedbystring:@" "]; nslog(@"%@", listitems); output:
2015-04-28 14:15:50.522 groundstation[69301:3573401] ( x01, y00, "" ) 2015-04-28 14:15:50.522 groundstation[69301:3573401] ( z05, "" ) 2015-04-28 14:15:53.675 groundstation[69301:3573401] ( x01, y00, "" ) 2015-04-28 14:15:53.675 groundstation[69301:3573401] ( z04, "" ) 2015-04-28 14:15:58.226 groundstation[69301:3573401] ( "x-34", "y-1" ) 2015-04-28 14:15:58.228 groundstation[69301:3573401] ( 4, z38, "" )
is data coming serial port encoded in utf8 or maybe it's plain ascii or else?
i'd try replacing second line this:
nsstring *temp = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; from question got impression not sure what's coming out of serial connection, if it's binary protocol?
edit:
after reading comment, differently, don't separate spaces each time receive nsdata, message can broken in multiple pieces, receive x , y , z alone next time receivedata called, or in last step value of y broken in 2 messages...
you need store data received callback , process when have full message, not parsing every time doing now.
you can 2 things:
keep fixed size messages e.g. size of 12 characters: x123y123z123, sign included. put content receive in buffer , parse these 12 chars only when have them. when you've parsed 3 x,y,z components remove 12 chars buffer , repeat.
add line terminator (could char). each time receive data put in buffer, , parse message only when find line terminator, when parse 3 x,y,z components remove stuff buffer , repeat.
Comments
Post a Comment