Creating a SoftwareSerial object array

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Creating a SoftwareSerial object array

Postby roherman00 » Thu Apr 26, 2012 6:10 pm

Hello,

I have been searching the internet and asked on Robotshop forum but have not solved this issue yet. Is it possible to make an array of SoftwareSerial objects such as:
SoftwareSerial softSerial[2] = { SoftwareSerial(2, 3), SoftwareSerial(4, 5) }

It compiles and uploads fine but only the softSerial[1] returns or sends an serial data and softSerial[0] always returns 0. I'm confused because I saw this code:
LED led[5] = { LED(1), LED(2), ... LED(5) } and it works

For example led[4].on() will turn on an led and
softSerial[1].write("hello") will send hello but
softSerial[0].write("hello") sends nothing and returns a 0

I know both serial devices work because if I swap them in the array the other serial device now sends data, but it is always the first instantiation (i.e. softSerial[0]) that does not seem to exist.

Thank you to anyone who has an idea
roherman00
 
Posts: 11
Joined: Thu Apr 26, 2012 6:00 pm

Re: Creating a SoftwareSerial object array

Postby adafruit_support_bill » Fri Apr 27, 2012 5:29 am

SoftwareSerial can only receive on one port at a time. By default, the last port you initialize is enabled for receive. To receive on the other port you must explicitly enable it with the "listen()" function.
http://arduino.cc/en/Tutorial/TwoPortReceive
Not sure why it wouldn't send though...
User avatar
adafruit_support_bill
 
Posts: 16038
Joined: Sat Feb 07, 2009 9:11 am

Re: Creating a SoftwareSerial object array

Postby roherman00 » Fri Apr 27, 2012 7:02 am

Thank you very much. I'm glad that it is an easy solution. To be honest I'm sure it sent data but because it returned 0 on a receive I figured it wasn't doing either. Just a couple of questions. Yes or no answers are cool with me.

1) If I write for example softSerial[1].write(some number) and then the next line is softSerial[0].listen() would the next line be softSerial[0].write(some number)? If I use listen() does it automatically stop listening to the other?

2) Can you do softSerial[0].listen().write(some value)

3) I assume from what you wrote that if I'm only writing data either port can do that but once I want to listen I have to attach a listen() argument to the softSerial that I want?

4) Adding on to 3 it says arduino can do multiple softSerial ports so to listen to any of them I just swap listen() arguments?

Again thanks alot for the help.
roherman00
 
Posts: 11
Joined: Thu Apr 26, 2012 6:00 pm

Re: Creating a SoftwareSerial object array

Postby adafruit_support_bill » Fri Apr 27, 2012 7:40 am

1. not sure I understand the whole question, but to the last part: yes. Calling listen() on one port causes it to stop listening on any other SoftwareSerial port. (Regular hardware serial ports are not affected)

2. no - it is two separate statements.

3. yes. You can write to any port at any time. But you need to explicitly listen on a port before it can receive data.

4. yes. The trick is knowing when to listen to which port. Since there is no buffering on the non-listening ports, any data received on a port while not listening will be lost.
User avatar
adafruit_support_bill
 
Posts: 16038
Joined: Sat Feb 07, 2009 9:11 am

Re: Creating a SoftwareSerial object array

Postby roherman00 » Fri Apr 27, 2012 7:49 am

1) I was just giving some poor example of code
softSerial[1].write("hello") //the last instantiation of softSerial made
softSerial[0].listen(); //listen to the first instantiation now
softSerial[0].write("hello");
softSerial[0].read();
Oh now I see why my question was confusing because I just remembered you mentioning that you do not need to listen to a port to be able to write.
4) Thanks for mentioning that I'll keep that in mind. If I did not know that I would probably have another major issue in a day or two.

I can't wait to try it out now. Thank you for the help.
roherman00
 
Posts: 11
Joined: Thu Apr 26, 2012 6:00 pm


Return to Arduino

Who is online

Users browsing this forum: TripleT and 10 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]