5
用C++的Arduino和Xcode我已經提出了類似的問題。這是在C一個簡單的程序++至極發送通過串行端口的Arduino的角度,並且該Arduino的角度適用於伺服電機。打開一個串行端口兼容Mac
這是C++代碼
#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
int main()
{
unsigned int angle;
fstream arduino;
cout<<"check-1";
arduino.open("/dev/tty.usbmodem3a21");
cout<<"check-2";
if(arduino)
{
do
{
cout<<"\n\ninsert a number between 0 and 179";
cin>>angle;
arduino<<angle;
}while(angle <= 179);
arduino.close();
}
else
{
cout<<"\n\nERROR!!\n\n";
}
}
,這是Arduino的公司:
#include <Servo.h>
Servo servo;
const int pinServo = 2;
unsigned int angle;
void setup()
{
Serial.begin(9600);
servo.attach(pinServo);
servo.write(0);
}
void loop()
{
if(Serial.available()>0)
{
angle = Serial.read();
if(angle <= 179)
{
servo.write(angle);
}
}
}
的問題是,它停在arduino.open(...)
,我不知道爲什麼,它甚至不寫「check-1」,我也確定這是在工具>串口中arduino應用程序中選擇的端口。
知道如果我寫arduino.open("/dev/tty.usbmodem3a21",iOS::binary)
或者我寫錯誤的串口名稱它會寫入「check-1」,「check-2」和「ERROR !!」,這個錯誤可能會有用。
爲什麼您確信它停止在'open'如果不甚至打印第一張支票? – frarugi87
這是http://stackoverflow.com/questions/11677639/two-way-c-communication-over-serial-connection同一個問題 - 你需要設置波特率和關閉硬件流控制。 – TomKeddie