我在 Xcode
控制台中使用 C++
项目构建了一个代码,之前工作得很好:
#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 应用程序。
我非常确定我的 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
禁用)
但问题仍然存在:
&
我想问一下:
1. 为什么我的代码在 Xcode C++
项目上工作正常,但在 Xcode
上的 swift's cocoa
应用程序上失败?
2. 如何解决“不允许操作”
问题。
我的 Xcode 版本是 11.3.1,Mac OS 版本是 10.14.6 Mojave。
最佳答案
我自己解决了。
它的APP沙箱让我很困扰。 您需要做的就是关闭沙箱 通过单击鼠标点上的 X 将其关闭。 如果您想将其添加回来,只需单击“+功能”并将其重新打开即可。
关于swift - cocoa应用程序中关于串口获取操作不允许的Xcode C++代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59998744/