
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 CserialfileDescriptor = open( "/dev/tty.usbmodemfa131",O_RDWR | O_NOCTTY | O_NONBLOCK );struct termios options;// Block non-root users from using this portioctl(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 porttcgetattr(serialfileDescriptor,&options);// Setting raw-mode allows the use of tcsetattr() and ioctl()cfmakeraw(&options);speed_t baudrate = 9600;// Specify any arbitrary baud rateioctl(serialfileDescriptor,IOSSIOSPEED,&baudrate);NSLog (@"before");sleep (5); // Wait for the Arduino to restartNSLog (@"after");int val = 255;write(serialfileDescriptor,val,1);NSLog (@"after2");
因此,当我运行应用程序时,它会等待五秒钟,但随后会冻结.控制台中的输出是这样的:
beforeafter
那么,我在这里做错了什么?
更新:所以,当我评论这一行时
fcntl(serialfileDescriptor,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全部内容,希望文章能够帮你解决objective-c – 无法使用Cocoa(IOKit)将数据发送到我的Arduino Uno所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)