https://github.com/armadsen/ORSSerialPort
当前版本不能一行一行读取串口数据,需要添加以下代码。
- (NSString *) readLine {
char *bufptr;
char buffer[1024];
bzero(buffer, 1024);
int nbytes;
char inchar;
int localPortFD = self.fileDescriptor;
bufptr = buffer;
while ((nbytes = read(localPortFD, &inchar, 1)) > 0)
{
if (inchar == '\n') break;
if (inchar == '\r') break;
*bufptr = inchar;
++bufptr;
}
*bufptr = '\0';
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
// Data is available
// char buf[1024];
// long lengthRead = read(localPortFD, buf, sizeof(buf));
// if (lengthRead>0)
// {
// NSData *readData = [NSData dataWithBytes:buf length:lengthRead];
// if (readData != nil) [self receiveData:readData];
// }
NSString *line = [self readLine];
if (line != nil) [self receiveData:[line dataUsingEncoding:NSUTF8StringEncoding]];