Go Down

Topic: Using only TX of SoftwareSerial, leaving RX pin free (Read 34563 times) previous topic - next topic

tobek

Hi everyone.

I'm using SparkFun's OpenLog, a microSD card data-logger to which you communicate through serial. The only connection needed is Arduino TX to OpenLog RX, because the OpenLog handles the rest.

I have run out of pins in the project I'm using, and have exactly one digital I/O pin left to use for the OpenLog. I'm using SoftwareSerial. However, when I declare an instance of SoftwareSerial, I need to give it 2 pins: RX and TX. I only need TX, and every other pin on my Arduino (Nano) is currently being used for output.

Will something go wrong if I declare SoftwareSerial with an RX pin that's actually being used elsewhere for output? Can I, instead, declare SoftwareSerial with a pin that doesn't exist - say, 100? Any other options?

Thanks for any advice!
Ideally, if anything were any good, it would be indescribable.
- Edward Gorey

Nick Gammon

I wouldn't use pin 100 or anything like that, because it tries to use that pin, eg. here:

Code: [Select]
void SoftwareSerial::setRX(uint8_t rx)
{
  pinMode(rx, INPUT);
  if (!_inverse_logic)
    digitalWrite(rx, HIGH);  // pullup for normal logic!
  _receivePin = rx;
  _receiveBitMask = digitalPinToBitMask(rx);
  uint8_t port = digitalPinToPort(rx);
  _receivePortRegister = portInputRegister(port);
}


They don't seem to have allowed for no Rx at all, so you might be better off taking the class, and making a "send-only" version by removing the references to the reading part. I'll take a stab at it, shouldn't take long.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

I adapted SoftwareSerial to only have a send function. Download library here:

http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip

Example of use:

Code: [Select]
#include <SendOnlySoftwareSerial.h>

SendOnlySoftwareSerial mySerial (3);  // Tx pin

void setup ()
  {
  mySerial.begin(115200);
  }

int i;

