IOS Serial redpark不发送数据到串口?

时间:2015-04-08 05:42:34

标签: ios objective-c iphone model-view-controller

您好我是IOS的新手,我正在学习一个使用RedPark电缆将字符串写入串行的示例代码。 我有我正在学习的示例代码,它可以工作并可以将字符串发送到Arduino Serial。 另外,我有基于示例代码编写的代码,现在不能正常工作。 我已经尝试了几天并在网上进行了大量搜索,但仍然无法理解为什么我的代码无效。

有谁能告诉我为什么我的代码不起作用?

*************** * 有效的示例代码 **************************************   的 Appdelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@end  

AppDelegate.m

#import "AppDelegate.h"
#import "JMTerminalViewController.h"
#import "JMTerminalViewModel.h"
#import "RscMgr.h"

@implementation AppDelegate
{
@private

UIWindow* _mainWindow;
JMTerminalViewController* _terminalViewController;
RscMgr* _serialManager;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 _serialManager = [[RscMgr alloc]init];

JMTerminalViewModel* terminalViewModel = [[JMTerminalViewModel alloc]initWithSerialManager:_serialManager];
_terminalViewController = [[JMTerminalViewController alloc]initWithViewModel:terminalViewModel];


UINavigationController* navigationController = [[UINavigationController alloc]initWithRootViewController:_terminalViewController];
_mainWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_mainWindow.rootViewController = navigationController;
[_mainWindow makeKeyAndVisible];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

JMTerminalViewModel.h

#import <Foundation/Foundation.h>

@class RscMgr;

@interface JMTerminalViewModel : NSObject

@property (readonly) BOOL connected;
@property (nonatomic, strong, readonly) NSString* receivedText;

-(instancetype)initWithSerialManager:(RscMgr*)serialManager;

-(void) sendMessage:(NSString*)message;

@end

JMTerminalViewModel.h

#import <UIKit/UIKit.h>

@class JMTerminalViewModel;

@interface JMTerminalViewController : UIViewController

-(instancetype)initWithViewModel:(JMTerminalViewModel*)viewModel;

@end

JMTerminalViewController.h

#import "JMTerminalViewController.h"
#import "JMTerminalView.h"
#import "JMTerminalViewModel.h"
#import "RscMgr.h"

@interface JMTerminalViewController () <UITextFieldDelegate>

@end

@implementation JMTerminalViewController
{
    @private

    JMTerminalViewModel* _viewModel;
    RscMgr* manager;
}

-(instancetype)initWithViewModel:(JMTerminalViewModel *)viewModel
{
self = [super init];

if (self)
{
    _viewModel = viewModel;
}

return self;
}

-(void)loadView
{
    self.view = [[JMTerminalView alloc]init];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

self.navigationItem.title = @"Disconnected";

JMTerminalView* terminalView = (JMTerminalView*)self.view;

terminalView.inputTextField.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

JMTerminalView* terminalView = (JMTerminalView*)self.view;

terminalView.inputTextField.delegate = nil;
}

#pragma mark - Text field delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_viewModel sendMessage:@"w2Open@w1Open@w2Open@"];
    textField.text = nil;
    return NO;
}

@end

JMTerminalView.h

#import <UIKit/UIKit.h>

@interface JMTerminalView : UIView

@property (nonatomic, strong, readonly) UITextField* inputTextField;
@property (nonatomic, strong, readonly) UITextView* receivingTextView;

@property CGFloat bottomOffset;

@end

JMTerminalView.m

#import "JMTerminalView.h"

@implementation JMTerminalView
{
    @private

    NSLayoutConstraint* _bottomConstraint;
}

- (instancetype)init
{
    self = [super init];

    if (self)
    {
    self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];

    _inputTextField = [[UITextField alloc]init];
    _inputTextField.borderStyle = UITextBorderStyleRoundedRect;
    _inputTextField.translatesAutoresizingMaskIntoConstraints = NO;
    [_inputTextField setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

    _receivingTextView = [[UITextView alloc]init];
    _receivingTextView.backgroundColor = [UIColor whiteColor];
    _receivingTextView.translatesAutoresizingMaskIntoConstraints = NO;
    _receivingTextView.editable = NO;
    _receivingTextView.font = [UIFont systemFontOfSize:18.0];
    _receivingTextView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

    [self addSubview:_receivingTextView];
    [self addSubview:_inputTextField];

    _bottomConstraint = [NSLayoutConstraint constraintWithItem:_inputTextField attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5];

    [self addConstraint:_bottomConstraint];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_inputTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:0.0 constant:35]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_receivingTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_receivingTextView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_inputTextField attribute:NSLayoutAttributeTop multiplier:1.0 constant:-5]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|    [_receivingTextView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_receivingTextView)]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-5-[_inputTextField]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_inputTextField)]];
    }

    return self;
}

