Skip to content

Commit

Permalink
bug fix - clear buffer when packet processed in full
Browse files Browse the repository at this point in the history
  • Loading branch information
kpishere committed Jul 26, 2020
1 parent 97be337 commit 2ca2ff2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Framework Project/POSIXSerialPortTests/POSIXSerialPortTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ - (void)serialPortWasClosed:(POSIXSerialPort *)serialPort;

@property (nullable,weak) XCTestExpectation *portOpen;
@property (nullable,weak) XCTestExpectation *firstWrite;
@property (nullable,weak) XCTestExpectation *nextPacket;
@property (nullable,weak) XCTestExpectation *readWriteTestComplete;
@end

Expand All @@ -40,6 +41,7 @@ - (void)serialPort:(POSIXSerialPort *)serialPort didReceiveData:(NSData *)data;
, data );
if(self.readWriteTestComplete !=nil ) [self.readWriteTestComplete fulfill];
if(self.firstWrite !=nil ) [self.firstWrite fulfill];
if(self.nextPacket !=nil ) [self.nextPacket fulfill];
}
- (DataSegment)serialPort:(POSIXSerialPort *)serialPort nextDataSegmentValidIn:(NSData *)data;
{
Expand Down Expand Up @@ -110,20 +112,24 @@ - (void)test_writeRead {
self.pDelegate.portOpen = [self expectationWithDescription:@"Port open"];
self.pDelegate.firstWrite = [self expectationWithDescription:@"Read incomplete packet"];
self.pDelegate.firstWrite.expectedFulfillmentCount = 1;
self.pDelegate.nextPacket = [self expectationWithDescription:@"Next complete packet"];
self.pDelegate.nextPacket.expectedFulfillmentCount = 2;
self.pDelegate.readWriteTestComplete = [self expectationWithDescription:@"Read complete"];
self.pDelegate.readWriteTestComplete.expectedFulfillmentCount = 2;
self.pDelegate.readWriteTestComplete.expectedFulfillmentCount = 3;

NSData *sendData = [@"FEED BEEF" dataUsingEncoding:NSUTF8StringEncoding];

[self.port open];
[self waitForExpectations:@[self.pDelegate.portOpen] timeout:5];

NSData *sendData = [@"FEED BEEF" dataUsingEncoding:NSUTF8StringEncoding];
[self.port sendData:sendData];
[self waitForExpectations:@[self.pDelegate.firstWrite] timeout:10];

sendData = [@" " dataUsingEncoding:NSUTF8StringEncoding];
[self.port sendData:sendData];

[self waitForExpectations:@[self.pDelegate.nextPacket] timeout:15];

sendData = [@"EAAD " dataUsingEncoding:NSUTF8StringEncoding];
[self.port sendData:sendData];
[self waitForExpectations:@[self.pDelegate.readWriteTestComplete] timeout:20];

[self.port close];
Expand Down
2 changes: 2 additions & 0 deletions Source/POSIXSerialPort.m
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ - (void)processDataReceived;
if(ds.offset != offset || ds.size != size) {
self._data = dispatch_data_create_map(dispatch_data_create_subrange(region, offset+ds.size+(ds.offset-offset)
, size - (ds.size + (ds.offset-offset))),nil,nil);
} else {
self._data = nil; // Release the data
}
});
});
Expand Down

0 comments on commit 2ca2ff2

Please sign in to comment.