MAC桌面程序开发之 USB串口调试助手开发

本次主要讲解,如何在Mac电脑上开发,如今流行的电脑桌面程序,因为我们在做互联网/智能化设备的时候,无疑或许会用到MAC来进行调试;

重点如下:

  1. 如何在MAC电脑上创建桌面程序;
  2. 在项目中添加usb协议库。如:HID,ORSSerialPort;
  3. 项目内容主要分为:串口名,波特率,开/关串口,发送数据,接收设备数据,上传文件;
  4. 此文章用的USB协议是《ORSSerialPort》,目前也就基本是这个对接硬件设备,相互链接进行收发数据的。
  5. 逻辑:电脑监听usb接口~是否有设备链接~一旦有链接就进行读取名称~设置波特率~打开串口~上传固件文件(这块就需要与硬件工程师对接协议了)~发送字节数据~接收设备数据;

该具备的硬件与软件:

  1. MAC电脑一台,键盘,鼠标;
  2. Xcode编译器;
  3. 终端设备,开发板一块,如:stm32;
  4. USB数据线一根;
  5. 准备好一个文件,有一个升级的功能;

一:创建桌面程序

1,打开xcode编译器,选择fiie-New-Probject-macos-app-next;
在这里插入图片描述
2,创建项目名称了,这个自己定义了哦,下面的默认不要管。
在这里插入图片描述
3,桌面程序项目创建完成,这是我的总项目架构;
在这里插入图片描述
二:USB接口开发

1.上面就讲了,用的ORSSerialPort协议,如果有不了解的,也可以网上看看这个协议。

#import <Cocoa/Cocoa.h>
#import "CustomProgress.h"
#import "MIDIManager.h"
#import "ORSSerialPort.h"

@class ORSSerialPortManager;

@interface ViewController : NSViewController <ORSSerialPortDelegate,MidiGetDataDelegate, NSUserNotificationCenterDelegate,NSTextViewDelegate,NSTableViewDelegate>
@property (weak) IBOutlet NSArrayController *DeviceArray;

@property (weak) IBOutlet NSButton *OpenOrClose;

@property (weak) IBOutlet NSTextField *StatusText;
@property (weak) IBOutlet NSTextField *PassageD;

@property (weak) IBOutlet NSTextField *TextNameH;

//@property (weak) IBOutlet NSTextField *RXCounter;
//@property (nonatomic, assign) long RXNumber;

@property (nonatomic, strong) NSData * sendData;

//@property (weak) IBOutlet NSTextField *TXCounter;
//@property (nonatomic, assign) long TXNumber;

@property (weak) IBOutlet NSTextField *filePathTF;
@property (weak) IBOutlet NSButton *OpenBut;
@property (weak) IBOutlet NSButton *UpdateBut;
@property (weak) IBOutlet CustomProgress *processView;

@property (unsafe_unretained) IBOutlet NSTextView *RXDataDisplayTextView;

@property (unsafe_unretained) IBOutlet NSTextView *TXDataDisplayTextView;
@property (weak) IBOutlet NSMatrix *stringType;
@property (weak) IBOutlet NSMatrix *stringType_TX;

@property (weak) IBOutlet NSTextField *TimeInternel;
@property (weak) IBOutlet NSTextField *countOfSend;
@property (weak) IBOutlet NSButton *SendButton;

@property (nonatomic, assign) BOOL isRXHexString;

@property (nonatomic, assign) BOOL isTXHexString;

@property (nonatomic, assign) BOOL isRXGBKString;
@property (nonatomic, assign) BOOL isTXGBKString;

@property (nonatomic, strong) ORSSerialPortManager *serialPortManager;
@property (nonatomic, strong) ORSSerialPort *serialPort;//ORSSerialPort
@property (nonatomic, strong) NSArray *availableBaudRates;
@property (weak) IBOutlet NSTableView *tableviewFordevices;

@property (nonatomic,strong) NSSavePanel*  panel;
@property (nonatomic, assign) BOOL isLoopSend;
@property (nonatomic, assign) BOOL isWorkInSend;
@property (nonatomic, assign) BOOL isOnlyDisplayRxData;
@property (assign,nonatomic) int sendCount;
@property (assign,nonatomic) NSTimer *timer;
@property (nonatomic, strong) NSWindow *MyMoneyWindow;
@end

2,设置打开串口的代码

- (IBAction)openComPort:(id)sender {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.serialPort.isOpen ? [self.serialPort close] : [self.serialPort open];
    });
}

3.发送数据

