NewSoftSerial

A New Software Serial Library for Arduino

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
  read_gps_data();  // use gps as active device
  // collect temperature data from thermometer
  read_thermometer_data(); // now use therm
  // LCD becomes the active device here
  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 “use” an object by calling its begin(), available(), read(), or print[ln]() methods, 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()
{
  if (device1.available() > 0)
  {
    int c = device1.read();
    ...
  }
  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

Upcoming Improvements

  • RTS/CTS flow control
  • support for Arduino Mega

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: NewSoftSerial10c.zip

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.

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 January 18, 2010 at 12:26 pm
109 Responses → “NewSoftSerial”

  1. florent

    4 months ago

    Hie,

    I am sure that this is a really nice library but It doesn’t want to work with me.

    I use 2 arduino board : the first one is an arduino Mega and the second is a duemilanove.
    here is the program I uploaded in the first one :

    /////////////// first //////////
    void setup() {
    // initialize both serial ports:
    Serial.begin(9600);
    Serial1.begin(38400);
    Serial1.flush();
    Serial.flush();
    }

    void loop() {
    // read from port 1, send to port 0:
    if (Serial.available() > 0){
    int index=0;
    delay(100); // let the buffer fill up
    int numChar = Serial.available();
    char buffer[numChar-1];
    int numCharB = numChar;
    for (index=0;index 0){
    int index=0;
    delay(100); // let the buffer fill up
    int numChar = Serial1.available();
    char buffer[numChar-1];
    int numCharB = numChar;
    for (index=0;index<numChar;index++) {
    buffer[index] = Serial1.read();
    }
    Serial.print(”from1 \t\t”);
    Serial.println(buffer);
    }

    }
    /////////////// end first //////////

    and here is the program I ve upload in the second one :
    /////////////// second //////////
    #include

    NewSoftSerial mySerial(2, 3);

    void setup() {
    // initialize both serial ports:
    Serial.begin(9600);
    mySerial.begin(38400);
    mySerial.flush();
    Serial.flush();
    }

    void loop() {
    // read from port 1, send to port 0:
    if (Serial.available() > 0){
    int index=0;
    delay(100); // let the buffer fill up
    int numChar = Serial.available();
    char buffer[numChar-1];
    int numCharB = numChar;
    for (index=0;index 0){
    int index=0;
    delay(100); // let the buffer fill up
    int numChar = mySerial.available();
    char buffer[numChar-1];
    int numCharB = numChar;
    for (index=0;index<numChar;index++) {
    buffer[index] = mySerial.read();
    }
    mySerial.flush();
    Serial.print(”from1 \t”);
    Serial.print(numChar);
    Serial.print(”\t”);
    Serial.println(buffer);
    }

    }
    /////////////// end second //////////
    My device is well connected but in the second case, receive strange char

    Can u help me ??

    Thanks a lot !
    Florent

    PS : sorry for my bad english.


  2. Mikal

    4 months ago

    Florent–

    There are a couple of bugs in your code. For example, in two places you set

    int numChar = mySerial.available();
    char buffer[numChar-1];

    without checking whether numChar is greater than zero.

    The other thing is that if you are going to
    Serial.println(buffer);
    it is critical that you first terminate it with a 0. C strings must be terminated, otherwise, they are not, well, C strings.

    I bet you’ll see some improvement once you fix those.

    Mikal


  3. Florent

    4 months ago

    Mikal,

    … you’re right !
    In fact, It doesn’t work because I don’t master C programming.

    I didn’t find a bug in the NewSoftSerial library but you found a bug in my program

    All my apologizes!


  4. Maha

    4 months ago

    Great library! I’m really enjoying it so far. If i get a project into some form of complete, i’ll make sure you see your work driving it.


  5. Terry

    4 months ago

    I’m using NewSoftSerial to read NMEA from a GPS module. It works fine - until I introduce a delay(1000) statement elsewhere in my code - it seems that the delay somehow throws NewSoftSerial out, and when I read from the Serial device, lots of characters are missing - is there a way around this?


  6. Mikal

    4 months ago

    Terry,

    Let’s say you are connected to your GPS at 4800 baud. That means that every second, up to 4800 bits of data are arriving on your software serial port. Of these, about 80% are data bits. That’s about 4000 bits or 500 bytes. If you are delay()ing for 1 second, the first 64 of these 500 bytes enter NewSoftSerial’s data buffer, but the remainder are lost. You can’t use delay() for long periods and expect any serial communication, soft or otherwise, to work. If you really want to wait for 1 second, you’ve got to make sure you are handling the arriving characters during that time or face losing them. You can prove that you are losing characters by checking to see if nss.overflow() is true.

    I usually use a technique like this:

    unsigned long now = millis();
    while (1000 > millis() - now)
    {
      if (nss.available())
      {
        ... handle the character
      }
    }

    Mikal


  7. Terry

    4 months ago

    Hi Mikal - thanks for the quick reply!

    After some mucking around I realized pretty much what you explained :)

    Losing the chars wasn’t such a big deal - the hassle was that I was getting several NMEA sentences mashed together. I got around it by flush()’ing the NewSoftSerial after the delay() - it’s no hassle for me to wait half a second or so for a new NMEA sentence to arrive.

    That said, processing chars during my delay looks to be a more sensible option, I’ll write something up shortly!

    I had one other query - part of my project does RTTY (radio teletype) at 50 baud - I’m handling this with a few functions I’ve written to transmit the data - RTTY is just serial, so I could use a NewSoftSerial object to do the transmission, except that I’m transmitting at 50 baud.

    I found that when my GPS NewSoftSerial was ‘active’, the timing for my functions got thrown out (and hence my RTTY was not quite 50 baud). I figure this is due to NewSoftSerial processing incoming data from the GPS when triggered by an interrupt.

    I only intend to do one at a time (ie read from the GPS or transmit RTTY), at the moment I’m getting around the issue by creating a bogus (unused) NewSoftSerial, and setting this as the active device when I want to transmit via RTTY - but this is not the neatest solution.

    Is is possible to use NewSoftSerial at 50 baud? I realise it would likely mess up timing for other things when receiving chars, but the RTTY is for TX only, and I’m not overly fussed on other timing so long as my 50 baud is true. I’ve had a look at the delay tables in the source, but I’m not sure how the values are derived!

    Cheers,
    Terry.


  8. Mikal

    4 months ago

    @Terry–

    The delay constants in DELAY_TABLE are hand tuned with a logic analyzer from values I originally calculated. However, I think if you just take the values for 300 baud and multiply them by 6, these should be easily close enough. Just add a new line for 50.

    There’s one problem though. All the soft serial packages disable interrupt for the duration of the entire byte that’s being transmitted or received. At 50 baud that is a long time — ~200ms! Your millis() counter will definitely be way off if you use NewSoftSerial at 50 baud. If you don’t need millis, then don’t worry about it.

    Mikal


  9. Terry

    4 months ago

    Hi Mikal,
    Thanks so much for your help on this - I’ll have a go at NewSoftSerial @ 50 baud shortly and see how I get on - I’m not overly fussed about the millis() value being accurate at the moment, though if there were a way to ‘deactivate’ an active NewSoftSerial object, I would use my (simplistic) TX functions - I may end up needing millis() in future.

    Reading your previous posts, I can definitely put my hand up for wanting the ability to configure the data/parity/stop bits of the NewSoftSerial object. ;)

    Thanks again for your great work!
    Terry.


  10. normaldotcom

    4 months ago

    Any chance of support for atmega8? Due to the differently-named vector tables and interrupts, it won’t compile for the atmega8…


  11. Mikal

    4 months ago

    Hi normal–

    I don’t think the atmega8 supports the pin change interrupt, which NewSoftSerial relies on for data reception, so I don’t think it’s possible to get it to work on that chip.

    Mikal


  12. pseeling

    3 months ago

    Hi - first of all, BIG thanks for providing the community with this helpful library!
    ..and second - a little question ;-) - I am trying to run the library demo using a FunnelIO board (ATM168) at 9600 baud, but get the same weird character mappings that were described in a previous post. Since I only use chars, not strings, do you have any idea as of what might be my problem?
    (Yes, I am a newb)


  13. Paul Jacques

    3 months ago

    Hi,

    I have a Mega board and I need to use an XBee library I have that receives Xbee IOpackets, via “serialEvent()”. The XBee library uses the traditional serial library. I need to parse and send the analog data to an LCD. Can I still use NewSoftSerial library to setup a serial LCD to output on pin 3 and also use the XBee library to communicate with the Xbee via pins 0,1 ?

    Thank You,
    Paul Jacques


  14. Mikal

    3 months ago

    Paul, if you have a Mega, you have four serial ports, so you probably don’t need a software serial library to talk to your LCD. Use one of the other “hardware” ports. (NewSoftSerial is incompatible with Mega’s architecture anyway.)

    Mikal


  15. James

    3 months ago

    Mikal,

    Really nice work on this library.

    My project uses half-duplex communications. Therefore, I needed to have the transmit pin go inactive when not in use. This allows other nodes to communicate back on the same pin. I made a couple hacks to your code which seems to be working for me. However, I’m sure the multi-instance feature would get broken with this hack. Below is a summary of the changes I made that work for me.

    Just thought you might like to consider adding a half-duplex option to your official source in the future.

    Thanks again for the great library,
    Jim

    // End of NewSoftSerial::NewSoftSerial
    if ( transmitPin == receivePin ) {
    _transmitPin = transmitPin; // Update new private var.
    pinMode( _transmitPin, INPUT );
    _halfDuplex = true; // Update new private var.
    }
    else {
    _halfDuplex = false;
    }

    // Start of NewSoftSerial::write
    if ( _halfDuplex == true )
    pinMode( _transmitPin, OUTPUT );

    // End of NewSoftSerial::write
    if ( _halfDuplex == true )
    pinMode( _transmitPin, INPUT );


  16. SeeDoubleYou

    3 months ago

    Hey Mikal,

    For a project at my university I am using (or at least trying to) this amazing library. The thing is, I do need four serial connections. We are creating objects that can connect to each other. Each object has four nodes to which others can connect. Connection goes in two ways, so every object can send and receive from and to every direct ‘neighbor’. They have to send messages that represent their current state. The problem is to get this to work. I’ve read the ‘Using Multiple Instances’ part but can you possibly give me some more handles on how this would actually work. At this point my messages get messed up big time when using more than one serial. The receiving side gets a lot of ‘ÿ’ instead of the message I send.


  17. x893

    3 months ago

    Hi
    Great library. What about tuneDelay and r24-r30 error ? Are you plan change to delayMicroseconds ?


  18. Rich

    3 months ago

    Thanks for putting the time and effort into this library. I’m trying to use it with a uBlox GPS. No joy with the GPS, so I set up the “Goodnight Moon” test (great book for kids!) with the Arduino 017 IDE on the serial port and Hyperterminal on the mySerial end at 4800 baud 8n1. Board is Mini Pro 5v 16 MHz. While I’m getting characters across in both directions, the decoding appears to be off so they’re just jibberish. Any ideas what’s up here?
    Rich


  19. Mikal

    3 months ago

    How is the software serial port talking to Hyperterminal? There’s something missing here I think…?


  20. Rich

    3 months ago

    Hyperterminal is using COM3 with a USB-RS232 adapter, lines 2,3,5 (9-pin) are connected to pins 2,3 and GND on the MiniPro. Hardware and software flow control are disabled. I use this setup regularly to communicate with other serial devices. I know it’s operational. There is transmission in both directions. I can type a character into hyperterminal and a character appears at the serial monitor in the IDE. Likewise, I can type a character into the serial monitor and send it, and a character(s) will appear on the hyperterminal. Below is an embellished test app. It prints the word “Data:” and then the character sent, its ascii value in decimal and then in binary:
    #include
    NewSoftSerial mySerial(2, 3);
    void setup()
    {
    Serial.begin(9600);
    Serial.println(”Goodnight moon!”);
    mySerial.begin(4800);
    mySerial.println(”Hello, world?”);
    }
    void loop() // run over and over again
    {
    char c;
    if (mySerial.available()) {
    Serial.print(”Data: “);
    c = mySerial.read();
    Serial.print(c);
    Serial.print(” “);
    Serial.print(c, DEC);
    Serial.print(” “);
    Serial.println(c, BIN);
    }
    if (Serial.available()) {
    mySerial.print(”Data: “);
    c = Serial.read();
    mySerial.print(c);
    mySerial.print(” “);
    mySerial.print(c, DEC);
    mySerial.print(” “);
    mySerial.println(c, BIN);
    }
    }
    Here’s some output:
    HT = Hyperterminal connected to mySerial @ 4800 8n1, no hw ctrl, ASCI
    SM = Serial Monitor on serial port @ 9600.
    uc = unpritable character

    At Startup:
    HT output>
    «::
    :û‹!7åë«::
    :û‹!7åë
    SM output>
    Goodnight moon!

    NEXT Type “hello” on HT:
    SM output>
    Data: 9 1001
    Data: uc 0 0
    Data: M 77 1001101
    Data: uc 0 0
    Data: uc 18 10010
    Data: uc 0 0
    Data: uc 18 10010
    Data: uc 0 0
    Data: H 72 1001000
    Data: uc 0 0

    NEXT Type “hello” on SM:
    HT output>
    Wº=‹¿/¿¿¿:Ÿ—¿¿¿èŸŸŸŸåëw==‹¿5¿¿¿:Ÿ¿¿¿èŸŸŸåëw==‹¿’¿¿
    ¿:Ÿ¿¿¿èŸŸŸåëw==‹¿’¿¿¿:Ÿ¿¿¿èŸŸŸåëw==‹¿!¿¿¿:¿¿¿èŸåë

    As you can see, the ascii decode is correct in the HT->SM direction, just the wrong character, and there’s an extra line printed - why? In the SM->HT direction the characters are all unprintable. BTW, using Hypertermial with the GPS yields proper NMEA strings. Might this be a timing issue?
    Rich


  21. yair

    3 months ago

    hi Mikal, i am having trouble with string and number formaters like simpleMessageSystem firmeta and the likes. do you recommend using softwareserial against the computer?
    all i want is to toggle pins from processing…
    whats your toughts about this method?
    http://todbot.com/blog/2009/07/30/arduino-serial-protocol-design-patterns/


  22. Mikal

    3 months ago

    Gosh, Rich, that looks a lot like baud rate mismatch to me. Also, I’m a little alarmed that you are connecting an RS-232 device directly to your Arduino. You know, don’t you, that RS-232 logic voltage levels are incompatible with Arduino?

    Mikal


  23. Mikal

    3 months ago

    Yair–

    Yes, I use NewSoftSerial all the time to talk to computers. Is that your question?

    Mikal


  24. yair

    3 months ago

    yes, i have been reading about your work all over the arduino echo system and would like to hear your opnion on those parsers and methods.

    also thanks for the streaming page,
    http://arduiniana.org/libraries/streaming/


  25. Rich

    3 months ago

    Silly me - but of course! The uBlox kit puts level shifters on the UART and that got me going in the wrong direction. I bypassed the level shifters and it works like a champ! Thanks for the tip.
    Rich


  26. Al

    3 months ago

    Hi.

    Thanks for your great lib, but I’ve found a strange issue. After a lot of testing I’ve found than if I use the NewSoftSerial plus the Servo lib, my servo gets cray. The hardware is fine, and I can use the servo and the serial device using different code. But as soo as I use the sentence nss.begin(TRATE), the servo begins to move erratically.

    I’m new in this, so perhaps the questino is silly, but… Are both libs incompatible?


  27. Mikal

    3 months ago

    Al–

    This is definitely an issue that needs dealing with, but I discovered this as well. The 0017 version of the Servo library requires interrupts — interrupts which must be disabled for NewSoftSerial. So yes, they ARE incompatible. The workaround for my application (the Reverse Geocache Puzzle box) was to replace the new library with the 0016 version of Servo.

    Thanks for reminding me to bring this to attention.

    Mikal


  28. Jasonn

    3 months ago

    I know you say up to 28000, but could the code be pushed to 31250 baud for midi communication?


  29. Al

    3 months ago

    Thanks!!! now it works.

    The bad news are than Servo 0016 only works at ouput pins 9 or 10, and I’ll have to rebuild all my wiring.


  30. Mikal

    3 months ago

    Jasonn–

    NewSoftSerial explicitly supports 31250 baud. Should be no problem.

    Mikal


  31. pmgr

    2 months ago

    Hi Mikal,

    I am running into issues with a Arduino Pro board running ATmega328P at 8MHz. It seems the delay times don’t work properly for 38400 and 57600 baudrate and the serial output gets garbled. Any clues?

    PmgR


  32. Mikal

    2 months ago

    pmgr–

    Hmm… Those high speeds are pushing the envelope for an 8MHz chip, but I thought when I tested that configuration it (mostly) worked, especially TX. What does the garbled output look like?

    Mikal


  33. macegr

    2 months ago

    Here’s a somewhat tested DELAY_TABLE for running NewSoftSerial on a 20MHz Arduino. Seems to work well for send and receive all the way up to 115200 baud.

    #elif F_CPU == 20000000

    static const DELAY_TABLE PROGMEM table[] =
    {
    // baud rxcenter rxintra rxstop tx
    { 115200, 3, 21, 21, 18, },
    { 57600, 20, 43, 43, 41, },
    { 38400, 37, 73, 73, 70, },
    { 31250, 45, 89, 89, 88, },
    { 28800, 46, 98, 98, 95, },
    { 19200, 71, 148, 148, 145, },
    { 14400, 96, 197, 197, 194, },
    { 9600, 146, 297, 297, 294, },
    { 4800, 296, 595, 595, 592, },
    { 2400, 592, 1189, 1189, 1186, },
    { 1200, 1187, 2379, 2379, 2376, },
    { 300, 4759, 9523, 9523, 9520, },
    };

    #define XMIT_START_ADJUSTMENT 6


  34. Mikal

    2 months ago

    macegr–

    Outstanding! Thanks for the excellent submission. NewSoftSerial 10, here we come! :)

    Mikal


  35. Prune

    1 month ago

    Hi,
    First, thank you for your really goo work.

    I’m using a Arduino nano. I want to connect it to a xbee module.
    For now, I just plugged the nano in the usb port of my mac, and used another serial-to-USB cable to plug on pins 2/3 on the arduino.
    I’m using some of the example code.
    While things going from the arduino to the hardware serial port is fine, the data using the softserial port are messy.
    It’s the same kind of stuffs some reported above, but I can’t find any solution. I just understant it’s comming of serial port speed (baud) issues. I’ tried from 9600 down to 300 without more success.

    The usb to serial is a keyspan and is working well for other things (LCD screen, serial ports on network equipments…).

    1) do you think there is a problem with the Arduino nano V3 (328 chip) ?
    2) do you think there is a problem connecting tx/rx from the keyspan cable to the arduino’s pins ?
    3) do you have any idea left ?

    Many thanks.


  36. Mikal

    1 month ago

    Hmm… it’s a little difficult to tell. That configuration should work fine. Can you share more detail about when you mean by “messy”? Can you give an example? What version of NewSoftSerial are you using?

    Mikal


  37. Stanislav

    1 month ago

    Hello! First i want to say-thank you for this great lib! But i have a few questions:
    I’m using Arduino nano duo (last gen with 32kb mem) with single USB port
    So that means that my arduino have only 1 hardware serial…and i can use it via usb or via pins directly on arduino.

    So the question is:
    What happens if i query both newsoftserial and real hardware ports at same time ? Do they act same way like if they are both newserial (ie each time i .read() or .availible() 1 of them, others will flush buffers ?
    Is there any way to use hardware and software (newsoftserial) port at same time (performance side it will be ok..because both ibus and connection with pc is at 9600, and i’m getting like 1-3 byte’s from\to pc every 5 sec)?
    I’m using arduino with BMW I-BUS and need to communicate with car and pc at same time..

    I have tested this a bit today..and results are not good - i got a mess in incoming messages from i-bus…dunno why yet (because its like impossible to debug arduino…no way to know what happens when i recieve message from bus), i think maybe its because i query both Serial and NewSoftSerail…so i’m losing ~50% of info from both ports because of flushing buffers.
    ps sorry for my bad english.

    Mikal ?


  38. Stanislav

    1 month ago

    btw mess in messages from ibus - they are ok and no problems with baud rates…but i’m like losing big parts of messages
    also 1 more question - in what format data arriving to arduino - ie i need to sample it with my original messages and send answers when i see right info, like this:
    byte cdmode[7] = {0×68, 0×05, 0×18, 0×38, 0×00, 0×00, 0×4D};

    and the answer should be:
    byte CDplay[12] = {0×18, 0×0A, 0×68, 0×39, 0×02, 0×09, 0×00, 0×3F, 0×00, 0×01, 0×01};

    (it thos messages above in HEX)


  39. Mikal

    1 month ago

    Stanislav, I use the hardware and software ports together all the time. It should work fine. Make sure you read frequently enough so that the buffers don’t overflow. (Check with nss.overflow). If you are “losing big parts”, you probably are overflowing. If you need to send binary data, just write each byte like:

    Serial.print(CDplay[i], BYTE);

    Mikal


  40. Prune

    1 month ago

    Hi,

    Thanks Mikal for the answer.
    I’m using the code from Rich, just here in the comments :

    #include 
    
    NewSoftSerial mySerial(2, 3);
    void setup()
    {
      Serial.begin(9600);
      Serial.println("Goodnight moon!");
      mySerial.begin(9600);
      mySerial.println("Hello, world?");
    }
    
    void loop() // run over and over again
    {
      char c;
      if (mySerial.available()) {
        Serial.print("Data: ");
        c = mySerial.read();
        Serial.print(c);
        Serial.print(" ");
        Serial.print(c, DEC);
        Serial.print(" ");
        Serial.println(c, BIN);
      }
    
      if (Serial.available()) {
        mySerial.print("Data: ");
        c = Serial.read();
        mySerial.print(c);
        mySerial.print(" ");
        mySerial.print(c, DEC);
        mySerial.print(" ");
        mySerial.println(c, BIN);
      }
    }

    when I type “a” on the softserial console, I have on the arduino console :
    Data: O 79 1001111
    Data: 0 0

    If I enter “z”, I have :
    Data: ! 33 100001
    Data: 0 0

    –> caracters are shift, and I have a 0 sent after that

    If I type on the arduino console, I get on the softserial console :

    a -> W#=##}#4##gg####V###
    z -> W#=##
    #ж#####ggg##V###

    (on two lines).
    This happen whatever baud rate I try.

    Again, if I use the usb-to-serial adapter with another hardware, it’s working…

    Tomorrow I will try to plug two Arduino nano with a cross cable and see what happen. I’ll see if the problem is really from the usb-to-serial adapter… unless you have a better idea….
    Thanks.


  41. Mikal

    1 month ago

    @Prune, do you get the same values every time you type “a” on the softserial console? Are all the grounds tied together? Is that Serial-to-USB cable operating at TTL levels? If it’s RS-232 it won’t work.

    Mikal


  42. Prune

    1 month ago

    Mikal,

    Thanks for pointing that out. I seriously did not check !!!
    You should put this link (or a better one if you have) in a warning at the start of this page : http://www.seattlerobotics.org/encoder/aug97/cable.html

    I’m taking the adaptor apart, but i’m sure this is the root of all my problems.
    Wil let you know.
    Many thanks


  43. Jerry

    1 month ago

    Getting these errors when I try to compile your latest code:

    C:\dev\Arduino\arduino-0017\hardware\libraries\NewSoftSerial\NewSoftSerial.cpp: In member function ‘void NewSoftSerial::begin(long int)’:

    C:\dev\Arduino\arduino-0017\hardware\libraries\NewSoftSerial\NewSoftSerial.cpp:412: error: invalid type argument of ‘unary *’

    C:\dev\Arduino\arduino-0017\hardware\libraries\NewSoftSerial\NewSoftSerial.cpp:413: error: invalid type argument of ‘unary *’

    C:\dev\Arduino\arduino-0017\hardware\libraries\NewSoftSerial\NewSoftSerial.cpp: In member function ‘void NewSoftSerial::end()’:

    C:\dev\Arduino\arduino-0017\hardware\libraries\NewSoftSerial\NewSoftSerial.cpp:429: error: invalid type argument of ‘unary *’


  44. Mikal

    1 month ago

    Jerry, what kind of AVR processor are you compiling for?

    Mikal


  45. keywon

    1 month ago

    Hello Mikal, thank you for this lib, I’ve been using it for two months on a thesis project :)

    Sadly, it suddenly stopped working for me today and I was wondering how to troubleshoot it.

    I’ve been using this to communicate with a UART RFID reader @19200, and then pass it onto a python program via hardware serial @9600.

    When I run the test program that comes with the download (NewSoftSerialTest.pde), the soft serial doesn’t print the “Hello World” message it should. All I get in the serial monitor @4800 is one broken character like à or Ó. It prints “Goodnight moon” when I switch to the hardware serial @57600. When I send “a” etc. to the monitor, it doesn’t print anything at either 57600 or 4800.

    My RFID readers used to send a broken character (int value -1) when there is no tag present, and print ‘*’ + 16-byte tag ID when there’s one. Now all it’s sending is a broken character.

    I am assuming this is a problem with the Soft Serial communication because the RFID readers are printing the tag well over hardware serial when monitored in Terminal > Screen, with the ATMEGA chip removed. I have also re-installed the lib, Arduino 017 and the FTDI driver, and tried two different RFID readers as well. On a potentially related note, Arduino seems to forget to refresh Tools > Serial Port list half of the times when I plug in different boards, which also started today.

    How would you go about troubleshooting this? I am using it with Arduino Pro Mini 5v w/ATMEGA 328 16HMz, on Snow Leopard. I appreciate your help.


  46. Mikal

    1 month ago

    Keywon–

    Gosh, that’s a tough one. It was working previously? Are all the devices still grounded together?
    If you didn’t change anything with the software, I have to assume that something came loose. You’ve hooked the RFID RX pin to the NSS TX pin and vice versa?

    Mikal


  47. Denis

    1 month ago

    Hello.

    How i can configure start/stop with it’s library? Or it’s only planned feature?


  48. Mikal

    1 month ago

    Hi Denis–

    With NewSoftSerial 10, when you have finished with your communication, you can cease with

    nss.end();

    Does that answer your question?

    Mikal


  49. keywon

    1 month ago

    Mikal — thanks for the reply.

    I believe it’s grounded properly. Tried switching rx/tx as well :) Also tried:

    Watching the NSS rx/tx pins on oscilloscope: the pins are sending and receiving, we see the dense square wave, and they both read 5v when idle.

    Pull up resistor on NSS RX pin as suggested here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256934324: Makes no difference

    Using pins 0,1 for NSS like you suggested here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1252024653/4: worked as is, without NSS.available() or NSS.read()

    So we think that NSS is transmitting but not receiving. NSS.available() or NSS.read() isn’t working, as if there is nothing in the buffer.

    We tried same Arduino code and circuit on two Snow Leopard Macs, one Linux machine, one Windows (via VMware Fusion), and it only works on Windows now. All running Arduino 017, two Arduino mini pro 328 16mhz boards.

    I don’t have a good hypothesis. Does this ring any bell?
    Much appreciated –


  50. Mikal

    1 month ago

    Keywon, do I understand correctly?

    Working configurations: NSS on pins 0/1, or on other pins when serial partner is Windows.
    Not working (but did work previously): NSS on other pins when partner is Mac or Linux

    What version of NewSoftSerial are you using? Are those 3.3V Arduinos Pros? Is it possible that you have a voltage mismatch problem?

    This is a most curious issue.

    Mikal

