2014-12-22 1 views
5

나는 이미 비슷한 질문을했습니다. 이것은 직렬 포트를 통해 arduino에 각도를 보내고 arduino가이 각도를 서보 모터에 적용하는 C++의 간단한 프로그램입니다.Mac에서 Xcode와 함께 C++ 및 arduino를 사용하여 직렬 포트 열기

이 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.open(...)에서 정지 점이다

#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); 
     } 
    } 
} 

, 그리고 난 이유조차 기록하지 않습니다 모른다 "check-1"이고 도구> 직렬 포트에서 arduino 앱에서 선택한 포트 인지도 확신합니다.

arduino.open("/dev/tty.usbmodem3a21",iOS::binary)으로 작성하거나 잘못된 직렬 포트 이름을 쓰면 "check-1", "check-2"및 "ERROR !!"로 표시되므로 오류가 발생합니다.

+0

:

close(fd); 

은 실제 파일 기술자에서 읽으려면 심지어 첫 번째 수표를 인쇄합니까? – 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 

이 그냥 종료 : 왜하지 않는 경우는`open`에서 정지 당신이 있는지입니다

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 
      } 
     } 
    } 
}