I have one problem, how to recognise structure sended from arduino decimila board to pc. At the moment i have program, that loges information and these informations are gathered into structure datarecord - temperature, rpm, I, V,status, gps. But how to transmit, recognise these structure in pc software? I would like to get informations in pc software like datarecord.temperature, datarecord.rpm etc.
For pc software i use c++ for decimila i use ordinary Arduino language... The connection between board and pc is with xbee pro.
You could develop a function that receives a pointer to the structure and the length of it and use serial.print for each byte at a time.
Something like this:
void SendStructure( char *StructurePointer, int StructureLength)
{
int i;
for (i = 0 ; i < StructureLength ; i++)
serial.print(StructurePointer*, HEX);* } Something you should consider is integer and longs bytes swapping (if needed). I don't really know if AVG-GCC in ATmega168/8 uses little indian or big indian format. Juan C.
Part of the fun is knowing where the structure begins too.
This is probably easiest if you have the arduino listen on the com port for a "getdata" string then spit out the data in response. Then whenever the PC is ready to process another set of values it sends "getdata" to the arduino.
But there are plenty of other schemes as well, like a special set of bytes inserted between sets of data.
Oh, and the Arduino processor is little endian, meaning the lowest order addresses contain the least significant data. This means that you should be able receive binary data without byte swapping on most PC architectures, the most notable exception being the PowerPC chip, which is big endian.
I think it would be looking something like this on transsmition part ?
So i have to let know pc SW (software) how long is the structure, where to begin and end of recieving data? For the redy status of
"getdata" is just another if case needed for arduino to listen from pc.
if (serial.read()="getdata")
{
SendStructure((char *)&MyStruct, sizeof(MyStruct));
};
I am sitting behind a c++ books and studing how to solve that problem. i have not program for ages, so i have to renew my knowledge. Extra comments are welcome.
Ok and here is a program.
void setup();
struct MyStruct // structure of gathered data
{
int rpm[4];
int voltage[4];
int current[4];
int temp1[4];
int temp2[4];
char longitudeGps[8];
char altitudeGps[8];
char latitudeGps[8]
};
void SendStructure( char *StructurePointer, int StructureLength) //Function SendStructure
{
int i;
for (i = 0 ; i < StructureLength ; i++) // counting value i and sending data to serial, till it is lower than StructureLength
serial.print(StructurePointer*, HEX);* } Question. So if i am not wrong, by calling "SendStructure((char *)&MyStruct, sizeof(MyStruct));" values mirrors into values of function "SendStructure( char *StructurePointer, int StructureLength)" ? As i know (char *)&MyStruct points to the char *StructurePointer and sizeof(MyStruct) points to the int StructureLength? That is only one part of preparing to send data, what about pc SW to recieve? again writte another structure and mirroring recieved data intoo struct arduinoData = serial.read(), so that i can work with like arduino.temp1 , arduino.voltage,..... Thank you in advance and success in programing and developing;)
My C isn't the best either, but "if (serial.read()="getdata")" will need some work, you will have to put the read characters into a string then do a strcmp or something on it.
Also you need to wait for that string before each sendstructure, not just in setup.
void loop()
{
WaitForDataRequest(); //
SendStructure(...);
};
And if you dont want to worry about endian, or even possibly difference in the size of an int or all that, just send them as text numbers and atoi them on the pc if you need to do anything other than log them. You have character data in there already from the GPS, and you will be mixing in binary data in non human readable form
//this will make it real easy to troubleshoot using any terminal program and pc platform independent
SendStructure(){
Serial.println(rpm);
Serial.println(voltage);
Serial.println(current);
Serial.println(temp1);
Serial.println(temp2);
Serial.println(longitudeGps); //assuming zero terminated string
Serial.println(altitudeGps);
Serial.println(latitudeGps);
}
}
Putting all the data in a structure and serializing it is a neat trick, but not always the best idea when crossing hardware platforms.
I agree that dcb's solution will be much easier to debug and implement, even though you'll have to do a little parsing on the PC side.
Here's a suggestion: instead of sending the string "getdata" to signal that the PC is ready for data, how about just sending a single byte, like 'd'? Then you could easily write WaitforDataRequest() like this:
void WaitForDataRequest()
{
while (true)
if (Serial.available() && Serial.read() == 'd')
return;
}