Pages: [1] 2   Go Down
Author Topic: Combining Serial prints ... HELP!  (Read 1321 times)
0 Members and 1 Guest are viewing this topic.
Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am working on a time sensitive application, and I need to cut down on the time-consuming Serial.print() function in the following code:
Code:
          distance = getDistance(Lsensor);
          pressure = getPressure(Psensor);
          prSwitch = getPswitch(Pswitch);
          temp = getTemperature(tmp);
          Fldtemp = getTemperature(Fluidtmp);

          Serial.print(1);
          Serial.print(" ");
          Serial.print(millis());
          Serial.print(" ");
          Serial.print(distance);
          Serial.print(" ");
          Serial.print(pressure);
          Serial.print(" ");
          Serial.print(prSwitch);
          Serial.print(" ");
          Serial.print(temp);
          Serial.print(" ");
          Serial.println(Fldtemp);
All the functions return int's.
I want to combine all of these variables into a string and spit them out on serial print in one shot.
I hope it can be done.

Thanks in advance
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3693
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at sprintf. Also, see if you can run at a higher baud rate.
Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a bit confused on what sprintf() does.
could someone write a sample code combining two int variables and sending over serial?

Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26280
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am working on a time sensitive application, and I need to cut down on the time-consuming Serial.print() function in the following code:
Not sure what you're asking here; compared to the time taken to transmit the character, the time taken in the transmit routine itself is negligible.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL,

I need a way to send all the variables that I am sending using serial prints, in one call of serial println.
Is there a way to combine the variables that I have in a "data type" (not necessarily string) and send it via serial.println()?

Thanks
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char buffer[100]; //This is where the data will be merged into. Ideally you should make this as small as you can whilst still having enough room
sprintf(buffer,"1 %ul %d %d %d %d %d", millis(), distance, pressure, prSwitch, Fldtemp); //merge together
Serial.println(buffer); //send

Basically sprintf has a set of special characters (the %_ where the _ is a letter(s)). As it works along it replaces each of these with the corresponding variable (first % is the first variable, second % is the second variable and so on).

Each of the special charaters formats the variable in a different way. For example %d takes an integer and converts it to a string of numbers. %c takes a byte and prints the corresponding ascii character (so 30 would print ' ' (space)). %ul takes an unsigned long type (which is why it is used for millis()).
There are others as well, you would have to look on the cplusplus reference (if you google sprintf() you should find it).

Note that in my example as I don't know the variable types of your numbers I have assumed that they are all bytes or integers (so %d) except for millis() which I know returns an unsigned long.
Logged

~Tom~

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Tom for your example. And yes... all my vars are ints

I am wondering if there is space character between the variables in your example.

Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26280
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need a way to send all the variables that I am sending using serial prints, in one call of serial println.
Why?
What difference does it make?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need a way to send all the variables that I am sending using serial prints, in one call of serial println.
Why?
What difference does it make?

That would be my question also. The speed of character leaving the arduino is not going to be much different no matter which method you use and I like easy to read code over more complex combined print statements.

Lefty
Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

because it take a lot time sending the amount of data I have.
I am sampling all those variables at 1 ms period
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What baud rate are you using?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26280
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But the sketch will still stall if you fill the transmit buffer, whichever approach you take.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using the maximum baud rate 115200.

Oh ... and how big is the transmit buffer?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26280
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Prior to 1.0, there was only the USART register as a buffer.
1.0 and after, there's a 64 byte buffer, plus the USART.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you bothered to work out the math for this?

at 115,200 baud, each bit is about 9 uS
A byte is 72 uS
A 4 byte int is 288 uS

Even if you usually send less than the full 4 bytes worth, it looks like there is no way to send all your data in less than a millsecond.
(I did not even include start bits!)
It might be a bit faster to send the data as hex bytes to cut down the conversion time, but that is probably not your problem.
Logged

Pages: [1] 2   Go Up
Jump to: