dadycoke
Posts: 8
Joined: Thu Feb 14, 2013 8:24 am

communication Raspberry arduino

Thu Feb 14, 2013 8:33 am

Bonjour,

j'essaie de faire communiquer ma raspberry avec mon arduino uno via le port usb.
Pour ca j'ai un petit programme en C pour l'UART. Cependant je recois bien ce qui est envoyé par l'arduino mais la raspberry ne peut pas envoyer.

Pourriez vous m'aidez s'il vous plais .
Voila le code de l' UART

#include <stdio.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART

//Setting Up The UART


//-------------------------
//----- SETUP USART 0 -----
//-------------------------
//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
int main() {
int uart0_filestream = -1;

uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
struct termios options;
tcgetattr(uart0_filestream, &options);
cfsetispeed(&options, B9600); //<Set baud rate
cfsetospeed(&options, B9600); //<Set baud rate
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR | ICRNL;
options.c_oflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);


//Transmission

//----- TX BYTES -----
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;

p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = 'H';
*p_tx_buffer++ = 'e';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'o';

if (uart0_filestream != -1)
{
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
if (count < 0)
{
printf("UART TX error\n");
}
}
//----- CHECK FOR ANY RX BYTES -----
if (uart0_filestream != -1)
{
// Read up to 255 characters from the port if they are there
unsigned char rx_buffer[256];
int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
if (rx_length < 0)
{
//An error occured
printf("UART RX error\n");
}
else if (rx_length == 0)
{
//No data waiting
}
else
{
//Bytes received
rx_buffer[rx_length] = '\0';
printf("%i bytes read : %s\n", rx_length, rx_buffer);
}
}
return 0;
}

Merci

totoharibo
Posts: 4212
Joined: Thu Jan 24, 2013 8:43 am

Re: communication Raspberry arduino

Thu Feb 14, 2013 9:29 am

pas regardé mais il y a un article dans le MagPi N°7 sur le sujet
(c'est du python)

mras2an
Posts: 5
Joined: Tue Sep 18, 2012 3:45 pm

Re: communication Raspberry arduino

Fri Feb 15, 2013 11:22 am

salut,

cela marche bien sur beagleboard donc à tester raspberry.
https://github.com/ynezz/librs232/blob/ ... 232_test.c

Return to “Français”