2014-12-22 26 views
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 !!」,這個錯誤可能會有用。

+0

爲什麼您確信它停止在'open'如果不甚至打印第一張支票? – frarugi87

+0

這是http://stackoverflow.com/questions/11677639/two-way-c-communication-over-serial-connection同一個問題 - 你需要設置波特率和關閉硬件流控制。 – TomKeddie

回答

0

Arduino的顯示爲一個串行設備。你應該看看使用open()close()功能。我已經在Linux上完成了這個工作,但我很確定這在Mac上的工作方式類似。這是代碼片段的示例。第一個片段打開並設置文件描述符。

int fd;        // File descriptor 
// Open port 
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); 
if (fd == -1){ 
    printf("Device cannot be opened.\n"); 
    exit(-1);      // If the device is not open, return -1 
} 
struct termios options; 

fcntl(fd, F_SETFL, FNDELAY);     // Open the device in nonblocking mode 

// Set parameters 
tcgetattr(fd, &options);      // Get the current options of the port 
bzero(&options, sizeof(options));    // Clear all the options 
speed_t   Speed; 
switch (baudRate)        // Set the speed (baudRate) 
{ 
    case 110 :  Speed=B110; break; 
    case 300 :  Speed=B300; break; 
    case 600 :  Speed=B600; break; 
    case 1200 :  Speed=B1200; break; 
    case 2400 :  Speed=B2400; break; 
    case 4800 :  Speed=B4800; break; 
    case 9600 :  Speed=B9600; break; 
    case 19200 : Speed=B19200; break; 
    case 38400 : Speed=B38400; break; 
    case 57600 : Speed=B57600; break; 
    case 115200 : Speed=B115200; break; 
    default : exit(-4); 
} 
cfsetispeed(&options, Speed);     // Set the baud rate at 115200 bauds 
cfsetospeed(&options, Speed); 
options.c_cflag |= (CLOCAL | CREAD | CS8); // Configure the device : 8 bits, no parity, no control 
options.c_iflag |= (IGNPAR | IGNBRK); 
options.c_cc[VTIME]=0;       // Timer unused 
options.c_cc[VMIN]=0;       // At least on character before satisfy reading 
tcsetattr(fd, TCSANOW, &options);    // Activate the settings 

這只是關閉它:

close(fd); 

從實際文件描述符閱讀:

ioctl(fd, FIONREAD, &t1);       
if(t1 > 0) { 
    // If the number of bytes read is equal to the number of bytes retrieved 
    if(read(fd,pByte, t1) == t1) { 
     for(int i =0; i < t1; i++) { 
      if(pByte[i] != '\r'){ // Just makes sure you're not scanning new lines 
       // TODO: Do what you want with this character 
      } 
     } 
    } 
}