【问题标题】:RedPark TTL Cable ReadBytesData - unable to get any dataRedPark TTL 电缆 ReadBytesData - 无法获取任何数据
【发布时间】:2013-01-31 02:25:03
【问题描述】:

我有两个 UILabel,信息和接收。接收应该告诉我我实际上正在接收数据。但是,当它确实表明......我的信息 UILabel 没有相应地更新。它有时有效,有时无效..

这是怎么回事?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [super viewDidLoad];

        receiving = [[UILabel alloc] initWithFrame: CGRectMake(30,50,255,100)];
        info = [[UILabel alloc] initWithFrame: CGRectMake(30,200,255,100)];

        info.backgroundColor = [UIColor orangeColor];
        receiving.backgroundColor = [UIColor orangeColor];
//        [self.view addSubview: toggleSwitch];

        receiving.text = @"Not Receiving...";

        [self.view addSubview: info];
        [self.view addSubview: receiving];
    }
    return self;
}

- (void) startCommThread:(id)object
{

//    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // initialize RscMgr on this thread
    // so it schedules delegate callbacks for this thread

    rcsMgr = [[RscMgr alloc] init];
    [rcsMgr setDelegate:self];

    // run the run loop
    [[NSRunLoop currentRunLoop] run];

//    [pool release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create and start the comm thread.  We'll use this thread to manage the rscMgr so
    // we don't tie up the UI thread.
    if (commThread == nil)
    {
        commThread = [[NSThread alloc] initWithTarget:self
                                             selector:@selector(startCommThread:)
                                               object:nil];
        [commThread start];  // Actually create the thread
    }
}


- (void) readBytesAvailable:(UInt32)numBytes {

    receiving.text = @"Receiving...";
    NSString* str = [rcsMgr getStringFromBytesAvailable];
    if ( str ) receiving.text = @"I GOT SOMETHING!";
//    if ( numBytes ) receiving.text = numBytes;
    else receiving.text = @"Still Receiving...";
    info.text = str;

}

【问题讨论】:

  • 这里没有足够的信息让任何人说太多。但是,我首先想到的是,-readBytesAvailable: 是在主线程/队列上调用的吗?
  • 是的,它正在主线程上调用
  • 说实话。到目前为止,我的项目看起来像这样...makeprojects.com/Project/… 但是现在我正在尝试使用串行端口读取字符,它说“我有东西”但什么都没有出现...
  • 你在调试器中运行过这个吗? str 是您在致电 -getStringFromBytesAvailable 后所期望的吗?
  • 我在哪里可以找到这个调试器?我正在将枫木板的 USB 连接到笔记本电脑以供电,并将我的 iphone 的 TTL 电缆连接到枫木板……不知道如何调试。否则,我希望 str 具有字符串类型。例如。 "123,123,123,123,"

标签: iphone objective-c serial-port


【解决方案1】:

这是我最好的答案,但需要注意的是我从未使用过 RedPark 的电缆或 SDK。我的预感是 -readBytesAvailable: 由 RscMgr 在用于设置它的同一后台线程上调用。您不能在后台线程上使用 UIKit 对象。您需要将工作转移回主线程/队列,如下所示:

- (void)readBytesAvailable:(UInt32)numBytes 
{
    NSString* str = [rcsMgr getStringFromBytesAvailable];
    dispatch_async(dispatch_get_main_queue(), ^{
        if (str) {
            receiving.text = @"I GOT SOMETHING!";
        } else { 
            receiving.text = @"Still Receiving...";
        }
        info.text = str;
    });
}

此外,您的 commThread 上应该有一个自动释放池。你为什么要评论它?

【讨论】:

  • 感谢您的回复。它似乎没有满足我的需求,因为问题仍然存在。此外,ARC 已开启,因此不可用。
  • 在 ARC 开启的情况下,通过包围 @autoreleasepool{} 中的代码来设置自动释放池。恐怕我不能再提供任何帮助了...
  • 似乎电缆是问题所在。得到另一根电缆,一切似乎都工作正常。非常感谢安德鲁的帮助。 :)
  • 很高兴听到:)。我想重要的是要记住硬件有时确实可能有缺陷。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多