Je cherchais un moyen de lire des valeurs sur l'Arduino avec mon Mac depuis quelques temps...
En furetant sur le net j'ai trouvé ça :
How to talk to the USB Arduino board with your C++ Code and Xcode (Comment communiquer via USB avec Aduino en C++ et avec Xcode)
Le problème c'est que ce code montre bien comment envoyer des données à l'Arduino, mais pas comment les lire.
Ensuite j'ai trouvé ça :
Création d'une centrale interactive de capteurs
Projet dans lequel j'ai récupéré un bout de code pour modifier le premier.
Ce qui donne :
#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h> //write(), read()
using namespace std;
int fd;
/******************************************************************************************************************
Setting the serial port up to talk to the Arduino board
******************************************************************************************************************/
void init_port(int *fd, unsigned int baud)
{
struct termios options;
tcgetattr(*fd,&options);
switch(baud)
{
case 9600: cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
case 19200: cfsetispeed(&options,B19200);
cfsetospeed(&options,B19200);
break;
case 38400: cfsetispeed(&options,B38400);
cfsetospeed(&options,B38400);
break;
default:cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
}
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(*fd,TCSANOW,&options);
}
/******************************************************************************************************************
Main Function
******************************************************************************************************************/
int main()
{
char USBPort[1000];
cout << "what USB port is your Arduino Board pluged into?" << endl;
cout << "Example:\n/dev/tty.usbserial-A70041zl\nor\n/dev/tty.usbserial-A1001N2Y" << endl;
cin >> USBPort;
fd = open(USBPort, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
perror("open_port: unable to open port");
init_port(&fd,9600); //set serial port to 9600,8,n,1
fcntl(fd, F_SETFL, 0);
char readport();
int Temp,Temp1,Temp2 = 0;
char table[5]={0};
read(fd,&Val,5);
cout<<Val<< endl;
cout<<Val[2]<<Val[3]<<Val[4]<< endl;
cout<<Val[0]<<Val[1]<<endl;
Temp1 = Val[2]-'0'; // convert ASCII
Temp2 = Val[3]-'0'; // to
Temp3 = Val[4]-'0'; //decimal
Temp = Temp1*100 + Temp2*10 + Temp3 ;
cout<<endl<<Temp<<endl;
close (fd) //close USBPort
return 0;
}
Ainsi cela me permet de lire des données en ASCII envoyée par l'Arduino par ce code :
int Test1 = 0;
void setup()
{
Serial.begin(9600);
}
void loop () {
Temp1 =123;
Serial.print("ok");
Serial.println(Test1);
}
Et voilà ce qui sort à la console :
[Session started at 2008-03-21 19:49:38 +0100.]
what USB port is your Arduino Board pluged into?
Example:
/dev/tty.usbserial-A70041zl
or
/dev/tty.usbserial-A1001N2Y
/dev/tty.usbserial-A1001N2Y
ok123
123
ok
Executable “digit” has exited with status 123.
C'est peut-être rudimentaire, mais comme je ne connais pas le langage C (je commence à apprendre :)), c'est un grand pas pour moi.