Go Down

Topic: Ard <--> Pi serial data transfer(unsolved) (Read 4 times) previous topic - next topic

Cyric

nice idea but i seem to be having trouble sending any stuct to the Pi and reading it back into the stuct. think it has to do with how big the bytes for vars are on Pi and Arduino. int on arduino is 2 and 4 on PI but floats are both 4 bytes so it's very confusing to me at this point. was hoping some one had sent structs back and forth and had some code for it.

Cyric

So this is my sending (arduino) side
Code: [Select]


struct MyStruct_t {
  double imuRoll;  //MinIMU-9 data
  double imuPitch;//MinIMU-9 data
  double imuYaw; //MinIMU-9 data
  long flAt;  // GPS latitude
  long flOn; // GPS Longitude
  //char GPSs{300]="";
  unsigned int FC_AP;     //Front Center Ultrasonic Avrage Ping
  unsigned int FL_AP;     //Front Left Ultrasonic Avrage Ping
  unsigned int FR_AP;     //Front Rgith Ultrasonic Avrage Ping
  unsigned int RC_AP;     //Rear Center Ultrasonic Avrage Ping
  int FCir;       //Front Center Infrared sen
  int FLir;       //Front Left Infrared sen
  int FRir;       //Front Right Infrared sen
  int RCir;       //Rear  Center Infrared sen
  double BSTempature;       //Barometric sen tempature
  int BSPressure;      //Barometric sen pressure
  int Heading;         //LM303 mag heading
}MyStruct;
void setup(){
Serial.begin(57600);
Serial3.begin(57600);
}
char sendBuff[90], tmpbuff[10];
void loop() {
    fillStruct();
    makeString();
    sendStr();
}

