NewSoftSerial

A New Software Serial Library for Arduino

News: NewSoftSerial is in the core!  Starting with Arduino 1.0 (December, 2011), NewSoftSerial has replaced the old SoftwareSerial library as the officially supported software serial library.  This means that if you have 1.0 or later, you should not download this library.  To port your code to 1.0, simply change all NewSoftSerial references to SoftwareSerial.

NewSoftSerial is the latest of three Arduino libraries providing “soft” serial port support. It’s the direct descendant of ladyada’s AFSoftSerial, which introduced interrupt-driven receives — a dramatic improvement over the polling required by the native SoftwareSerial.

Without interrupts, your program’s design is considerably restricted, as it must continually poll the serial port at very short, regular intervals. This makes it nearly impossible, for example, to use SoftwareSerial to receive GPS data and parse it into a usable form. Your program is too busy trying to keep up with NMEA characters as they arrive to actually spend time assembling them into something meaningful. This is where AFSoftSerial’s (and NewSoftSerial‘s) interrupt architecture is a godsend. Using interrupt-driven RX, your program fills its buffer behind the scenes while processing previously received data.

Improvements

NewSoftSerial offers a number of improvements over SoftwareSerial:

  1. It inherits from built-in class Print, eliminating some 4-600 bytes of duplicate code
  2. It implements circular buffering scheme to make RX processing more efficient
  3. It extends support to all Arduino pins 0-19 (0-21 on Arduino Mini), not just 0-13
  4. It supports multiple simultaneous soft serial devices.*
  5. It supports a much wider range of baud rates.**
  6. It provides a boolean overflow() method to detect buffer overflow.
  7. Higher baud rates have been tuned for better accuracy.
  8. It supports the ATMega328 and 168.
  9. It supports 8MHz processors.
  10. It uses direct port I/O for faster and more precise operation.
  11. (New with version 10).  It supports software signal inversion.
  12. (New) It supports 20MHz processors.
  13. (New) It runs on the Teensy and Teensy++.
  14. (New) It supports an end() method as a complement to begin().

*But see below for an important caveat on multiple instances.
**Be circumspect about using 300 and 1200 baud though. The interrupt handler at these rate becomes so lengthy that timer tick interrupts can be starved, causing millis() to stop working during receives.

Using Multiple Instances

There has been considerable support for an library that would allow multiple soft serial devices. However, handling asynchronously received data from two, three, or four or more serial devices turns out to be an extremely difficult, if not intractable problem. Imagine four serial devices connected to an Arduino, each transmitting at 38,400 baud. As bits arrive, Arduino’s poor little processor must sample and process each of 4 incoming bits within 26 microseconds or else lose them forever. Yikes!

It occurred to me, though, that multiple instances could still be possible if the library user were willing to make a small concession. NewSoftSerial is written on the principle that you can have as many devices connected as resource constraints allow, as long as you only use one of them at a time. If you can organize your program code around this constraint, then NewSoftSerial may work for you.

What does this mean, exactly? Well, you have to use your serial devices serially, like this:

#include <NewSoftSerial.h>

// Here's a GPS device connect to pins 3 and 4
NewSoftSerial gps(4,3);

// A serial thermometer connected to 5 and 6
NewSoftSerial therm(6,5);

// An LCD connected to 7 and 8
NewSoftSerial LCD(8,7); // serial LCD

void loop()
{
  ...
  // collect data from the GPS unit for a few seconds
  gps.listen();
  read_gps_data();  // use gps as active device
  // collect temperature data from thermometer
  therm.listen();
  read_thermometer_data(); // now use therm
  // LCD becomes the active device here
  LCD.listen();
  LCD.print("Data gathered...");
  ...
}

In this example, we assume that read_gps_data() uses the gps object and read_thermometer_data() uses the therm object. Any time you call the listen() method, it becomes the “active” object, and the previously active object is deactivated and its RX buffer discarded. An important point here is that object.available() always returns 0 unless object is already active. This means that you can’t write code like this:

void loop()
{
  device1.listen();
  if (device1.available() > 0)
  {
    int c = device1.read();
    ...
  }
  device2.listen();
  if (device2.available() > 0)
  {
    int c = device2.read();
    ...
  }
}