14 Trackbacks For This Post
  1. NewSoftSerial 5 « Arduino

    [...] NewSoftSerial version 5 is available. A lot of people have been using this library — thanks! — but I really need to recognize the exceptional work of two contributors. [...]

  2. NewSoftSerial 6 « Arduiniana

    [...] I posted the new library. [...]

  3. เริ่มต้นสร้าง GPS จอสีกับอาดูอี้โน่ | Ayarafun Factory

    [...] ในรอบนี้ผมได้ใช้ newSoftwareSerial3 จะได้ลองด้วยว่า มีปัญหาไหม

  4. Unlogic » USB Storage and Arduino

    [...] of the first things to do is download NewSoftSerial

  5. The Hired Gun » GPS project: the Bluetooth saga

    [...] to the task at hand, which happened to be adding 3 lines of code: declaration of an instance of NewSoftSerial, calling the instance constructor with a baud rate, and a single call to pass the char from the [...]

  6. layer8 » Controlling A Roomba with an Arduino

    [...] and the XBee module has a serial interface.  So how does this really solve my problem?  Enter NewSoftSerial, an updated version of the Arduino software serial library, which basically lets you drive a serial [...]

  7. This and That » Blog Archive » Arduiniana - What else would it be?

    [...] is probably the coolest gift idea I’ve seen.  Mikal, you rock.  And also, thank you for NewSoftSerial.  [...]

  8. Interfacing the Arduino with the DS1616

    [...] way.  Let’s move onto the software.  Communication with the DS1616 is established using the NewSoftSerial library.  Getting data is essentially a case of lots of bit banging.  The DS1616 library [...]

  9. Serial Multiplexing « Interactive Environments Minor 2009-2010

    [...] So we started looking for a solution to overcome this tiny inconvenience. First we looked into a software serial but this didn’t work out, it was a bit too much for the arduino’s little processor to [...]

  10. Atmega/Arduino (Soft-) Serial Ports | Jochen Toppe's Blog

    [...] software serial port. I briefly thought about writing one, but then I found this great libary, the New SoftSerial. It is as simple to use as the original library, but unfortunately once I connect the RF receiver, [...]

  11. The Frustromantic Box, Part 4: Software « New Bright Idea

    [...] developers for the great libraries, and to Mikal Hart in particular for his work on the TinyGPS and NewSoftSerial [...]

  12. side2 » Bimeji Client for Arduino

    [...] このソースでは、PS2ライブラリとNewSoftSerialライブラリを利用しています。 コンパイルするには、これらのライブラリを有効にしておく必要があります。 [...]

  13. Live Twitter Table using New Bluetooth Shield | Club45

    [...] as a well. The shield can be wired to any of the pins on the Arduino. Right now we’re using NewSoftSerial on pins 4 and 5. It can be attached to the hardware RX and TX pins, but interferes with [...]

  14. tokyo->kobe->osaka << Motoi Ishibashi

    [...] 急遽、Arduinoでシリアル通信をふたつやる必要が発生してホテルで開発。といっても手元にハードがないので、ほとんど勘でプログラムしているようなもの。 次の日現場で試すも、予想通り動かない。そりゃそうだ。 NewSoftwareSerialなんていう便利なものがあるのを後で知った。 [...]

Leave a Reply