Xcode, C ++ serial port with arduino
im making a very simple c ++ program that sends an angle to an arduino via a serial port and then arduino applies that angle to the servo. I know that Unix sees the serial port device as a file, in fact this is C ++ code:
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
int angole;
FILE * arduino;
do
{
arduino = fopen("/dev/tty.usbmodem3a21","w");
cout<<"\n\give me the angle\n\n";
cin>>angole;
fprintf(arduino,"%d",angole);
sleep(1);
}while(angole>=0 && angole<=179);
}
and this is arduinos:
#include <Servo.h>
Servo servo;
const int pinServo = 2;
int angle;
void setup()
{
Serial.begin(9600);
servo.attach(pinServo);
servo.write(0);
}
void loop()
{
if(Serial.available()>0)
{
angle = Serial.read();
servo.write(angle);
}
}
I also checked in the arduino app under tools> serial port> /div/tty.usbmodem3a21 that it was the correct port.
The problem is that the program stops at arduino = fopen ("/dev/tty.usbmodem3a21", "w"); because he doesn't even write the message "give me the corner".
for example, when I write the wrong port in an open function, it writes a message.
source to share
Indeed, "everything in Linux is a file", but not literally â the point is what type of file - in your case, you are considering the port as a regular vanilla file (that is, something like a txt file), while you need to handle this device file , so no fopen
, but:
fd = open("/dev/tty.usbmodem3a21", O_RDWR | O_NOCTTY | O_NDELAY);
below is a good link about the serial port file interface And this one is even arduino oriented
source to share
I downloaded the code, now this is Xcode (C ++):
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
int main()
{
string angle;
int arduino;
cout<<" i'm going to connect \n\n";
do
{
cout<<"\n\n i'm in the loop \n\n";
arduino = open("/dev/tty.usbmodem3a21", O_WRONLY);
cout<<"\n\n i'm going to check the connection\n\n";
if(arduino == -1)
{
cout<<"\n\n error \n\n";
}
else
{
do
{
cout<<"\n\n write a letter between a and c \n\n";
cin>>angle;
write(arduino,&angle,1);
}while(angle=="a" || angle=="b" || angle=="c");
}
}while(arduino == -1);
close(arduino);
}
and this is arduino:
#include <Servo.h>
Servo servo;
const int pinServo = 2;
char angle;
void setup()
{
Serial.begin(9600);
servo.attach(pinServo);
servo.write(0);
}
void loop()
{
if(Serial.available()>0)
{
angle = Serial.read();
if(&angle=="a")
{
servo.write(20);
}
else if(&angle == "b")
{
servo.write(90);
}
else if(&angle == "c")
{
servo.write(180);
}
else
{
servo.write(0);
}
}
}
And I still have the same problem: it writes "I'm going to connect" and "I'm in a loop" and then nothing, so it stops at the function open
and I think the code might have a few more problems as well.
Is there any C ++ code to recover some kind of feedback from arduino and what should it be?
I'm also sure it's not a matter of time to get connected, and I left it for a very long time.
source to share