This code will never do anything but activate one device after the other.

Signal Inversion

“Normal” TTL serial signaling defines a start bit as a transition from “high” to “low” logic.  Logical 1 is “high”, 0 is “low”.  But some serial devices turn this logic upside down, using what we call “inverted signaling”.  As of version 10, NewSoftSerial supports these devices natively with a third parameter in the constructor.

NewSoftSerial myInvertedConn(7, 5, true); // this device uses inverted signaling
NewSoftSerial myGPS(3, 2); // this one doesn't

Library Version

You can retrieve the version of the NewSoftSerial library by calling the static member library_version().

int ver = NewSoftSerial::library_version();

Resource Consumption

Linking the NewSoftSerial library to your application adds approximately 2000 bytes to its size.

Download

The latest version of NewSoftSerial is available here: NewSoftSerial12.zip.  Note: don’t download this if you have Arduino 1.0 or later.  As of 1.0, NewSoftSerial is included in the Arduino core (named SoftwareSerial).

Change Log

  1. initial version
  2. ported to Arduino 0013, included example sketch in package
  3. several important improvements: (a) support for 300, 1200, 14400, and 28800 baud (see caveats), (b) added bool overflow() method to test whether an RX buffer overflow has occurred, and (c) tuned RX and TX for greater accuracy at high baud rates 38.4K, 57.6K, and 115.2K.
  4. minor bug fixes — add .o file and objdump.txt to zip file for diagnostics.
  5. etracer’s inline assembler fix to OSX avr-gcc 4.3.0 interrupt handler bug added.
  6. ladyada’s new example sketch, fix to interrupt name, support for 328p.
  7. etracer’s workaround is now conditionally compiled only when avr-gcc’s version is less than 4.3.2.
  8. 8 MHz support and flush() and enable_timer0()  methods added
  9. digitalread/write scrapped in favor of direct port I/O.  Revised routines now get perfect RX up to 57.6K on 16MHz processors and 31.25K on 8MHz processors.
  10. inverted TTL signalling supported.  20MHz processors supported.  Teensy and Teensy++ supported.  New end() method and destructor added to clean up.
  11. added listen() method to explicitly activate ports.
  12. warn users about 1.0 conflict

Acknowledgements

Many thanks to David Mellis, who wrote the original SoftwareSerial, and to the multi-talented ladyada, whose work with AFSoftSerial is seminal.  Ladyada also provided the “Goodnight, moon” example sketch, fixed a problem with the interrupt naming (see v6) and tested NSS with the 328p.

Thanks also to rogermm and several other forum users who have tested NewSoftSerial and given useful feedback.

The diligent analysis of forum user etracer yielded the root cause of a tricky problem with NSS on OSX.  A bug in avr-gcc 4.3.0 causes the compiler to fail to generate the proper entry and exit sequences for certain interrupt handlers.  etracer identified the problem and provided an inline workaround.  etracer’s fix is in NSS 5.

User jin contributed a large body of work based on NSS and identified a potential problem that could result in data loss (fixed in NSS 5).  jin also made a variant of NSS that supports 4-pin serial, with the additional pins providing a very nice RTS/CTS flow control.  We may see this in NSS in the near future.

Thanks to Garret Mace, who contributed the delay tables for 20MHz processors and claims that he can send and receive at 115K baud.  Cool!

Thanks to Paul Stoffregen, both for his fine work with Teensy and Teensy++, and for contributing some useful suggestions that help NewSoftSerial run on them without modification.

I appreciate any and all input.

Mikal Hart

