swift - cocoa应用程序中关于串口获取操作不允许的Xcode C++代码

标签 swift xcode macos c++11 serial-port

我在 Xcode 控制台中使用 C++ 项目构建了一个代码,之前工作得很好:

Photo

#include "SerialPort.hpp"
#include "TypeAbbreviations.hpp"
#include <iostream>

int main(int argc, const char * argv[]) {

    //* Open port, and connect to a device
    const char devicePathStr[] = "/dev/tty.usbserial-A104RXG4";
    const int baudRate = 9600;
    int sfd = openAndConfigureSerialPort(devicePathStr, baudRate);
    if (sfd < 0) {
        if (sfd == -1) {
            printf("Unable to connect to serial port.\n");
        }
        else { //sfd == -2
            printf("Error setting serial port attributes.\n");
        }
        return 0;
    }

    // * Read using readSerialData(char* bytes, size_t length)

    // * Write using writeSerialData(const char* bytes, size_t length)

    // * Remember to flush potentially buffered data when necessary

    // * Close serial port when done
    const char dataToWrite[]="abcd";
    char databuffer[1024];

    while(1){
        readSerialData(databuffer, 4);
        sleep(2);
        writeSerialData(databuffer, 4);
        sleep(2);

    }

    printf("end.\n");
    return 0;
}

在此构建之后,我尝试使用下面的 C++ 包装器将其迁移到我的 Xcode cocoa 应用程序。

Photo

我非常确定我的 Wrapper 可以与测试 C++ 代码配合良好。这意味着,我可以从 ViewController.swift 调用 C++ 函数。

但是发生了一件奇怪的事情。我无法使用以下代码打开连接:

    sfd = open(portPath, (O_RDWR | O_NOCTTY | O_NDELAY));
    if (sfd == -1) {
        printf("Unable to open serial port: %s at baud rate: %d\n", portPath, baudRate);
        printf("%s", std::strerror(errno));
        return sfd;
    }

返回错误消息:

Unable to open serial port: /dev/tty.usbserial-A104RXG4 at baud rate: 9600
Operation not permitted 

我尝试更改应用程序沙箱配置,设置我的系统偏好设置以授予对我的应用程序的访问权限,同时我还禁用了无根。 (csrutil 使用 command + R 禁用) 但问题仍然存在:

Problem & Problem

我想问一下:
1. 为什么我的代码在 Xcode C++ 项目上工作正常,但在 Xcode 上的 swift's cocoa 应用程序上失败?
2. 如何解决“不允许操作”问题。

我的 Xcode 版本是 11.3.1,Mac OS 版本是 10.14.6 Mojave。

最佳答案

我自己解决了。

它的APP沙箱让我很困扰。 您需要做的就是关闭沙箱 通过单击鼠标点上的 X 将其关闭。 如果您想将其添加回来,只需单击“+功能”并将其重新打开即可。

/image/ZOc18.jpg

引用:https://forums.developer.apple.com/thread/94177#285075

关于swift - cocoa应用程序中关于串口获取操作不允许的Xcode C++代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59998744/

相关文章:

swift - resignFirstResponder 仅当所有文本字段都已填充时

swift - 事件指示器未正确显示

ios - 带有并发进程错误的秒表 iOS 应用程序

iphone - 将 NSDate 设置为特定日期、时间和时区

ios - 是否可以针对一个特定的 viewController 的不同目标使用不同的 xib?

ios - 无法通过 CocoaPods 安装 pod (Mixpanel) - 编译失败(找不到文件) - 桥接头问题?

python - pyinstaller 没有名为 pyinstaller 的模块

swift - 在 swift 中将可变长度消息的高度设置为 UIlabel 并不总是正确的

mysql - 无法在 macOS Sierra 上安装 mysql2 gem

macos - 导入错误:无法从 'SourceDistribution' 导入名称 'pip._internal.distributions.source'