void sendStr(){
//   for (int i = 0; i < sizeof(sendBuff); i++){
   Serial.println(sendBuff);
   Serial3.println(sendBuff);
  // } 
// Serial.println("");
// Serial3.println("");   
}
void fillStruct(){
    MyStruct.imuYaw = 111.456;
    MyStruct.imuPitch = -222.679;
    MyStruct.imuRoll = 333.012;
    MyStruct.flAt = 43.475743;
    MyStruct.flOn = -76.429243;
    MyStruct.FC_AP = 00;     //Front Center Ultrasonic Avrage Ping
    MyStruct.FL_AP = 11;     //Front Left Ultrasonic Avrage Ping
    MyStruct.FR_AP = 22;     //Front Rgith Ultrasonic Avrage Ping
    MyStruct.RC_AP = 33;     //Rear Center Ultrasonic Avrage Ping
    MyStruct.FCir = 1;       //Front Center Infrared sen
    MyStruct.FLir = 1;       //Front Left Infrared sen
    MyStruct.FRir = 0;       //Front Right Infrared sen
    MyStruct.RCir = 1;       //Rear  Center Infrared sen
    MyStruct.BSTempature = 99.1;       //Barometric sen tempature
    MyStruct.BSPressure = 30000;      //Barometric sen pressure
    MyStruct.Heading = 292;         //LM303 mag heading
   
}
void makeString(){
  strcpy(sendBuff, "<");
  dtostrf(MyStruct.imuYaw, 4,2, tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  dtostrf(MyStruct.imuPitch, 4,2, tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  dtostrf(MyStruct.imuRoll, 4,2, tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, "::");
  (MyStruct.flAt,3,6,tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  dtostrf(MyStruct.flOn,3,6,tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.Heading,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, "::");
  itoa(MyStruct.FC_AP,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.FL_AP,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.FR_AP,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.RC_AP,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, "::");
  itoa(MyStruct.FCir,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.FLir,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.FRir,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.RCir,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, "::");
  dtostrf(MyStruct.BSTempature,2,2,tmpbuff);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ",");
  itoa(MyStruct.BSPressure,tmpbuff,10);
  strcat(sendBuff, tmpbuff);
  strcat(sendBuff, ">");
  sendBuff[90] = '\0';

}

Arduino output is
Code: [Select]

<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>
<111.46,-222.68,333.01::333.01,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000>

dunno why lat and long are so empty after the dec but o well for now.
minicom on Pi shows the data the same at /dev/ttyAMA0 so i know it's hooked up right.


this is my PI side
Code: [Select]

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>

void serialRead();
void prntBuff();

float yaw, pitch, roll;
int fd, z, x, sDA;
char cChar, recBuff[90];
int main ()
{
  if ((fd = serialOpen ("/dev/ttyAMA0", 57600)) < 0)
  {
   fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

for (;;)
        {
         x = 0;
         cChar = serialGetchar(fd);
        printf("Wating for data\n");
        if(cChar == '<') serialRead();
         serialRead();
}
}

void serialRead(){
        sDA = serialDataAvail(fd);
        while (cChar != '>'  ){
           if (x < sizeof(recBuff)-1){
             printf("Getting data!\n ");
             cChar = serialGetchar(fd);
             recBuff[x] = cChar;
             x++;
             }
          else if(sDA == 0){
             printf("Got no Data\n");
             }
            break;
            }

        if(cChar == '>'){
        printf("End of data\n");
        x++;
        recBuff[x] = '0';
        serialFlush(fd);
        prntBuff();

    }
}
void prntBuff(){
//          for(z = 0;z < sizeof(recBuff);z++){
            printf("%s\n",recBuff);
//              }
}


Pi output is this.
Code: [Select]

Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
End of data
>00
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!


am i filling the array wrong or printing it wrong?

PaulS

Code: [Select]
  long flAt;  // GPS latitude
  long flOn; // GPS Longitude


and yet you are assigning float values to them and using dtostrf (in one case, anyway) to convert them to strings. Why?

Code: [Select]
  (MyStruct.flAt,3,6,tmpbuff);
What's this supposed to do?

Cyric


Code: [Select]
  long flAt;  // GPS latitude
  long flOn; // GPS Longitude


and yet you are assigning float values to them and using dtostrf (in one case, anyway) to convert them to strings. Why?

Code: [Select]
  (MyStruct.flAt,3,6,tmpbuff);
What's this supposed to do?

LOL was in the middle of editing when i cut and pasted the code. as for the dtostrf it was in there from the cut and past for the fill buffer part and i passed it at this point -74.00000 is fine i am more interested in parsing the string back into values on the Pi side. ill look into fixing the dtostrf or what ever i need to use on the GPS data. prob just going to append the full GPRMC on anyhow or trans it every other send.
any who all moot when my RX code is failing.

Cyric

so im kind of reading it in now. here is the PI side output
Code: [Select]

Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
End of data
30000>
<111.46,-222.68,333.01::43.000000,-76.000000,292::0,11,22,33::1,1,0,1::99.10,30000
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!
Wating for data
Getting data!

here is the Pi code
Code: [Select]

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>

void serialRead();
void prntBuff();

int fd, sDA;
char cChar, recBuff[90];
int main ()
{
  if ((fd = serialOpen ("/dev/ttyAMA0", 57600)) < 0)
  {
   fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

for (;;)
        {
        cChar = serialGetchar(fd);
        printf("Wating for data\n");
        if(cChar == '<') serialRead();
         serialRead();
}
}

void serialRead(){
        sDA = serialDataAvail(fd);
        while (cChar != '>'  ){
             cChar = serialGetchar(fd);
             printf("Getting data!\n ");
             read(fd,recBuff,sizeof(recBuff));

        if(sDA == 0){
          printf("Got no Data\n");
          }
            break;
            }

        if(cChar == '>'){
        printf("End of data\n");
        serialFlush(fd);
        prntBuff();
    }
}
void prntBuff(){
            printf("%s\n",recBuff);
}

decided to try using read and write to the buffer now i need to "sync"? and better "start /stop" bits?any ideas would be great. ( thinking i need to flush my buffer, so like tmpbuff sizeof (recBuff) get the start bit to the stop bit parse the actual data into recBuff then NULL tempbuff and start over yes?. "maybe even set a value to tell the function how many chars to expect coming so like
$tart[77]::bla bla bla::[77]$top ,using 2 of them to make sure i got the rite values and has not been corrupted?") any how now i need a better trigger and end system tho. but the read/write to fd should be good yes?

Go Up