Jan*_*Jan 5 cocoa serial-port arduino objective-c iokit
我目前正在尝试通过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仍然没有得到任何数据.