Hi all,
Would you like to be able to print your serial data like this:
Serial.begin (9600);
const char *location = "outside";
int temperature;
temperature = ((readThermistor () * 1.8) + 32);
char cf = 'F';
fprintf (stdout, "Temperature %s is %d degrees %c\n", location, temperature, cf);
...instead of this:
Serial.begin (9600);
const char *location = "outside";
int temperature;
temperature = ((readThermistor () * 1.8) + 32);
char cf = 'F';
Serial.print ("Temperature ");
Serial.print (location);
Serial.print (" is ");
Serial.print ((int) temperature);
Serial.print (" degrees ");
Serial.print ((char) cf);
Serial.println ();
Well, it's simple, and what's more it's automatic. You open a serial device, stdin/out/error are automatically created. Close the serial device and they are destroyed. Open a different serial device (like Serial2 on a MEGA) and the stdin/out/err are automatically connected to that port!
First step: Go to your Arduino directory located at "[b]arduino-1.x.x/hardware/arduino/cores/arduino[/b]
" and make backup copies of HardwareSerial.cpp and HardwareSerial.h. Be sure the backups don't have a valid "C" style name (i.e. CPP, C, H, INO, etc) or else they will also get pulled in during a compile and cause an error. I suggest these names:
[b]HardwareSerial.cpp -> HardwareSerial.cpp.bak
HardwareSerial.h -> HardwareSerial.h.bak[/b]
Next, download the attached ZIP file containing "Stdinout.cpp" and "Stdinout.h". Unzip these files into a temporary directory and then COPY them to your Arduino directory located at "[b]arduino-1.x.x/hardware/arduino/cores/arduino[/b]
". Then open "HardwareSerial.cpp" and add a few lines:
Around line 348 or so is the function void HardwareSerial::begin (unsigned long baud).
Scroll down to the end of that function, looking for lines like this and INSERT the one line shown in blue:
[b] // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting;
transmitting = false;
sbi (*_ucsrb, _rxen);
sbi (*_ucsrb, _txen);
sbi (*_ucsrb, _rxcie);
cbi (*_ucsrb, _udrie);
[color=blue]STDIO.open (this);[/color] [color=red]// <-- INSERT HERE[/color]
}[/b]
Next will be another "Hardware::Serial::begin". Afain, scroll to the bottom of this function and ADD the line shown in blue.
[b] //set the data bits, parity, and stop bits
#if defined (__AVR_ATmega8__)
config |= 0x80; // select UCSRC register (shared with UBRRH)
#endif
*_ucsrc = config;
sbi (*_ucsrb, _rxen);
sbi (*_ucsrb, _txen);
sbi (*_ucsrb, _rxcie);
cbi (*_ucsrb, _udrie);
[color=blue]STDIO.open (this); // connect stdin/out/err to current serial stream[/color]
}
[/b]
Now, scroll down a bit more to "HardwareSerial::end()" and add the line in blue:
[b]void HardwareSerial::end (void)
{
// wait for transmission of outgoing data
while (_tx_buffer->head != _tx_buffer->tail);
cbi (*_ucsrb, _rxen);
cbi (*_ucsrb, _txen);
cbi (*_ucsrb, _rxcie);
cbi (*_ucsrb, _udrie);
// clear any received data
_rx_buffer->head = _rx_buffer->tail;
[color=blue]STDIO.close (); // flush, close and disconnect std streams[/color]
}[/b]
Now, close HardwareSerial.cpp and open HardwareSerial.h.
Near the top, add these two lines shown in blue:
[b]#include <stdio.h>
#include <inttypes.h>
#include "Stream.h"
[color=blue]#include "Stdinout.h"
static STDINOUT STDIO; /* object to connect standard in/out/err to */[/color]
struct ring_buffer;
class HardwareSerial : public Stream
[/b]
Now close HardwareSerial.h. You are done! Now, to be sure it works, close, then re-open your Arduino IDE (if it was open) and try this sketch:
void setup (void)
{
Serial.begin (9600) // whatever baud rate YOU want is OK
Serial.println ("Regular function works!");
printf ("New function works too!\n");
}
void loop (void);
Both lines should print. If you get an error message from the IDE, or if only the first line prints, then recheck the steps above. If nothing works, PM me and I'll try to help you. If all else fails, simply revert to your backed up versions of HardwareSerial.cpp and .h
Good luck!
stdinout.zip (816 Bytes)