2014-12-22 8 views
5

Podobno już zadałem podobne pytanie. Jest to prosty program w języku C++, który wysyła kąt do arduino przez port szeregowy, a arduino stosuje ten kąt do silnika Servo.Otwórz port szeregowy z arduino za pomocą C++ z Xcode na Macu

to C++ kod

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


} 

a to Arduino na:

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

problemem jest to, że zatrzymuje się na arduino.open(...), a ja nie wiem dlaczego, to nawet nie pisać "check-1" i jestem też pewien, że jest to port wybrany w aplikacji arduino w narzędziach> port szeregowy.

Warto również wiedzieć, że jeśli napiszę arduino.open("/dev/tty.usbmodem3a21",iOS::binary) lub napiszę niewłaściwą nazwę portu szeregowego, wypisze on "check-1", "check-2" i "ERROR !!", więc błąd działa.

+0

Dlaczego jesteś pewien, że zatrzymuje się na 'open' jeśli nie nawet wydrukować pierwsze czek? – frarugi87

+0

To jest to samo pytanie, co http://stackoverflow.com/questions/11677639/twoj-way-c-communication-over-serial-connection - Musisz ustawić szybkość transmisji i wyłączyć sprzętową kontrolę przepływu. – TomKeddie

Odpowiedz

0

Arduino pojawia się jako urządzenie szeregowe. Powinieneś spojrzeć na użycie funkcji open() i close(). Zrobiłem to na Linuksie, ale jestem prawie pewien, że działa to podobnie na Macu. Oto przykładowy fragment kodu. Pierwszy fragment kodu otwiera i ustawia deskryptor pliku.

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 

To właśnie zamyka go:

close(fd); 

Aby dowiedzieć się od rzeczywistego deskryptora pliku:

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 
      } 
     } 
    } 
} 
Powiązane problemy