//如果串口没打开,不能发生数据
-(void)sendDataWithPort{
    
    if (!self.serialPort.isOpen) {
        self.StatusText.stringValue = @"The serial port is not open and cannot send data.";
        return;
    }

        if(_sendData){
               if([self.serialPort sendData:_sendData]){
                   self.StatusText.stringValue = @"发送HEX数据成功";
                  // self.TXCounter.stringValue = [NSString stringWithFormat:@"%@",_sendData];
               }else{
                   self.StatusText.stringValue = @"发送HEX数据失败";
                   return;
               }
           }

4.打开串口,关闭串口;

#pragma mark - ORSSerialPortDelegate Methods

- (void)serialPortWasOpened:(ORSSerialPort *)serialPort
{
    self.OpenOrClose.title = @"Close the serial port";
    self.StatusText.stringValue = @"Serial port is open";
}

- (void)serialPortWasClosed:(ORSSerialPort *)serialPort
{
    self.OpenOrClose.title = @"Open serial port";
    self.StatusText.stringValue = @"Serial port is closed";
}

5.程序启动的时候,读取到usb插入的设备;

#pragma mark - Properties

- (void)setSerialPort:(ORSSerialPort *)port
{
    if (port != _serialPort)
    {
//      [_serialPort close];
        _serialPort.delegate = nil;
        _serialPort = port;
        _serialPort.delegate = self;
        self.OpenOrClose.title = self.serialPort.isOpen ? @"Close the serial port" : @"Open serial port";
        NSString *tmp=[NSString stringWithFormat:@"%@%@",_serialPort.name,(self.serialPort.isOpen ? @"Serial port is open" : @"Serial port is closed")];
        self.StatusText.stringValue = tmp;
    }
}

6,点击上传的按钮

扫描二维码关注公众号,回复: 9482458 查看本文章
- (IBAction)GDeviceFile:(NSButton *)sender {
    NSLog(@"我选择了 : OpenBut");
    [self openFinder];
}

- (void)openFinder{
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    
    [panel setCanChooseFiles:YES];  //是否能选择文件file
    
    [panel setCanChooseDirectories:YES];  //是否能打开文件夹
    
    [panel setAllowsMultipleSelection:NO];  //是否允许多选file
    
    NSInteger finded = [panel runModal];   //获取panel的响应
    
    if (finded == NSModalResponseOK) {
        
        for (NSURL *url in [panel URLs]) {
            
            NSLog(@"文件路径--->%@",url);
            _filePathTF.stringValue = [NSString stringWithFormat:@"%@",url];
            self.TextNameH.stringValue = [NSString stringWithFormat:@"%@",url];
            //同时这里可以处理你要做的事情 do something
            
            // 获取文件内容
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
            _sendData = [fileHandle readDataToEndOfFile];
            NSLog(@"内容 ==  %@",_sendData);
            
        }
    }
}

7,当设备(开发版)断开的时候,监测;

- (void)postUserNotificationForDisconnectedPorts:(NSArray *)disconnectedPorts
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_7)
    if (!NSClassFromString(@"NSUserNotificationCenter")) return;
    
    NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
    for (ORSSerialPort *port in disconnectedPorts)
    {
        NSUserNotification *userNote = [[NSUserNotification alloc] init];
        userNote.title = NSLocalizedString(@"侦测到串口线断开", @"侦测到串口线断开");
        NSString *informativeTextFormat = NSLocalizedString(@"串口设备 %@ 已从你的 Mac电脑断开物理连接.", @"Serial port disconnected user notification informative text");
        userNote.informativeText = [NSString stringWithFormat:informativeTextFormat, port.name];
        userNote.soundName = nil;
        [unc deliverNotification:userNote];
    }
#endif
}

8,当设备(开发版)重链接的时候,监测;

- (void)postUserNotificationForConnectedPorts:(NSArray *)connectedPorts
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_7)
    if (!NSClassFromString(@"NSUserNotificationCenter")) return;
    
    NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
    for (ORSSerialPort *port in connectedPorts)
    {
        NSUserNotification *userNote = [[NSUserNotification alloc] init];
        userNote.title = NSLocalizedString(@"侦测到串口线连接", @"侦测到串口线连接");
        NSString *informativeTextFormat = NSLocalizedString(@"串口设备 %@ 已经连接到你的 Mac电脑.", @"Serial port connected user notification informative text");
        userNote.informativeText = [NSString stringWithFormat:informativeTextFormat, port.name];
        userNote.soundName = nil;
        [unc deliverNotification:userNote];
    }
#endif
}

9.把核心的代码以上阐述了,点击运行,看图:
在这里插入图片描述
在这里插入图片描述
10.把写好的桌面程序,打包成dmg,可以参考此文章:打包项目的链接

此文章先描述到此为止了,希望能帮助到更多的看官初学者,如果有任何疑问可以留言,相互学习,谢谢,一起努力,下期见~~

源码:

本项目的Demo,详细的源码

发布了63 篇原创文章 · 获赞 38 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_37523448/article/details/102485318
今日推荐