- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我目前正在尝试通过 Mac 应用程序向我的 Arduino 发送数据。我的代码 Arduino Uno看起来像这样:
void setup()
{
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
Serial.begin (9600);
}
void loop ()
{
digitalWrite (2, HIGH);
if (Serial.available () > 0)
{
int c = Serial.read();
if (c == 255)
{
digitalWrite (3, HIGH);
}
else
digitalWrite (4, HIGH);
}
}
这是我在 XCode 项目中的代码:
// Open the serial like POSIX C
serialFileDescriptor = open(
"/dev/tty.usbmodemfa131",
O_RDWR |
O_NOCTTY |
O_NONBLOCK );
struct termios options;
// Block non-root users from using this port
ioctl(serialFileDescriptor, TIOCEXCL);
// Clear the O_NONBLOCK flag, so that read() will
// block and wait for data.
fcntl(serialFileDescriptor, F_SETFL, 0);
// Grab the options for the serial port
tcgetattr(serialFileDescriptor, &options);
// Setting raw-mode allows the use of tcsetattr() and ioctl()
cfmakeraw(&options);
speed_t baudRate = 9600;
// Specify any arbitrary baud rate
ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
NSLog (@"before");
sleep (5); // Wait for the Arduino to restart
NSLog (@"after");
int val = 255;
write(serialFileDescriptor, val, 1);
NSLog (@"after2");
因此,当我运行该应用程序时,它会等待五秒钟,但随后会停止运行。控制台中的输出是这样的:
before
after
那么,我做错了什么?
更新:所以,当我注释掉这一行时
fcntl(serialFileDescriptor, F_SETFL, 0);
程序没有卡住,但我的 Arduino 仍然没有得到任何数据。
最佳答案
1) write() 调用的第二个参数不正确 - write() 需要一个指向要写入的字节的指针。要将数值变量的值写入字节,请传递变量的地址,而不是变量本身:
write(serialFileDescriptor, (const void *) &val, 1);
关于 write() 的更多信息: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/write.2.html
2) 对本地 termios 变量和选项的更改 - 例如对 cfmakeraw() 的调用 - 不会影响终端设置;为了使用更改后的选项更新终端设置,请调用 tcsetattr():
cfmakeraw(&options);
// ...other changes to options...
tcsetattr(serialFileDescriptor, TCSANOW, &options);
有关 Mac OS X 串行通信的更多信息: http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/SerialDevices.html
关于objective-c - 无法使用 Cocoa (IOKit) 将数据发送到我的 Arduino Uno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15903747/
所以一切都很好,然后我决定开始在 XCode 5 DP3 和 iOS7 上开发 在升级之前,我的项目在我的 iPhone 3GS 上编译并运行良好(我做了一个小软电话应用程序);然而,它没有在我的 i
我是 IOKit 的新手,并且注意到我认为是沙箱导致它失败。 这是我正在尝试的测试(在 Pascal 中),它在沙箱外运行良好,但当我启用它时,IOServiceOpen 每次都会返回错误 kIORe
我有 IOKit 基础驱动程序,它是非硬件触发的并且派生自 IOResources。它还使用 IOServiceOpen 向用户空间客户端类提供,但我认为这与我的问题无关......主驱动程序具有 I
我是一名开发人员,正在开发一个非常大的内存密集型 32 位应用程序。耗尽虚拟地址空间(内存)对我们来说是一个问题。在调查最近的一些问题时,我注意到有一大块内存被 IOKit (512MB) 保留。该内
如何使用 IOKit 获取 Mac 上的当前电源? 我想查看 Mac 是通过内置电池还是通过电源适配器运行。 最佳答案 IOPowerSources.h中的函数(具体来说,IOPSGetProvidi
我正在尝试掌握 IOKit,我觉得我已经很接近了,但还没有达到目标。所以请原谅我的困惑。 我已经成功编写了检测我的 USB 设备的代码(USB 电缆末端的一个简单按钮,有 Windows 驱动程序,但
我将尝试使用 IOKit 在 OS X 上为我的应用程序编写 USB 驱动程序。我选择了用户空间路线而不是开发 kext,因为它更容易调试等,但我在弄清楚如何测试驱动我的开发时遇到了困难。 如果可能,
我使用 IOKit API 检索 iOS 设备电池的原始数据。当我将我的 iPhone 更新到 iOS 10 测试版时,我发现它无法正常工作。只能显示电池电压,没有其他数据如maxCapacity、c
我正在使用:IOServiceGetMatchingServices kr = IOServiceGetMatchingServices(kIOMasterPortDefault, IOService
我正在使用 IOKit 框架通过用户空间客户端的 IOConnectCallMethod 和驱动程序端的 IOExternalMethodDispatch 与我的驱动程序通信。 到目前为止,我能够发送
我发现了几个出现相同问题的主题,但它们很老,而且大多数都没有明确的答案,或者答案仅适用于 iOS。还有一些消息来源说这是被禁止的,而其他人则不同意。 例如,here或 here about iOS .
我一直在尝试在添加或删除特定 USB 设备时收到通知。我已阅读“从应用程序访问硬件”文档,并有一个简单的演示应用程序,主要基于该文档中提供的代码。 第一次添加或删除设备时它会起作用,但此后我的回调永远
我目前正在 10.6.7 上开发一个应用程序,当插入新的 USB 设备时,该应用程序应该收到通知。我发现有一个 IOKit 函数可以处理“IOServiceAddMatchingNotificatio
我在使用 IOPowerSources 及其文档时遇到了非常困难的事情。 我的问题是,好吧,我无法弄清楚 IOPowerSources 的该死导入。我正在阅读: http://developer.ap
在编译基于 IOKit 的内核扩展时,c++ 编译器无法识别关键字 decltype。 int f = 123; int (*f_ptr)() = 0; f_ptr = (decltype(f_pt
我正在尝试了解 IOKit 以及它如何允许我访问 Swift 程序中的串行端口。 我现在正在操作的类如下: import Foundation import Cocoa import IOKit.se
我目前正在学习如何编写 os x 内核扩展(在 Xcode 中),如果我有任何错误,我深表歉意。我正在寻找一个示例,说明如何编写一个 IOKit 驱动程序,该驱动程序可以在系统中注册一个虚拟存储设备(
我编写了一个 Objective-C POS,它需要与许多以太网设备(如条形码扫描仪、燃油泵、现金抽屉等)交互。我知道我可能必须为每个设备用 C++ 编写驱动程序。问题是我不知道如何编写以太网设备驱动
我正在开发一个使用 IOKit 获取电池信息的调整,但它无法构建。 Undefined symbols for architecture armv7: "_IOPSCopyPowerSources
我正在为我的 Mac 应用程序使用沙盒。我需要像这样为 IOKit 设置一个属性, IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"),
我是一名优秀的程序员,十分优秀!