I made a small tweak to the HardwareSerial library (IDE 1.0.1) to have the ability to use only RX, only TX, or both RX and TX (normal operation). The modifications are very simple and easy to use. I've shown the modifications below. Make a copy of Arduino.h, HardwareSerial.h and HardwareSerial.cpp located in folder \arduino-1.0.1\hardware\arduino\cores\arduino\ and save them to a folder somewhere so if you screw up you can copy the old files back to the original folder.
Make the changes where indicated to the files below.
Arduino.h
// Place the three statements below somewhere around line 20
// with the other #define's that are listed there.
#define RX_TX 0
#define RX_ONLY 1
#define TX_ONLY 2
HardwareSerial.h
// Change the declaration below (at line 52)
void begin(unsigned long);
// To this
void begin(unsigned long, uint8_t mode = RX_TX);
HardwareSerial.cpp
// Change the definition below (at line 294)
void HardwareSerial::begin(unsigned long baud)
// To this
void HardwareSerial::begin(unsigned long baud, uint8_t mode)
// Located at the end of the same function (above)
// Replace the statements below (at lines 328, 329, 330)
sbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie);
// With these
switch (mode)
{
case 1:
sbi(*_ucsrb, _rxen);
cbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie);
break;
case 2:
cbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen);
cbi(*_ucsrb, _rxcie);
break;
default:
sbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie);
break;
}
If you want keyword highlighting for the new constants, you can add a few lines to \arduino-1.0.1\lib\keywords.txt file, though it is not neccesary.
keywords.txt
// Right after the line below (at line 1)
# LITERAL1 specifies constants
// Place these three lines (Use tabs here, NOT spaces)
RX_TX
RX_ONLY LITERAL1 Serial_begin
TX_ONLY LITERAL1 Serial_begin
To use the new changes, it's a simple addition to Serial.begin().
Example:
// Functions the same as it always has
// The receive pin (RXDn) and transmit pin (TXDn) are both controlled by HardwareSerial
Serial.begin(115200);
// Functions the same as above
Serial.begin(115200, RX_TX);
// Only the receive pin (RXDn) is controlled by HardwareSerial
// The transmit pin (TXDn) is free to use as digital I/O
Serial.begin(115200, RX_ONLY);
// Only the transmit pin (TXDn) is controlled by HardwareSerial
// The receive pin (RXDn) is free to use as digital I/O
Serial.begin(115200, TX_ONLY);
I've tested this succesfully using IDE 1.0.1 and the Mega1280/2560 on Serial ports 1, 2 and 3. I think I need to connect two arduinos to check port 0. If someone could test this out and let me know, maybe we could get this in the next build.
Thanks for your time,
DigitalJohnson