-(void)setBottomOffset:(CGFloat)bottomOffset
{
    _bottomConstraint.constant = -bottomOffset - 5;
}

-(CGFloat)bottomOffset
{
    return -(_bottomConstraint.constant + 5);
}

@end

************************************ 我的代码不起作用 ****************

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"
#import "DetectionViewModel.h"
#import "RscMgr.h"


@interface AppDelegate ()

@end

@implementation AppDelegate{
@private
RscMgr* _serialManager;
ViewController* _viewController;

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
_serialManager = [[RscMgr alloc] init];

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UINavigationController* navController = (UINavigationController*)    [mainStoryboard instantiateViewControllerWithIdentifier:@"navController"];


DetectionViewModel* detectionViewModel = [[DetectionViewModel alloc] initWithSerialManager: _serialManager];
_viewController = [[ViewController alloc] initWithViewModel: detectionViewModel];
UINavigationController* navigationController = [[UINavigationController alloc]initWithRootViewController:_viewController];
navController = navigationController;

return YES;
}
@end

ViewController.h

#import <UIKit/UIKit.h>
#import <opencv2/videoio/cap_ios.h>
#import <opencv2/imgcodecs/ios.h>

using namespace cv;

@class DetectionViewModel;

@interface ViewController : UIViewController<CvVideoCameraDelegate>{
IBOutlet UIImageView* imageView;
__weak IBOutlet UITextField *inputTextField;
}

-(instancetype)initWithViewModel:(DetectionViewModel*)viewModel;

@end

ViewController.m

#import "ViewController.h"
#import "DetectionViewModel.h"
#import "opencv2/videoio/cap_ios.h"
#import "opencv2/imgcodecs/ios.h"
#import "RscMgr.h"


@interface ViewController ()<UITextFieldDelegate>
@end

@implementation ViewController
{
@private
DetectionViewModel* _viewModel;
}
-(instancetype)initWithViewModel:(DetectionViewModel *)viewModel{
self = [super init];
if(self){
    NSLog(@"init--DetectionViewModel");
    _viewModel = viewModel;
}
return self;
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self->inputTextField.delegate=self;

}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self->inputTextField.delegate = nil;
}


- (void)viewDidLoad {
[super viewDidLoad];
self->inputTextField.delegate=self;

[_viewModel open];  
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}

#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[_viewModel sendMessage:@"jsd@jh@@hj@jhg@jh@@jh@jh@w2Open@w2Open@"];
inputTextField.text=nil;

 return NO;
}


@end

DetectionViewMode.h

#import <Foundation/Foundation.h>

@class RscMgr;

@interface DetectionViewModel : NSObject

@property (readonly) BOOL connected;
@property (nonatomic, strong, readonly) NSString* receivedText;

-(instancetype)initWithSerialManager:(RscMgr*)serialManager;
-(void) sendMessage:(NSString*)message;
@end

DetectionViewModel.m

#import "DetectionViewModel.h"
#import "RscMgr.h"


@interface DetectionViewModel()<RscMgrDelegate>

@end

@implementation DetectionViewModel
{
@private
RscMgr* _serialManager;
UInt8 buf[14];
}

-(instancetype)initWithSerialManager:(RscMgr* )serialManager{
self = [super init];
if(self){
    _serialManager = serialManager;
    [_serialManager setDelegate:self];
     _receivedText = @"";
}
return self;
}


-(void)sendMessage:(NSString *)message{
NSLog(@"in sendMessage() now");
NSLog(@"baud in sendMessage is:%i",[self getBaud]);
NSLog(@"txMode in sendMessage is: %i",[self getTxMode]);

  if(_connected){

  [_serialManager writeString:message];
 }
}

-(void)setConnected:(BOOL)connected{
[self willChangeValueForKey:@"connected"];
_connected = connected;
[self didChangeValueForKey:@"connected"];
}

-(void)setReceivedText:(NSString *)receivedText{
[self willChangeValueForKey:@"receivedText"];
_receivedText = receivedText;
[self didChangeValueForKey:@"receivedText"];
}

#pragma mark - Delegate
-(void)readBytesAvailable:(UInt32)length{
NSString* messageReceived = [_serialManager getStringFromBytesAvailable];
[self setReceivedText:[_receivedText       stringByAppendingFormat:@"%@",messageReceived]];

}

