0
votes

I have a raspberry pi and an arduino. So far I have been able to have the Pi send data to the arduino using serial communication, however it only send one variable and I have multiple variables(2) that I want to send to the arduino (x,y coordinates). Does anyone know if this is possible. I want the first number that is sent from the pi to be the x and the second one the y and the next one the x of the next coord ect.

I have tried editing the code that I use to send one variable but it doesn't work.

Any help would be awesome

1
Welcome to Stack Overflow! May I refer you to this: meta.stackexchange.com/questions/156810/…Jonathan Root
Although you did describe your problem, it is difficult to help you without knowing what your code looks like. Visit here for help with formatting code into your question. It may also be helpful to use a jsFiddle to help illustrate your point.Cody Guldner

1 Answers

2
votes

Consider the following method to send 2 variable at the same time:

    int xpos, ypos;

    char x_tx_buffer[20], y_tx_buffer[20];
    char x_dummy_buffer[20];
    char y_dummy_buffer[20];
    char *p_x_tx_buffer, *p_y_tx_buffer;

    sprintf(x_dummy_buffer,"%d", xposs);
    sprintf(y_dummy_buffer,"%d", yposs);

    p_x_tx_buffer = &x_tx_buffer[0];
    *p_x_tx_buffer++ = x_dummy_buffer[0];
    *p_x_tx_buffer++ = x_dummy_buffer[1];
    *p_x_tx_buffer++ = x_dummy_buffer[2];
    *p_x_tx_buffer++ = x_dummy_buffer[3];

    p_y_tx_buffer = &y_tx_buffer[0];
    *p_y_tx_buffer++ = y_dummy_buffer[0];
    *p_y_tx_buffer++ = y_dummy_buffer[1];
    *p_y_tx_buffer++ = y_dummy_buffer[2];
    *p_y_tx_buffer++ = y_dummy_buffer[3];
    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");
    }
    if (uart0_filestream != -1)
    {
        int countx = write(uart0_filestream, &x_tx_buffer[0], (p_x_tx_buffer - &x_tx_buffer[0]));       //Filestream, bytes to write, number of bytes to write
        int county = write(uart0_filestream, &y_tx_buffer[0], (p_y_tx_buffer - &y_tx_buffer[0]));       //Filestream, bytes to write, number of bytes to write
        if (countx < 0 || county < 0)
        {
            printf("UART TX error\n");
        }
    }
    close(uart0_filestream);

You can send a max of 8 bytes at a time. Keep that in mind and with that you can modify the about code to send your x and y values in the same uart0_filestream.

Good luck.