我正在尝试通过popen()函数与我的Arduino通信。我用XCode写了一个简单的Mac App:
- (IBAction)ButtonPressed:(id)sender {
popen("echo hello world > /dev/tty.usbmodem1411", "r");
}这是Arduino代码:
int redPin = 8;
int greenPin = 9;
int bluePin = 10;
int inByte = 0; // for incoming serial data
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(8000);
}
void loop()
{
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
Serial.print(inByte);
setColor(inByte,inByte,inByte);
delay(1000);
setColor(0,0,0);
}
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}现在,当我尝试使用Arduino串行监视器并在那里写一些胡言乱语时,我的LED灯亮得很好,我看到它正在工作。当我运行我的XCode程序时,按下按钮,没有任何反应。我确实设置了断点,并且检查了代码行是否被执行。我仔细检查了串口,一切正常,但不走运。它仍然不能工作。
发布于 2013-10-07 23:48:49
基本上,我使用IOKit/ioctl解决了这个问题。我仍然不确定为什么popen()不能工作,可能是因为Arduino IDE正在使用这个端口,因此它不可用,但是IOKit工作得很棒。此外,它允许更多的控制和灵活性,您可以在开始之前实际搜索端口,看看它们是否可用。如果有人遇到同样的问题,请查看http://playground.arduino.cc/Interfacing/Cocoa#IOKit。感谢大家的帮助。
https://stackoverflow.com/questions/19182129
复制相似问题