-(void)cableConnected:(NSString *)protocol{
[self setConnected:YES];
[_serialManager setBaud:9600];
[_serialManager open];

}
-(void)cableDisconnected{  
[self  setConnected:NO];
}
-(void)portStatusChanged{
}

@end

不同之处在于示例使用代码来执行界面,我使用了故事板。我的代码也编译和运行,但只是不发送数据到串口。 我会尽快发布更多信息。

1 个答案:

答案 0 :(得分:0)

我最后只写了一个简单的测试应用程序,它可以运行并可以将数据发送到串行电缆: 对于Arduino:  ArduinoReceiver.ino

unsigned char inChar;
unsigned char commandChar;
char command[100];
int commandLength = 0;
String currentCommand;
int i=0;   //line 10

int w1In1 = 22;
int w1In2 = 23;
int w1Pwm = 9;
int w2In1 = 25;    //int w2In1 = 7;
int w2In2 = 24;    //int w2In2 = 6;
int w2Pwm = 5;    //line 17

int tempLeng=0;
int tempSpeed=0; //line 20

void setup(){
  currentCommand="";
  pinMode(w1In1, OUTPUT);
  pinMode(w1In2, OUTPUT);
  pinMode(w1Pwm, OUTPUT);
  pinMode(w2In1, OUTPUT);
  pinMode(w2In2, OUTPUT);
  pinMode(w2Pwm, OUTPUT);

  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");
} //line 30

void loop(){
  if(Serial.available() > 0){
    readChar();   
    Serial.print(currentCommand);//
  }
}


//*******************Reading psedo commands from Serial buffer*******************
void readChar(){
  commandChar = Serial.read();

//  if(commandChar != '$'&&){
  if( commandChar != '\r' && commandChar!='\n'&&commandChar!='@'){
    if(commandLength < 999){
     command[commandLength++]=commandChar;
   }
 }
 else{

  if(commandLength>0){
    command[commandLength]='\0';
    Serial.print("commandLength:");
    Serial.println(commandLength);
    Serial.print("arduino received >");
    Serial.println(command);
    for(i=0;command[i]!='\0';i++)
    { currentCommand +=command[i];}
    //*********************************Processing

    //***********************************processing
    commandLength = 0;
    memset(command,0b00000000, sizeof(command)); 

   }
  }

}

代码输入:

首先,您需要RedPark串行电缆库,可以在Redpark的网站上下载:http://redpark.com/products/developers/

代码:

AppleDelegate.h and AppleDelegate.m is the default code for single view App


ViewController.h

#import <UIKit/UIKit.h>
#import "RscMgr.h"
@interface EnterCommandViewController : UIViewController  <RscMgrDelegate>
{
RscMgr* rscMgr;
IBOutlet UITextField *inputTextField;
}

-(void)sendMessage:(NSString*)message;
@end

ViewController.m

#import "EnterCommandViewController.h"
#import "DetectionViewController.h"

@interface EnterCommandViewController () <UITextFieldDelegate>
{
    @private
    BOOL connected;
}

@end
- (void)viewDidLoad {

    [super viewDidLoad];
    rscMgr = [[RscMgr alloc] init];
    [rscMgr setDelegate:self];
    // Do any additional setup after loading the view, typically from a nib.
    [inputTextField becomeFirstResponder];
}
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    inputTextField.delegate = self;
}

-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    inputTextField.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
} 
#pragma mark - UITextFieldDelegate Methods
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [self sendMessage:textField.text];
    textField.text = nil;
    return NO;
}
#pragma mark - Serial Tx/Rx Methods Implementation
-(void) sendMessage:(NSString *)message{
    if(connected == YES) {
        [rscMgr writeString:message];
    }
    else{
        NSLog(@"CableDisconnected!");
       NSLog(@"Attempted To Send: %@",message);
    }
}
#pragma mark - RscMgrDelegate Methods Implementation
-(void) cableConnected:(NSString *)protocol{
    inputTextField.text = @"cableConnected";
    [rscMgr setBaud:9600];
    [rscMgr open];
    connected = YES;
}
-(void) cableDisconnected{
    inputTextField.text = @"cableDisconnected";
    connected = NO;
}
-(void) readBytesAvailable:(UInt32)length{}
-(void) portStatusChanged{}

@end

所以加班你输入一个字符串到textField然后按返回字符串将被写入Arduino串口,你可以读取串口管理器中的字符串。