void loop ()
{
  mySerial.print ("test: ");
  mySerial.println (i++);
  delay (100);
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

tobek

Thanks so much, that's brilliant, I appreciate it!
Ideally, if anything were any good, it would be indescribable.
- Edward Gorey

glenndrives

Fantastic! Thank you very much.

Glenndrives

MushfiqSarker

You can set RX to 0.

Code: [Select]
SoftwareSerial Serial2 = SoftwareSerial(0, 4);    //!< RX is 0. Pin 4 is TX

This works because there is no pin 0.

Mushfiq
Check out my site for DIY information: http://inventige.com

Nick Gammon

Yes there is a pin 0. Take a look at your board. It's labelled Rx (and 0).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

maeterlinck


meman


I adapted SoftwareSerial to only have a send function. Download library here:

http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip

Example of use:

Code: [Select]
#include <SendOnlySoftwareSerial.h>

SendOnlySoftwareSerial mySerial (3);  // Tx pin

void setup ()
  {
  mySerial.begin(115200);
  }

int i;

void loop ()
{
  mySerial.print ("test: ");
  mySerial.println (i++);
  delay (100);
}



in my case I want to release TX pin
how I can change the library in order to implement this method?
more info:
http://forum.arduino.cc/index.php?topic=227760.0

Nick Gammon

I adapted SoftwareSerial to only have a receive function. Download library here:

http://gammon.com.au/Arduino/ReceiveOnlySoftwareSerial.zip

Example of use:

Code: [Select]

#include <ReceiveOnlySoftwareSerial.h>

ReceiveOnlySoftwareSerial mySerial(10); // RX

void setup() 
{
  Serial.begin(115200);
  while (!Serial) { }  // wait for Serial to become available

  // set the data rate for the ReceiveOnlySoftwareSerial port
  mySerial.begin(9600);
}

void loop() // run over and over
{
  // echo from software serial to hardware serial
  if (mySerial.available())
    Serial.write(mySerial.read());
}


This could be handy for situations (like reading from a GPS) where you don't need to write, and thus you can save a few hundred bytes of program code.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

yair


had to add a line in your send only cpp file to make it work with micronuclius/digispark cores running at 16.5MHz.
see http://digistump.com/board/index.php/topic,212.msg1214/topicseen.html#msg1214

thank you nick, again

orthho

I adapted SoftwareSerial to only have a receive function. Download library here:

http://gammon.com.au/Arduino/ReceiveOnlySoftwareSerial.zip

Example of use:

Code: [Select]

#include <ReceiveOnlySoftwareSerial.h>

ReceiveOnlySoftwareSerial mySerial(10); // RX

void setup() 
{
  Serial.begin(115200);
  while (!Serial) { }  // wait for Serial to become available

  // set the data rate for the ReceiveOnlySoftwareSerial port
  mySerial.begin(9600);
}

void loop() // run over and over
{
  // echo from software serial to hardware serial
  if (mySerial.available())
    Serial.write(mySerial.read());
}


This could be handy for situations (like reading from a GPS) where you don't need to write, and thus you can save a few hundred bytes of program code.
Hi Nick !

I want to ask question,
I'm using Arduino UNO, and i want to reduce I/O pin..
Is it possible to combine your library and SoftwareSerial library?
because when i tried your library alone thats perfect !

right now i try to combine your library to SoftwareSerial in order to retrieve data serial from my ID-12 (RFID) and send it to other device using Serial too..



this is my code
Code: [Select]
#include <SoftwareSerial.h>
#include <ReceiveOnlySoftwareSerial.h>

// DEFINE
// ======== SERIAL =============
// == RFID
#define RXID  8
ReceiveOnlySoftwareSerial RFID (RXID);

// == RS 485
#define RX45           2
#define TX45           3
#define SerialControl  7
#define Transmit       HIGH
#define Receive        LOW
SoftwareSerial toSlave (RX45, TX45);

void setup(){
}

void loop(){
}


and these the error message
Code: [Select]
This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
ReceiveOnlySoftwareSerial\ReceiveOnlySoftwareSerial.cpp.o: In function `__vector_3':
--------------\Arduino\libraries\ReceiveOnlySoftwareSerial/ReceiveOnlySoftwareSerial.cpp:300: multiple definition of `__vector_3'
SoftwareSerial\SoftwareSerial.cpp.o:--------.cpp:305: first defined here
ReceiveOnlySoftwareSerial\ReceiveOnlySoftwareSerial.cpp.o: In function `__vector_4':
---------------\Arduino\libraries\ReceiveOnlySoftwareSerial/ReceiveOnlySoftwareSerial.cpp:307: multiple definition of `__vector_4'
SoftwareSerial\SoftwareSerial.cpp.o:-------\Arduino\libraries\SoftwareSerial/SoftwareSerial.cpp:312: first defined here
ReceiveOnlySoftwareSerial\ReceiveOnlySoftwareSerial.cpp.o: In function `__vector_5':
---------------\Arduino\libraries\ReceiveOnlySoftwareSerial/ReceiveOnlySoftwareSerial.cpp:314: multiple definition of `__vector_5'
SoftwareSerial\SoftwareSerial.cpp.o:-------------\Arduino\libraries\SoftwareSerial/SoftwareSerial.cpp:319: first defined here


Thanks ~ :)

Nick Gammon

Once you have SoftwareSerial you can have multiple instances of it (I think), so you don't need to combine the libraries.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

orthho

Hi Nick ! Thanks for your reply :)

Ya you were right, SoftwareSerial can allow us to have Serial Port programming more than 1..I've tried it with two Serial port. And it works.. But I haven't tried it for more than two yet.
Ah.. ok.. I understand.. I just tried to minimize my I/O pin so I try to combine it with your library.

Thanks Nick ~


Nick Gammon

Quite possibly the pin you chose. Please post your code. Also state which Arduino you have.

How to use this forum
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up