Two NewSoftSerial Ports

I seem to be having some trouble getting my project working with two NewSoftSerial devices.

I have: - A serial barcode scanner on ports 2 & 3 - An XBEE on ports 4 & 5

Both of these devices seem to work perfectly when I run them separately, but I am encountering problems when I run them together.

I need to be able to read incoming data (~15 bytes) from a serial barcode scanner (on ports 2 and 3) and send this off via the XBEE to a remote controller. The remote controller parses the information and responds (~ 80-160 bytes) back through the XBEE.

I see from various posts online and the NewSoftSerial examples that only one NSS can be active at a time. The example switches between them on a 10 second interval. This is too slow for my purposes. I have tried adjusting this delay as low as 250ms and no matter what I do, inputs from either the scanner or the XBEE are dropped.

I assumed that NSS would buffer some of the received characters so between slices I would still receive some or all of them. Is it possible to increase the size of the buffers, or am I doing something just plain wrong?

Here is the basic code -sans obvious declarations, buffer overrun checks, and debug messages to Serial.print():

void loop() { if ((millis() / 500) % 2 == 0) { if (ScanPort.available()) { sBuffer[iS] = (char)ScanPort.read();

if (sBuffer[iS] == 0x0D) { sBuffer[iS] = 0x00; XBEEPort.println(sBuffer); iS = 0; } else { iS++; } } else { if (XBEEPort.available()) { xBuffer[iX] = (char)XBEEPort.read();

if (xBuffer[iX] == 0x0D) { xBuffer[iX] = 0x00; // Process host response iX = 0; } else { iX++; } } } }

Thanks for any help you can offer...

Well, you state the issue with NewSoftSerial, and effectively ignore it. Only one NewSoftSerial device can be active at a time. Yet, you want to actively use two NewSoftSerial devices at a time, the problem lies in this, NewSoftSerial can only listen to one device.

Now you did provide a nice line that states what you want to do:

I need to be able to read incoming data (~15 bytes) from a serial barcode scanner (on ports 2 and 3) and send this off via the XBEE to a remote controller. The remote controller parses the information and responds (~ 80-160 bytes) back through the XBEE.

How about putting the barcode scanner as active NewSoftSerial (NSS) object, when it reads a barcode, put the XBEE NSS object as active, send your data, wait for the response, and then put the barcode scanner as active object again.

The trick with working with multiple NSS items is that you can only ever listen to one device at a time, and as such need to adjust your logic to take this limitation into account (your logic was allready compliant on this part).

Thanks for the prompt reply!

I could switch the active devices like that, but it would preclude the ability to send commands to my remote device from the host (the PC on the other end of the XBEE) - as the XBEE port would not be listening all of the time. This is something that I wanted the ability to do, but I guess I can skip that function if need be.

Is it possible to use a NSS port and and AFSoftSerial port concurrently or does whatever limitation afflicts the NSS afflict the AFS too? (apologies if this is a dumb question, but I do not understand the origin or the issue with NSS).

Thanks...

Why not use a NewSoftSerial port to listen to the scanner, and the hardware serial port to listen/talk to the XBee?

Good idea, but the hardware port is spoken for... I use that for monitoring through USB.

Thanks though...

AFSoftSerial stands for AdaFruitSoftSerial, it is the predecessor to NewSoftSerial, essentially they are the same. They would suffer the same limitation.

If you only use the hardware serial port for debugging purposes, I'd suggest using the hardware serial port for XBEE aswell. You can debug over XBEE too, afterall.

Hi Nateloaf--

The NewSoftSerial example that switches every 10 seconds is somewhat contrived. A more real-world example would be something like this:

  1. Listen to device A waiting for some activity -- a command or other interesting serial event.
  2. When that happens, activate device B and establish some communications with it. When communication is complete, go back to 1: listening on device A.

It sounds like you want to listen to both devices at once, and you can't.

Mikal