- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发我的第一个应用程序。该应用程序将使用我从RedPark获得的电缆与外部硬件进行通信。
它们提供了一个具有易于使用的多个功能的库。
现在,我需要通过发送命令来要求一个9 * 9表格,其中包含浮点数。
称为(void)readBytesAvailable的函数用于读取,它是事件驱动的。
首先,我使用NSMutableArray放置表:
for (int i=0; i<81; i++) {
NSNumber *number = [NSNumber numberWithFloat:rxLoopBuffer[i]];
[self.arrayRead addObject:number];
NSLog(@"%f",[number doubleValue]);
}
NSMutableData *dataRead = [[NSMutableData alloc]initWithBytes:&rxLoopBuff length:81];
unsigned char buffer[100];
[dataRead getBytes:buffer];
if (!self.arrayRead) {
[self.arrayRead removeAllObjects];
}
for (int i=0; i<81; i++) {
NSNumber *number = [NSNumber numberWithFloat:buffer[i]];
[self.arrayRead addObject:number];
NSLog(@"%f",[number doubleValue]);
}
- (void) readBytesAvailable:(UInt32)numBytes
{
int bytesRead;
BOOL res = NO;
// Read the data out
bytesRead = [rscMgr read:(rxLoopBuff+loopbackCount) length:numBytes];
rxCount += bytesRead;
//NSLog(@"Read %i, total=%i\n", bytesRead, rxCount);
if (this is the package I need)
{
//do something;
}
}
最佳答案
免责声明-我没有此电缆,因此,我无法对其进行测试,并且代码可能无法正常工作。但这只是盲目演示,我将如何做。它是启用ARC的代码。
SerialPortCommunication.h
#import <Foundation/Foundation.h>
@interface SerialPortCommunication : NSObject
- (void)readBytes:(UInt32)numberOfBytes
success:(void(^)( NSData *data ))success
failure:(void(^)( NSData *dataReadSoFar, NSError *error ))failure;
@end
#import "SerialPortCommunication.h"
#import "RscMgr.h"
#define BUFFER_SIZE 128
@interface SerialPortCommunication() < RscMgrDelegate > {
RscMgr *_rscMgr; // Resource Manager
BOOL _portAvailable; // Can we use port?
NSMutableData *_data; // Buffer for data
UInt32 _totalBytesToRead; // Number of total bytes to read
// Success block to call
void(^ _successBlock )( NSData *data );
// Failure block to call
void(^ _failureBlock )( NSData *dataReadSoFar, NSError *error );
UInt8 *_buffer;
}
@end
@implementation SerialPortCommunication
#pragma mark - Lifecycle
- (void)dealloc {
free( _buffer );
}
- (id)init {
self = [super init];
if ( self ) {
_rscMgr = [[RscMgr alloc] init];
[_rscMgr setDelegate:self];
_buffer = malloc( BUFFER_SIZE * sizeof( UInt8 ) );
// DO SERIAL PORT CONFIGURATION HERE
}
return self;
}
#pragma mark - Public
- (void)readBytes:(UInt32)numberOfBytes
success:(void(^)( NSData *data ))success
failure:(void(^)( NSData *dataReadSoFar, NSError *error ))failure {
if ( ! _portAvailable ) {
if ( failure ) {
// Create some NSError that port is not available
failure( nil, nil );
return;
}
}
if ( _data ) {
if ( failure ) {
// Create some NSError that port is in use
failure( nil, nil );
return;
}
}
_failureBlock = failure;
_successBlock = success;
_totalBytesToRead = numberOfBytes;
_data = [NSMutableData data];
// Now we're waiting for delegate methods
}
#pragma mark - RscMgrDelegate
- (void)cableConnected:(NSString *)protocol {
// From now on you can use serial port
_portAvailable = YES;
[_rscMgr open];
}
- (void)cableDisconnected {
// From now on you can't user serial port
_portAvailable = NO;
// Ouch, we can't read, fire failure
if ( _failureBlock ) {
// Create NSError which describes cable disconnection
_failureBlock( _data, nil );
}
// Our task ends here
_successBlock = nil;
_failureBlock = nil;
_data = nil;
_totalBytesToRead = 0;
}
- (void)portStatusChanged {
// Check status if everything's alright and you can still user serial port
// Set _portAvailable accordingly
// Replace if ( NO ) with condition if port is still alright and we can read
if ( NO ) {
if ( _failureBlock ) {
// Create NSError which describes status change and we can't use serial port
_failureBlock( _data, nil );
}
// Our task ends here
_successBlock = nil;
_failureBlock = nil;
_data = nil;
_totalBytesToRead = 0;
_portAvailable = NO;
} else {
_portAvailable = YES;
}
}
- (void)readBytesAvailable:(UInt32)length {
if ( ! _data ) {
// Here no one's interested in received data
// So you can decide if you want to save them for later use or trash them
// We are going to trash them
[_rscMgr read:_buffer length:MIN( BUFFER_SIZE, length )];
return;
}
UInt32 bytesToRead;
// First of all, we can't read more than our buffer size
bytesToRead = MIN( BUFFER_SIZE, length );
// Also it's enough to read only what user requested
UInt32 remainingBytes = _totalBytesToRead - _data.length;
bytesToRead = MIN( bytesToRead, remainingBytes );
// Now read bytes
UInt32 bytesRead = [_rscMgr read:_buffer length:bytesToRead];
if ( bytesRead > 0 ) {
[_data appendBytes:_buffer length:bytesRead];
}
if ( _data.length == _totalBytesToRead ) {
if ( _successBlock ) {
_successBlock( _data );
}
_successBlock = nil;
_failureBlock = nil;
_data = nil;
_totalBytesToRead = 0;
}
}
@end
#import "SerialPortUser.h"
#import "SerialPortCommunication.h"
@interface SerialPortUser() {
SerialPortCommunication *_serial;
}
@end
@implementation SerialPortUser
- (id)init {
self = [super init];
if ( self ) {
_serial = [[SerialPortCommunication alloc] init];
}
return self;
}
- (void)getData {
[_serial readBytes:81
success:^(NSData *data) {
// Here you have NSData with 81 bytes for sure
}
failure:^(NSData *dataReadSoFar, NSError *error) {
// Here you have NSData with dataReadSoFar.length bytes only
// read so far, because an error happens
// And error is in NSError *error
}];
}
@end
关于objective-c - iOS中的串行通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847214/
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
有没有办法只允许 https 而不是 http 与 Neo4j 服务器通信?另外,Neo4j Shell 的通信使用哪个 channel ,http 还是 https? 最佳答案 这来自 Neo4j
您好,我有新问题 :) 我正在构建带有面板的简单时事通讯,以向注册用户和时事通讯邮件地址发送邮件。 我有此代码,但时事通讯仅发送给注册用户。谁能告诉我为什么? $zapytanie = mys
第一次发帖,所以可能会有比必要的更多的信息,但我想彻底: 我们的 C 练习之一是创建发送器和接收器程序,通过 RS232 串行通信与零调制解调器交换数据。我们使用了虚拟端口程序(如果你想测试的话,我使
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有一台通过 RS485 连接到另一台设备的单板计算机。计算机应向设备发送请求并接收响应(使用设备相关协议(protocol))。我可以毫无问题地发送消息并且设备接收它们(例如,我可以更改设备的参数)
我目前正在尝试在我的 Visual Basic 6 应用程序中引用 .NET COM 库。我已经使用 Regasm 注册了它,并且在我的类(class)中将 ComVisible 设置为 true。但
Closed. This question needs to be more focused 。它目前不接受答案。 想改善这个问题吗?更新问题,使其仅通过 editing this post 关注一个
我尝试通过 https 协议(protocol)在 Archiva 和 Jenkins 之间建立通信,但我收到以下错误: [WARNING] Could not transfer metadata .
我完成了一个运行良好的客户端/服务器套接字通信程序。现在我想弄清楚如何做到这一点,以便我可以同时拥有到服务器的多个客户端连接。我环顾四周,似乎有不止几种不同的方法可以做到这一点。所以我来这里是想向你们
我正在 mailchimp 中制作时事通讯,我在使用 outlook 时遇到了这个问题,它一直在干扰我的两个专栏,如图所示: 这是这部分的代码:
我正在创建一份时事通讯,经过大量努力,它在除 android 的 gmail 应用程序之外的任何地方都有效。问题是它似乎有最小字体大小,这会导致我的表格损坏。 有没有办法克服最小字体大小而不是媒体查询
C++ 作为Client端 view plaincopy to clipboardprint? 复制代码 代码如下: // Client.cpp : Defines the entry poi
我创建了 ECM NewsLetter,其中包含一些网站链接(另一个项目)。在在线版本的 NewsLetter 链接中工作正常,但是当我将此 NewsLetter 发送到我的电子邮件并尝试单击我的邮件
无论出于何种原因,我的文本大小调整在 iPhone 上无法正常工作,但在 Android 和其他电子邮件格式中工作正常。似乎看不出这两个文件之间有任何区别。图片也调整了大小,似乎只是文本的问题。
我正在开发一个网站,我需要将 HTML 新闻稿发送到邮件列表。 我构建了一个 html 框架,其中包含“在浏览器中查看”和“取消订阅”链接(原因会有所不同)。在管理模块中,我发布 html 并为 ht
我正在尝试在 B-L072Z-LRWAN(Master) 和 Arduino(Slave) 之间进行 I2C 通信。 我使用以下代码成功将数据从主站发送到从站: B-L072Z-LRWAN 代码: #
我有 ECM NewsLetter,其中包含指向站点(另一个项目)的一些链接。当我通过单击发送按钮将此时事通讯发送到我的电子邮件时 - 当我从电子邮件收件箱中单击它们时,链接不起作用。它显示 404
我是一名优秀的程序员,十分优秀!