Page last updated on July 3, 2013 at 7:37 pm
647 Responses → “NewSoftSerial”

  1. Mikal

    14 years ago

    Quite right. The new (>= 0016) Servo library doesn’t coexist well with software serial. The former requires interrupts and the latter requires interrupts to be disabled. Whenever I need to drive servos with NewSoftSerial, that is, all the time, I use the old Servo library, which I’ve reposted on this site as PWMServo.

    Mikal


  2. Doug

    14 years ago

    I am only using RX to receive external data with no need for Ack and want to use just a single pin. Is there an option for not assigning a TX pin?

    Example:
    NewSoftSerial mySerial(softRx, NULL);


  3. Mikal

    14 years ago

    Doug, you almost have it right. Do this instead:

    NewSoftSerial mySerial(softRX, -1);

    Mikal


  4. Wyatt

    14 years ago

    I have a question that might seem rather easy but I am integrating 5 devices. A BMA 180 triple-axis accelerometer, ITG 3200 triple-axis gyroscope, HMC5843 triple axis magnometer, BMP085 barometric pressure sensor, and the Venus 09133 gps unit. The problem I am having is I can get the data from all the devices except the gps. I am only use the Analog pins 4 and 5 to talk to these devices. It runs smoothly. Now I tested the gps and I can get this to run perfectly by itself from the digital pin 0 and 1 by using your library. Now my question is when I connect the gps to the Arduino I can’t get any data from the devices. Do I have to use seperate pins for each device and call them in whatever order I think the most important? Is there anyway to just use pins 4 and 5 for the first four devices and use 0 and 1 for the gps at the same time?


  5. Wyatt

    14 years ago

    Well after sometime going over the library again I realized the problem and it was a very simple fix. I am able to grab all the data from those five devices and route them to the processing to get a 3D simulation of the whole perf board that I created. It was a great feeling. I want to thank you for having such good libraries. Now all I have to do is fix my EKF to get a better simulation of the device!


  6. Mik

    14 years ago

    Hiya,
    Can you tell us where the documentation for this is? Is there a javadocs api? All I could find was a download for a work of documentation in progress.
    Thanks, and great work on the newsoftserial library!


  7. Mikal

    14 years ago

    This is it, Mik. :)


  8. Stephen G.

    14 years ago

    Here’s a interesting twist.. the same digital pin for serial in & out, but with a need to send a solid 300ms sync pulse 1st, and a 2nd digital pin to act as a PTT switch. I’m looking at the Parallax 433Mhz RF transceivers, and these operate like walkie-talkies when used. they use the same pin for serial in & out, a RX/TX pin to handle which mode, and a signal detected pin to tell if a carrier is detected. I’m trying to make a rudimentary link between an arduino connected to the computer as a host transceiver, and the arduino on a mobile robot as it’s own transceiver. protocol to talk between the two, would be similar to the old (showing my age) X-Modem protocol, but with a variable length packet up to 256 bytes at a time., with a attention byte, length byte, the string of data, and a 16-bit CRC. to which the receiver would acknowledge if the data CRC matches, to either Okay, or corrupt.(and resend request.) Both would talk back & forth in this method. Could the NewSoftSerial library be adapted to handle this?


  9. Aof

    14 years ago

    Hello, Mikal.

    From your example,

    #include

    // Here’s a GPS device connect to pins 3 and 4
    NewSoftSerial gps(4,3);

    // A serial thermometer connected to 5 and 6
    NewSoftSerial therm(6,5);

    // An LCD connected to 7 and 8
    NewSoftSerial LCD(8,7); // serial LCD

    If I typed gps.println(“Hello World”), I would like to where would the output show out?? If it possible to make it print on the serial monitor or HyperTerminal.

    Thx for your answer.


  10. Mikal

    14 years ago

    @Stephen G,

    Interesting. I think you could use NewSoftSerial in this case like this. (I haven’t tried it but it ought to work.)
    if (need_to_transmit)
    {
    NewSoftSerial nss(-1, 6); // transmit on pin 6
    nss.write(...);
    nss.write(...);
    nss.flush();
    }

    else // receive mode
    {
    NewSoftSerial nss(6, -1); rx only
    // read here
    }


  11. Mikal

    14 years ago

    @Aof,

    If you do gps.println(“Hello, world”) the data is sent to the GPS device which is probably not what you want. If you want to send data to the serial monitor, use

    Serial.println(“Hello, world”);


  12. Aof

    14 years ago

    But if we use some libraries which cannot use with “Serial”, how can I receive the data from serial monitor or hyperterminal??


  13. Jordan

    14 years ago

    Very specifically, where is the documentation for NewSoftSerial??


  14. Mikal

    14 years ago

    Jordan, I need to write some formal documentation, but I haven’t yet. This is it.

    Mikal


  15. Roger

    14 years ago

    So I am new to this. Does this NewSoftSerial download work with: http://www.sparkfun.com/products/9395

    Serial Enabled 16×2 LCD – White on Black 5V.

    Thanks


  16. Rick

    14 years ago

    I’d like to use this library to communicate to a device with a single wire.

    Currently, I’m using the library like this:
    NewSoftSerial mySerial(2, 2); (same pin for rx and tx)

    This works (with version 9) if I use:
    mySerial.setTX(2); and mySerial.setRX(2) before printing and reading

    However, when I remove the setTx/setRx functions it no longer works.

    If the Rx and Tx pins are the same, why would I need to set them with the setTX/setRX function?

    I’d like to use version 10 – but can’t figure out how to support a 1 wire communication with it yet.


  17. Mikal

    14 years ago

    @Roger,

    Yep, it should!


  18. Mikal

    14 years ago

    @Rick.

    Hmm… interesting question.

    I think I would create temporary NewSoftSerial objects, something like this:

    if (time_to_print)
    {
    NewSoftSerial mySerial(-1, 2);
    mySerial.begin(4800);
    mySerial.print("Hello, world");
    }
    else
    {
    NewSoftSerial mySerial(2, -1);
    mySerial.begin(4800);
    // do some RX here
    }

    Mikal


  19. Douglas

    14 years ago

    OK, I think I need help with this. Making a box, but I’m having very slow GPS pickup (or none at all) and I’m trying to debug. So I figured I’d look at the GPS lines using:

    in setup( )
    /* establish a debug session with a host computer */
    Serial.begin(115200);

    // Establish communication with the GPS
    nss.begin(4800);

    in loop( )

    Serial.print(nss.read(), BYTE);

    hoping to get what I saw on this YouTube video (http://youtu.be/AO_Dv6kqn5k) – that is, a bunch of lines with interpretable $GP… type statements on the serial monitor.

    but what I see is this:

    $GPRMC,022438.471,V,,,,,,,080611,,*2C
    ÿÿ—479,,,,,0,00,,,M,0.0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

    [MANY LINES OF THIS]

    ÿÿ,ÿÿÿÿMÿÿÿÿ,ÿÿÿÿ0ÿÿÿÿ.ÿÿÿÿ0ÿÿÿÿ,ÿÿÿÿMÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ0ÿÿÿÿ0ÿÿÿÿ0ÿÿÿÿ0ÿÿÿÿ*ÿÿÿÿ5ÿÿÿÿ5ÿÿÿÿ
    ÿÿÿÿ
    ÿÿÿÿ$ÿÿÿÿGÿÿÿÿPÿÿÿÿGÿÿÿÿSÿÿÿÿAÿÿÿÿ,ÿÿÿÿAÿÿÿÿ,ÿÿÿÿ1ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ*ÿÿÿÿ1ÿÿÿÿEÿÿÿÿ
    ÿÿÿÿ
    ÿÿÿÿ$ÿÿÿÿGÿÿÿÿPÿÿÿÿRÿÿÿÿMÿÿÿÿCÿÿÿÿ,ÿÿÿÿ0ÿÿÿÿ2ÿÿÿÿ2ÿÿÿÿ4ÿÿÿÿ4ÿÿÿÿ8ÿÿÿÿ.ÿÿÿÿ4ÿÿÿÿ7ÿÿÿÿ8ÿÿÿÿ,ÿÿÿÿVÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿ,ÿÿÿÿÿÿ
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ<ðÿÌ22451.477,,,,,0,00,,,M,0.0,M,,0000*52
    $GPGSA,A,1,,,,,,,,,,ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

    [AND SO ON]

    ??? I don't know what to make of it. Some of the $GP lines are there, but what else is going on???

    Any ideas?


  20. Mikal

    14 years ago

    @Douglas,

    Yep, I know exactly what’s going on. You are trying to read from the nss port before the data has arrived. (That umlauted y character is ASCII for 255. read() returns -1 (255) when no character is available.) You need to check nss.available() > 0 before calling nss.read().

    Mikal


  21. Douglas

    14 years ago

    @Mikal, awesome, thanks. Got it. Now I’ll try to figure out what’s going on…


  22. Alex Forencich

    14 years ago

    Is there any particular reason the write function is private? Some people need to work in binary mode from time to time…..


  23. Sebastian

    14 years ago

    Hi, how can i communicate two arduinos with this Library, i need to send a float.


  24. MAPGPS

    14 years ago

    How can I use NewSoftSerial to make half-duplex communication over one Pin?

    I tried:
    NewSoftSerial test(2,2);

    But it does not work.

    Or should I use:
    NewSoftSerial test(2,3);
    And combine Pin 2 and Pin 3?


  25. Mikal

    14 years ago

    @Alex, in the beta 11 version (elsewhere on this site) the write function is no longer private.


  26. Mikal

    14 years ago

    @MAPGPS,

    The best way to accomplish that is to create a new temporary NewSoftSerial object that communicates in one direction, then destroy it and create another one for the other direction. For example

    if (transmit)
    {
    NewSoftSerial(-1, 2);
    ... transmit on pin 2
    }
    else
    {
    NewSoftSerial(2, -1);
    ... receive on pin 2
    }


  27. Stephen

    14 years ago

    How would I go about using this library at 8600 baud? It works great at 9600 however the piece of equipment I’m using is fixed at this odd baud rate.

    I assume that it would be as simple as inserting another row into the look up table. However I don’t know how to calculate the values. I’m using the Uno. Thanks!

    { 9600, 114, 236, 236, 233, },
    { 8600, ? , ? , ? , ? , },
    { 4800, 233, 474, 474, 471, },


  28. Mikal

    14 years ago

    Stephen,
    8600? Weird. You are correct that inserting a new row in the table should be all you need to support the new baud rate.

    As a very rough guess at the delay numbers for a new rate, you can do an arithmetic interpolation to get 9600*114=1094400 and 1094400/8600=127. Similarly, 9600*236=2265600 and 2265600/8600=263. Using this technique you get

    { 9600, 114, 236, 236, 233, },
    { 8600, 127, 263, 263, 260 },
    { 4800, 233, 474, 474, 471, },

    These aren’t exactly right, but at these low baud rates I wouldn’t expect it to matter.

    Mikal


  29. Stephen

    14 years ago

    Thanks! Those values work perfectly.


  30. Sigurd

    14 years ago

    Hi Mikal,

    Great Library, but I have a problem.

    I have a gps and cell shield connected to the arduino. When I try to communicate with one of them – it works fine, but when I try to communicate with both of them – I dont get anything back.

    Do you see what the problem could be?

    Kind Regards
    Sigurd

    /////////////////////////THE CODE/////////////////////////////////
    #include
    #include

    TinyGPS gps;
    NewSoftSerial nss(4, 5), cell(2, 3);

    bool feedgps();
    void gpsdump(TinyGPS &gps);

    void setup(){
    Serial.begin(9600);
    Serial.println(“Initialize…”);
    nss.begin(4800);
    cell.begin(9600);
    delay(5000);
    cell.println(“AT+CMGF=1”);
    delay(200);
    cell.println(“AT+CNMI=3,3,0,0”);
    delay(200);
    Serial.println(“Ready to receive!”);
    }

    void loop(){
    if(feedgps()){
    gpsdump(gps);
    }

    if(cell.available()){
    //Do something.
    //If I remove this if statement – I can read the GPS data, but when I include this if statement – I get nothing.
    }
    }

    bool feedgps(){
    while (nss.available()){
    if (gps.encode(nss.read()))
    return true;
    }
    return false;
    }

    void gpsdump(TinyGPS &gps){
    float latitude, longitude;
    gps.f_get_position(&latitude, &longitude);
    Serial.print(“Latitude: “); Serial.println(latitude, 5);
    Serial.print(“longitude: “); Serial.println(longitude, 5);
    delay(500);
    }
    ///////////////////////////////////////////////////////////////////////


  31. JeffOB

    14 years ago

    Mikal,
    I needed a way to time out if no gps was available.
    do you see any problem with using newsoftserial as i do in the code below.
    specifically i’m curious what state the pins are left in after i call nss.end()
    I’m trying to track down an electrical noise issue that i see randomly after a gps fix. wondering if the pins are being left in a state that could contribute to a noise problem.

    Thanks Jeff

    code below

    //*****************************************
    // FeedGPS()
    //*****************************************
    boolean FeedGPS()
    {
    while (nss.available())
    {
    if (gps.encode(nss.read())) return true;
    }
    return false;
    }

    //*****************************************
    // GPS_Available(unsigned long ulTimeOutMS)
    //*****************************************
    int GPS_Available(unsigned long ulTimeOutMS) {
    bool newdata = false;
    unsigned long feedstart = 0;
    unsigned long theTime = 0;

    Serial.print(“Begin GPS_Available…\r”);

    nss.begin(4800);
    msCounter = 0;
    while (msCounter < ulTimeOutMS) {
    feedstart = msCounter;
    while (msCounter – feedstart < 2000)
    {
    if (FeedGPS()) newdata = true;
    }

    if (newdata)
    {
    theTime = msCounter;
    Serial.print("GPS_Avail. took ");
    Serial.print(theTime);
    Serial.print(" ms.\r");
    nss.end();
    gps.get_position(&lat, &lon, &age);
    gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
    return SUCCESS;
    }

    }

    nss.end();
    Serial.println("Error: GPS Timed Out\r");
    return FAILURE;
    }


  32. Dave

    13 years ago

    Greetings Mikal,
    Thank you for your hard work on NewSoftSerial. It has been invaluable for me to get debugging output while working with XBee modules and other testing.

    I need to add more serial devices to a Arduino Uno and I want to determine if I understand what you wrote in “Using Multiple Instances”.
    I will be using a GPS, XBee, RFID reader, and a debugging output.

    Can I do the following so that I do not lose any serial input to Arduino from any device?
    NewSoftSerial debug(-1, 6); // transmit debug messages on pin 6
    NewSoftSerial gps(3, 4);
    // XBee connected to Arduino UART pins 1 & 2
    NewSoftSerial rfid(5, -1); // receive only

    void loop()
    {

    gps.begin(38400); // select GPS module for use with NSS
    if (gps.available())
    {
    // process gps.read();
    }
    // use gps.end(); ???
    rfid.begin(9600); // select RFID reader for use with NSS
    if (rfid.available())
    {
    // process rfid.read();
    }
    // Check if XBee has valid data

    }

    Is this the correct way to switch between serial inputs?
    I’m assuming that I’m at risk of loosing GPS serial data that is received at the same time as the RFID reader is selected and is in the process of outputting data, correct?
    Or am I going about this the wrong way?

    Thank you for any assistance you can offer!


  33. Mikal

    13 years ago

    @Sigurd,

    You can’t listen to two devices at the same time over soft serial. Read elsewhere in this blog for details, but what you need to do is work with one and then the other serially.

    Mikal


  34. Mikal

    13 years ago

    @Jeff,

    The timeout code seems relatively good to me, although take note that encode() returns true when it has parsed any legal NMEA sentence, NOT just when it successfully gotten a GPS fix. On my puzzle boxes this routine would never reach the “if (newdata)” line, even inside the basement of a tall building, because my GPS is always generating legal NMEA at least once per second.

    If that’s not what you want, you might try looping on age == GPS_INVALID_AGE and/or newdata as well.

    Mikal


  35. Mikal

    13 years ago

    @Dave,

    I’m sorry to say that there is no way to “not lose any serial input to Arduino [Uno] from any device”. This example won’t work because while the “gps” port is active, the “rfid” port will be missing incoming data and vice-versa. The only hope really is to make, say, the RFID reader the active listening device, and then only switch to the GPS device once you’ve completely processed an RFID “transaction”–whatever that means. Something like

    void loop()
    {
      bool finished_with_RFID = false;
      bool finished_with_GPD = false;
      rfid.begin(9600);
      while (!finished_with_RFID)
      {
        // process XBee data from Serial port
        // process RFID data from RFID port
        // if RFID complete, set finished_with_RFID=true
      }
    
      rfid.end();
      gps.begin(38400);
    
      while (!finished_with_GPS)
      {
        // process XBee data from Serial port
        // process GPS data from GPS port
        // if GPS complete, set finished_with_GPS=true
      }
      gps.end();
    }
    

    Mikal


  36. JeffOB

    13 years ago

    Hi Mikal,
    Thanks for your response. can you comment on my repeated use of nss.begin(4800) and nss.end()
    the gps is constantly sending data but i am not listening until i call nss.begin() and once i’m done i stop listening with nss.end() eventually the board powers off the gps and goes to sleep for 10 minutes before waking up to get another fix.

    are there any issues with this strategy? i didn’t want my main code being bogged down with the interrupts from the gps strings when i wasn’t needing gps. but i didn’t want to power off the gps either to avoid the longer waits of a cold start. so i used nss.begin() and nss.end() to sort of listen only when i needed to. does this make sense?

    thanks again

    Jeff


  37. Mikal

    13 years ago

    @Jeff, I implemented nss.end() for exactly the scenario you describe–to turn off interrupts during the time where the input device is not interesting.

    Your project seems interesting. Can you share a bit about it?

    Mikal


  38. Henry

    13 years ago

    Hi

    I’m trying to use TinyGPS, newsofserial and to send the data via I2C, however, when it read the GPS and trting to send data via I2C, the arduino stops working few seconds.

    I don’t know how to solve this, could you please advice

    Many Thanks


  39. JeffOB

    13 years ago

    Hi Mikal,
    Thanks again for your response. Could you also comment on what state the rx and tx pins are left in after calling nss.end(). Input/Output? Pulled up or floating? etc.

    also I was under the impression that gps.encode() would return when it got a legal string that had and “a” for active. what causes fix age to change from invalid to some amount of milliseconds.

    The system uses gps to make decisions on when to sample some environmental data.

    Thanks.
    Jeff


  40. Mike

    13 years ago

    Help, Iam using newSoftSerial to drive a GSM modem and everything is fine until I call “an0 = analogRead(A0);”.

    After that, I get no received characters at all.

    With the serial port hooked upto gtkterm, I can see output i.e. (the Arduino issuing AT+CSQ in this test case).

    If I comment out the analogRead call, it works fine.

    I’ve tried re-initialising the newSoftSerial in the loop(), but to no avail. Any ideas?

    Mike


  41. Mike

    13 years ago

    Hi

    Has anyone experience problems with the newSoftSerial library ceasing to read serial data after a call to analogRead() has been made?

    If I comment out, the analog call everything works as it should do.

    Regards

    Mike


  42. Paddy

    13 years ago

    Hi

    I’m having difficulties reading data using newsoftserial. Board is a mega, pin 42 conected to 16, 43 to 17.
    Output is:
    10
    My_nss.read=-1
    Serial2.read=65
    repeating


  43. Mikal

    13 years ago

    Paddy, you can only use Mega pins that support pin change interrupts, and none of the four your mention do. Get the new beta version of NSS 11 and change your wiring to use one of the supported pins.

7 Trackbacks For This Post
  1. Blog What I Made » YAHMS: Base Station

    […] of just the standard Serial interface, see the links below for that too. You’ll also need NewSoftSerial of course and the Flash library which I’ve used to decrease memory usage. Follow the […]

  2. Infovore » Nikon D-Series Intervalometer

    […] a single wire, which again, keeps the number of wires from the Arduino down. I’m using the NewSoftSerial library to talk to it, which makes life […]

  3. Arduino Experiments

    […] you can use multiple serial “ports”, that are actually digital I/O lines, by using the NewSoftSerial library. This works exactly like the Serial library, but you can read from multiple pins, as long as you […]

  4. EasyTransfer Arduino Library « The Mind of Bill Porter

    […] it’s easier to pick which Serial port to use; Serial, Serial1, etc. AND support for the NewSoftSerial library for creating software serial ports on any pin. Inside the download zip file are two versions of the […]

  5. Research: RFID, XBee and Arduino « Beyond the keyboard

    […] neat thing is the NewSoftSerial library for Adruino, allowing you to turn any set of pins into additional RX/TX pins with free to set baud […]

  6. jomuoru weblog » Blog Archive » Esto es Camus Party

    […] de instalar la librería NewSoftSerial pude compilar e instalar el Arduino Firmware en mi placa. A continuación necesitaba descargarme […]

  7. Update: Design review « Appiphania

    […] a bit of this code at the end of this journal entry. The “NewSoftSerial” library http://arduiniana.org/libraries/newsoftserial/ was extremely easy to get working (code example […]

Leave a Reply