Я создаю код в 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;
}
После этой сборки я попытался перенести его на свой Xcode
приложение какао с C++
обертками ниже.
Я почти уверен, что мой Wrapper
отлично работает с тестовым C++
кодом. Это означает, что я могу вызвать функцию C++
из моего ViewController.swift
.
Но произошла одна странная вещь. Я не могу открыть соединение с помощью следующего кода:
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
Я пытался изменить app sandbox configuration
, настроить мой system preference
для предоставления доступа к моему app
, также я отключил свой rootless
. (csrutil
отключить с помощью command + R
) Но проблема все еще сохраняется:
&
Я хочу спросить, что:
1. Почему мой код включен Xcode C++
Проект работает нормально, но не работает на swift's cocoa
приложении на Xcode
?
2. Как решить "Operation not permitted"
Проблема.
Моя Xcode
версия 11.3.1
и Mac OS
это 10.14.6 Mojave
.