KatherineJean:
I am fairly new to arudino. I have written/put together from existing code a program that grabs data from several sensors. It then does math, conversions etc to this data, and then using Serial1.print I output via TX1 to NI LabVIEW. The problem is, for example one of the sensors gives heading, so sometimes this may be 56 and sometimes it might be 234. The receiving program needs to know how long to expect the string to be, otherwise it cuts some off. If I tell it to expect a string too long, stuff gets cut off on the next loop. After trial and error, Ive come to the conclusion the problem is on the arudnio side. At first I thought of converting the data to binary, that way, each piece of data will always be the same length, however this will not work for two reason 1) Some values, such as heading, are larger then the the largest binary value I can get. 2) Many of these values have decimal components.
Any suggestions would be greatly appreciated
How about this: Example, you want every value to be exactly 4 digits with leading zeros if necessary:
char buffer [32]; // must be large enough to hold the entire string
int rpm; // example rpm can range from 0 to over 3000
// function to send string to serial port
void sendData (int dat)
{
sprintf (buffer, "RPM: %04d\n", dat);
Serial.print (buffer);
}
// this part of your program reads the RPM
rpm = sensorRead (num); // example get data from your code
sendData (rpm); // send data to serial port
This example will do the following:
If RPM is 0, it prints:
[b]RPM: 0000[/b]
If RPM is 100:
[b]RPM: 0100[/b]
If RPM is 2500:
[b]RPM: 2500[/b]
If RPM is 30000 (note string got longer)
[b]RPM: 30000[/b]
Notice the "%04d" formatting. The format code "%d" means "print an int".
The 4 means print 4 characters regardless
The 0 means fill unprinted spaces with "0"
So, "%d" would just print like this:
[b]0
100
2500
30000[/b]
If you used "%4d" it would do this:
[b] 0
100
2500
30000[/b]
Finally the "0" in "%04d" means fill the spaces with zero:
[b]0000
0100
2500
30000[/b]
For other kinds of data types, different formatting codes are used. Google "printf formatting" for more info.
Lastly, the stupid Arduino IDE setup doesn't support floating point numbers by default. If you try to print a float value by using something like "%5.2f", all it will do is print a question mark.
This can be fixed by editing and recompiling the IDE to send the proper GCC command line or else you can use the "dtostrf()" function (see here): dtostrf() alternative for concatenating a float and a string - Programming Questions - Arduino Forum
Hope this helps!