Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently working on a project on serial communication between RPi and Arduino. This is the code that I am using from RPi side, it's in C:

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

   //OPEN THE UART
   //The flags (defined in fcntl.h):
   //   Access modes (use 1 of these):
   //       O_RDONLY - Open for reading only.
   //       O_RDWR - Open for reading and writing.
   //       O_WRONLY - Open for writing only.
   //
   //   O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
   //                                           if there is no input immediately available (instead of blocking). Likewise, write requests can also return
   //                                           immediately with a failure status if the output can't be written immediately.
   //
   //   O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
   Arduino = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);           //Open in non blocking read/write mode
   if (Arduino == -1)
   {
       //ERROR - CAN'T OPEN SERIAL PORT
       printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
   }

   //CONFIGURE THE UART
   //The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
   //   Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
   //   CSIZE:- CS5, CS6, CS7, CS8
   //   CLOCAL - Ignore modem status lines
   //   CREAD - Enable receiver
   //   IGNPAR = Ignore characters with parity errors
   //   ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
   //   PARENB - Parity enable
   //   PARODD - Odd parity (else even)
   struct termios options;
   tcgetattr(Arduino, &options);
   options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;      //<Set baud rate
   options.c_iflag = IGNPAR;
   options.c_oflag = 0;
   options.c_lflag = 0;
   tcflush(Arduino, TCIFLUSH);
   tcsetattr(Arduino, TCSANOW, &options);

   //----- TX BYTES -----
   unsigned char tx_buffer[20];
   unsigned char *p_tx_buffer;
   int Angle=-20;
   int Speed=100;

   int Botschaft=Fusion(Crypt(Angle),Crypt(Speed));

   p_tx_buffer = &tx_buffer[0];
   *p_tx_buffer++ = Decompositer(Botschaft,1)+'0';
   *p_tx_buffer++ = Decompositer(Botschaft,2)+'0';
   *p_tx_buffer++ = Decompositer(Botschaft,3)+'0';
   *p_tx_buffer++ = Decompositer(Botschaft,4)+'0';
   *p_tx_buffer++ = Decompositer(Botschaft,5)+'0';
   *p_tx_buffer++ = Decompositer(Botschaft,6)+'0';


   if (Arduino != -1)
   {
      int count = write(Arduino, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));     //Filestream, bytes to write, number of bytes to write
      if (count < 0)
      {
        printf("UART TX error\n");
      }
   }

//----- CHECK FOR ANY RX BYTES -----
   if (Arduino != -1)
   {
      // Read up to 255 characters from the port if they are there
      unsigned char rx_buffer[256];
      int rx_length = read(Arduino, (void*)rx_buffer, 255);     //Filestream, buffer to store in, number of bytes to read (max)
      if (rx_length < 0)
      {
        //An error occured (will occur if there are no bytes)
      }
      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);
      }
   }

   //----- CLOSE THE UART -----
   close(Arduino);
}

int Fusion(int Angle, int Speed)
{
   int msg;
   msg=Angle*1000+Speed;
   return msg;
}

int Decompositer(int Num,int Place)
{
   int Result[5];

   Result[0]=Num/100000;
   Result[1]=(Num-Result[0]*100000)/10000;
   Result[2]=(Num-Result[0]*100000-Result[1]*10000)/1000;
   Result[3]=(Num-Result[0]*100000-Result[1]*10000-Result[2]*1000)/100;
   Result[4]=(Num-Result[0]*100000-Result[1]*10000-Result[2]*1000-Result[3]*100)/10;
   Result[5]=(Num-Result[0]*100000-Result[1]*10000-Result[2]*1000-Result[3]*100-Result[4]*10);
   return Result[Place-1];
}

int Crypt(int Value)
{
   int result;
   result = Value + 100;
   return result;
}

And this is my code from the Arduino side:

void setup()
{
    Serial.begin(9600);
    Serial2.begin(9600);
}

void loop(){
    if (Serial2.available())
    {
        int inByte = Serial2.read();
        Serial.write(inByte);
    }
    if (Serial.available())
    {
        int inByte = Serial.read();
        Serial2.write(inByte);
    }
}

My purpose is to send a 6 digit number to my Arduino side, and be able to read them and then separate them into 2 different numbers...

The problem is I see only the first 2 digits and the rest are weird characters like 1‚þ.

I really appreciate your help.

share|improve this question
    
Do you know just what it is your Pi program is actually sending out? – Majenko Dec 5 '15 at 10:51
    
I guess, Characters ? by the way I tried to set a higher baude rate and for some reason it works , I dont know why :/ – Mehdi Dec 5 '15 at 10:54
    
Was that 115200 baud perchance? – Majenko Dec 5 '15 at 11:11
    
Yes exactly! Is there something wrong with it? – Mehdi Dec 5 '15 at 11:15
1  
Here is how I use the serial port on Pi-like devices: github.com/majenkotech/SerialPort – Majenko Dec 5 '15 at 12:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.